qypop_widget.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import 'package:flutter/material.dart';
  2. import 'package:lszlgl/base/base_lifecycle_state.dart';
  3. class QyPopWidget extends StatefulWidget {
  4. final List pTitleList;
  5. final String otherStr;
  6. final Function(String nameStr, String otherStr) pTitleCallBack;
  7. const QyPopWidget(
  8. {super.key, required this.pTitleList, required this.otherStr, required this.pTitleCallBack});
  9. @override
  10. State<QyPopWidget> createState() => _QyPopWidgetState();
  11. }
  12. class _QyPopWidgetState extends State<QyPopWidget> {
  13. late TextEditingController ctrl;
  14. final ScrollController sctrl = ScrollController();
  15. @override
  16. void initState() {
  17. super.initState();
  18. ctrl = TextEditingController(text: widget.otherStr); // 赋值
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. return PopScope(
  23. canPop: false, // 关闭系统返回,防止选中数据出错
  24. child: GestureDetector(
  25. behavior: HitTestBehavior.translucent,
  26. onTap: () {
  27. FocusManager.instance.primaryFocus?.unfocus();
  28. },
  29. child: SafeArea(
  30. child: SingleChildScrollView(
  31. controller: sctrl,
  32. padding: EdgeInsets.fromLTRB(12, 22, 12, MediaQuery.of(context).viewInsets.bottom),
  33. child: Column(
  34. mainAxisSize: MainAxisSize.min, //
  35. crossAxisAlignment: CrossAxisAlignment.stretch,
  36. children: [
  37. const Text(
  38. '请选择或填写扦样陪同人员(最多2人)',
  39. style: TextStyle(fontSize: 15),
  40. textAlign: TextAlign.center,
  41. ),
  42. const SizedBox(height: 16),
  43. Wrap(
  44. spacing: 10,
  45. children: List.generate(
  46. widget.pTitleList.length,
  47. (i) => ActionChip(
  48. backgroundColor: Colors.grey[200],
  49. padding: const EdgeInsets.all(6),
  50. side: BorderSide.none,
  51. shape: const RoundedRectangleBorder(
  52. borderRadius: BorderRadius.all(Radius.circular(18))),
  53. label: Text(
  54. widget.pTitleList[i].name,
  55. style: widget.pTitleList[i].isChose
  56. ? const TextStyle(fontSize: 14, color: Color(0xFF25A6EE))
  57. : const TextStyle(fontSize: 14, color: Colors.grey),
  58. ),
  59. onPressed: () {
  60. setState(() {
  61. widget.pTitleList[i].isChose = !widget.pTitleList[i].isChose;
  62. });
  63. })),
  64. ),
  65. const SizedBox(height: 12),
  66. TextField(
  67. controller: ctrl,
  68. style: const TextStyle(fontSize: 14),
  69. decoration: const InputDecoration(
  70. border: InputBorder.none,
  71. icon: Text('其他陪同人员:'),
  72. hintText: '多人请用逗号隔开',
  73. isDense: true),
  74. onTap: () {
  75. Future.delayed(const Duration(milliseconds: 600), () {
  76. sctrl.animateTo(
  77. sctrl.position.maxScrollExtent,
  78. duration: const Duration(milliseconds: 400),
  79. curve: Curves.easeOut,
  80. );
  81. });
  82. },
  83. ),
  84. const Divider(color: Color(0xFFE7E7E7), height: 8, thickness: 1),
  85. const SizedBox(height: 12),
  86. TextButton(
  87. style: ButtonStyle(
  88. backgroundColor: WidgetStateProperty.all(const Color(0xFF25A6EE)),
  89. foregroundColor: WidgetStateProperty.all(Colors.white),
  90. ),
  91. onPressed: () {
  92. String finalVal = '';
  93. for (OtherPeopleModel model in widget.pTitleList) {
  94. if (model.isChose) {
  95. finalVal = '$finalVal${model.name},';
  96. }
  97. }
  98. String inputVal = ctrl.text;
  99. if (inputVal.isNotEmpty) {
  100. List<String> result = inputVal.split(RegExp(r'[,,.。;;\s]'));
  101. result.removeWhere((val) => val == '');
  102. inputVal = result.join(',');
  103. if (inputVal.isNotEmpty) {
  104. // 判断只输入符号出现问题
  105. finalVal = '$finalVal$inputVal,';
  106. }
  107. }
  108. FocusManager.instance.primaryFocus?.unfocus();
  109. if (finalVal.isNotEmpty) {
  110. String vals = finalVal.substring(0, finalVal.length - 1);
  111. List<String> li = vals.split(',');
  112. if (li.length > 2) {
  113. MyNavigator.showToast('最多两个陪同人员');
  114. return;
  115. }
  116. widget.pTitleCallBack.call(vals, inputVal);
  117. } else {
  118. // 输入为空 选择为空
  119. widget.pTitleCallBack.call('', inputVal);
  120. }
  121. Navigator.pop(context);
  122. },
  123. child: const Text('确 定')),
  124. ],
  125. )),
  126. ),
  127. ),
  128. );
  129. }
  130. }
  131. class OtherPeopleModel {
  132. String? name;
  133. bool isChose;
  134. OtherPeopleModel({this.name, this.isChose = false});
  135. }