ToneSnap_FSX_Flutter/lib/components/dialog/rename_dialog.dart
2024-08-01 13:38:25 +08:00

166 lines
5.3 KiB
Dart

// Author: fengshengxiong
// Date: 2024/5/11
// Description: 重命名弹框
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/components/divider_widget.dart';
import 'package:tone_snap/utils/obj_util.dart';
class RenameDialog extends StatefulWidget {
const RenameDialog({
super.key,
required this.name,
required this.confirmOnTap,
});
final String name;
final Function(String value) confirmOnTap;
@override
State<StatefulWidget> createState() => RenameDialogState();
}
class RenameDialogState extends State<RenameDialog> {
final _textEditingController = TextEditingController();
int _cursorPosition = 0;
@override
void initState() {
super.initState();
_textEditingController.text = widget.name;
_textEditingController.addListener(_saveCursorPosition);
}
@override
void dispose() {
_textEditingController.removeListener(_saveCursorPosition);
_textEditingController.dispose();
super.dispose();
}
void _saveCursorPosition() {
_cursorPosition = _textEditingController.selection.base.offset;
}
@override
Widget build(BuildContext context) {
return PopScope(
canPop: true,
child: Center(
child: SingleChildScrollView(
padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: Container(
width: 0.8.sw,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8).r,
),
child: IntrinsicHeight(
child: Column(
children: [
SizedBox(height: 10.h),
Text(
'Rename',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 18.sp,
fontWeight: FontWeight.w500,
),
),
Container(
height: 40.h,
alignment: Alignment.center,
margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.h),
decoration: BoxDecoration(
color: const Color(0xFFf9f9fc),
borderRadius: BorderRadius.circular(8).r,
),
child: TextField(
maxLines: 1,
controller: _textEditingController,
textInputAction: TextInputAction.done,
textAlign: TextAlign.start,
keyboardType: TextInputType.text,
style: TextStyle(color: Colors.black, fontSize: 16.sp),
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
counterText: '',
hintText: 'please enter',
hintStyle: TextStyle(color: const Color(0xFF999999), fontSize: 16.sp,),
contentPadding: const EdgeInsets.symmetric(horizontal: 10).w,
isCollapsed: true,
border: InputBorder.none,
),
onChanged: (text) {
_textEditingController.selection = TextSelection.fromPosition(
TextPosition(offset: _cursorPosition),
);
},
),
),
const DividerWidget(),
SizedBox(
height: 52.h,
child: Row(
children: [
_optionButton('Cancel', false),
Container(
width: 1.w,
height: double.infinity,
color: const Color(0xFFE5E5E5),
),
_optionButton('Confirm', true),
],
),
),
],
),
),
),
),
),
);
}
Widget _optionButton(String label, bool isConfirm) {
return Expanded(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
if (isConfirm) {
if (!ObjUtil.isNotEmpty(_textEditingController.text)) {
BaseEasyLoading.toast('name cannot be empty');
return;
}
Get.back();
widget.confirmOnTap(_textEditingController.text);
} else {
Get.back();
}
},
child: SizedBox(
height: double.infinity,
child: Center(
child: Text(
label,
textAlign: TextAlign.center,
style: TextStyle(
color: isConfirm ? Colors.black : const Color(0xFF666666),
fontSize: 16.sp,
fontWeight: FontWeight.w500,
),
),
),
),
),
),
);
}
}