import 'package:flutter/material.dart'; import 'package:lszlgl/base/base_lifecycle_state.dart'; class QyPopWidget extends StatefulWidget { final List pTitleList; final String otherStr; final Function(String nameStr, String otherStr) pTitleCallBack; const QyPopWidget( {super.key, required this.pTitleList, required this.otherStr, required this.pTitleCallBack}); @override State createState() => _QyPopWidgetState(); } class _QyPopWidgetState extends State { late TextEditingController ctrl; final ScrollController sctrl = ScrollController(); @override void initState() { super.initState(); ctrl = TextEditingController(text: widget.otherStr); // 赋值 } @override Widget build(BuildContext context) { return PopScope( canPop: false, // 关闭系统返回,防止选中数据出错 child: GestureDetector( behavior: HitTestBehavior.translucent, onTap: () { FocusManager.instance.primaryFocus?.unfocus(); }, child: SafeArea( child: SingleChildScrollView( controller: sctrl, padding: EdgeInsets.fromLTRB(12, 22, 12, MediaQuery.of(context).viewInsets.bottom), child: Column( mainAxisSize: MainAxisSize.min, // crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text( '请选择或填写扦样陪同人员(最多2人)', style: TextStyle(fontSize: 15), textAlign: TextAlign.center, ), const SizedBox(height: 16), Wrap( spacing: 10, children: List.generate( widget.pTitleList.length, (i) => ActionChip( backgroundColor: Colors.grey[200], padding: const EdgeInsets.all(6), side: BorderSide.none, shape: const RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(18))), label: Text( widget.pTitleList[i].name, style: widget.pTitleList[i].isChose ? const TextStyle(fontSize: 14, color: Color(0xFF25A6EE)) : const TextStyle(fontSize: 14, color: Colors.grey), ), onPressed: () { setState(() { widget.pTitleList[i].isChose = !widget.pTitleList[i].isChose; }); })), ), const SizedBox(height: 12), TextField( controller: ctrl, style: const TextStyle(fontSize: 14), decoration: const InputDecoration( border: InputBorder.none, icon: Text('其他陪同人员:'), hintText: '多人请用逗号隔开', isDense: true), onTap: () { Future.delayed(const Duration(milliseconds: 600), () { sctrl.animateTo( sctrl.position.maxScrollExtent, duration: const Duration(milliseconds: 400), curve: Curves.easeOut, ); }); }, ), const Divider(color: Color(0xFFE7E7E7), height: 8, thickness: 1), const SizedBox(height: 12), TextButton( style: ButtonStyle( backgroundColor: WidgetStateProperty.all(const Color(0xFF25A6EE)), foregroundColor: WidgetStateProperty.all(Colors.white), ), onPressed: () { String finalVal = ''; for (OtherPeopleModel model in widget.pTitleList) { if (model.isChose) { finalVal = '$finalVal${model.name},'; } } String inputVal = ctrl.text; if (inputVal.isNotEmpty) { List result = inputVal.split(RegExp(r'[,,.。;;\s]')); result.removeWhere((val) => val == ''); inputVal = result.join(','); if (inputVal.isNotEmpty) { // 判断只输入符号出现问题 finalVal = '$finalVal$inputVal,'; } } FocusManager.instance.primaryFocus?.unfocus(); if (finalVal.isNotEmpty) { String vals = finalVal.substring(0, finalVal.length - 1); List li = vals.split(','); if (li.length > 2) { MyNavigator.showToast('最多两个陪同人员'); return; } widget.pTitleCallBack.call(vals, inputVal); } else { // 输入为空 选择为空 widget.pTitleCallBack.call('', inputVal); } Navigator.pop(context); }, child: const Text('确 定')), ], )), ), ), ); } } class OtherPeopleModel { String? name; bool isChose; OtherPeopleModel({this.name, this.isChose = false}); }