guest_vehicle_page.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import 'package:cs_resources/generated/l10n.dart';
  2. import 'package:cs_resources/theme/app_colors_theme.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.dart';
  5. import 'package:flutter_hooks/flutter_hooks.dart';
  6. import 'package:hooks_riverpod/hooks_riverpod.dart';
  7. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  8. import 'package:router/ext/auto_router_extensions.dart';
  9. import 'package:shared/utils/date_time_utils.dart';
  10. import 'package:shared/utils/util.dart';
  11. import 'package:widgets/ext/ex_widget.dart';
  12. import 'package:widgets/my_appbar.dart';
  13. import 'package:widgets/my_button.dart';
  14. import 'package:widgets/my_text_field.dart';
  15. import 'package:widgets/my_text_view.dart';
  16. import 'package:widgets/shatter/form_require_text.dart';
  17. import 'package:widgets/shatter/picker_container.dart';
  18. import 'package:widgets/widget_export.dart';
  19. import '../../router/page/form_page_router.dart';
  20. import 'vm/apply_view_model.dart';
  21. @RoutePage()
  22. class GuestVehiclePage extends HookConsumerWidget {
  23. const GuestVehiclePage({Key? key}) : super(key: key);
  24. //启动当前页面
  25. static void startInstance({BuildContext? context}) {
  26. if (context != null) {
  27. context.router.push(const GuestVehiclePageRoute());
  28. } else {
  29. appRouter.push(const GuestVehiclePageRoute());
  30. }
  31. }
  32. @override
  33. Widget build(BuildContext context, WidgetRef ref) {
  34. final viewModel = ref.watch(applyViewModelProvider.notifier);
  35. final state = ref.watch(applyViewModelProvider);
  36. // 使用 useState 来持久化 TextEditingController 和 FocusNode
  37. final TextEditingController vehicleNumController = useTextEditingController();
  38. final TextEditingController vehicleInfoController = useTextEditingController();
  39. final TextEditingController guestNameController = useTextEditingController();
  40. final TextEditingController guestPhoneController = useTextEditingController();
  41. final FocusNode vehicleNumFocusNode = useFocusNode();
  42. final FocusNode vehicleInfoFocusNode = useFocusNode();
  43. final FocusNode guestNameFocusNode = useFocusNode();
  44. final FocusNode guestPhoneFocusNode = useFocusNode();
  45. useEffect(() {
  46. //赋值State的值
  47. Future.microtask(() {
  48. vehicleNumController.text = state.formContentDetail.vehicleNumber ?? "";
  49. vehicleInfoController.text = state.formContentDetail.vehicleMakeModelColour ?? "";
  50. guestNameController.text = state.formContentDetail.guestName ?? "";
  51. guestPhoneController.text = state.formContentDetail.guestMobileNumber ?? "";
  52. });
  53. return () {};
  54. }, []);
  55. return WillPopScope(
  56. child: Scaffold(
  57. appBar: MyAppBar.appBar(context, state.detailPage?['title']),
  58. backgroundColor: context.appColors.backgroundWhite,
  59. body: Column(
  60. crossAxisAlignment: CrossAxisAlignment.start,
  61. children: [
  62. SingleChildScrollView(
  63. scrollDirection: Axis.vertical,
  64. physics: const BouncingScrollPhysics(),
  65. child: Container(
  66. width: double.infinity,
  67. child: Column(
  68. mainAxisSize: MainAxisSize.max,
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. children: [
  71. //下拉选 - 访问时间
  72. FormRequireText(
  73. text: S.current.date_of_entry,
  74. textColor: context.appColors.textBlack,
  75. fontSize: 17,
  76. fontWeight: FontWeight.w500,
  77. ).marginOnly(top: 30, bottom: 16, left: 15, right: 15),
  78. PickerContainer(
  79. hint: S.current.choose_date,
  80. enable: state.enableEdit,
  81. content: state.formContentDetail.dateOfEntry,
  82. onClick: () {
  83. viewModel.pickDate(null, (date) {
  84. viewModel.updateFormContentDetail((content) {
  85. content.dateOfEntry = DateTimeUtils.formatDate(date, format: 'yyyy-MM-dd');
  86. });
  87. });
  88. },
  89. ).marginOnly(left: 15, right: 15),
  90. //输入框 - 车牌号
  91. FormRequireText(
  92. text: S.current.vehicle_number,
  93. textColor: context.appColors.textBlack,
  94. fontSize: 17,
  95. fontWeight: FontWeight.w500,
  96. ).marginOnly(top: 14, bottom: 16, left: 15, right: 15),
  97. // 表单
  98. _buildInputLayout(
  99. context,
  100. "vehicle_num",
  101. enable: state.enableEdit,
  102. textInputType: TextInputType.text,
  103. textInputAction: TextInputAction.next,
  104. controller: vehicleNumController,
  105. focusNode: vehicleNumFocusNode,
  106. onSubmit: (formKey, value) {
  107. vehicleNumFocusNode.unfocus();
  108. FocusScope.of(context).requestFocus(vehicleInfoFocusNode);
  109. },
  110. ),
  111. //输入框 - 车辆信息
  112. FormRequireText(
  113. text: S.current.vehicle_make_model_colour,
  114. textColor: context.appColors.textBlack,
  115. fontSize: 17,
  116. fontWeight: FontWeight.w500,
  117. ).marginOnly(top: 14, bottom: 16, left: 15, right: 15),
  118. // 表单
  119. _buildInputLayout(
  120. context,
  121. "vehicle_info",
  122. enable: state.enableEdit,
  123. textInputType: TextInputType.text,
  124. textInputAction: TextInputAction.next,
  125. controller: vehicleInfoController,
  126. focusNode: vehicleInfoFocusNode,
  127. onSubmit: (formKey, value) {
  128. vehicleInfoFocusNode.unfocus();
  129. FocusScope.of(context).requestFocus(guestNameFocusNode);
  130. },
  131. ),
  132. //输入框 - 访客姓名
  133. FormRequireText(
  134. text: S.current.guest_name,
  135. textColor: context.appColors.textBlack,
  136. fontSize: 17,
  137. fontWeight: FontWeight.w500,
  138. ).marginOnly(top: 14, bottom: 16, left: 15, right: 15),
  139. // 表单
  140. _buildInputLayout(
  141. context,
  142. "guest_name",
  143. enable: state.enableEdit,
  144. textInputType: TextInputType.text,
  145. textInputAction: TextInputAction.next,
  146. controller: guestNameController,
  147. focusNode: guestNameFocusNode,
  148. onSubmit: (formKey, value) {
  149. guestNameFocusNode.unfocus();
  150. FocusScope.of(context).requestFocus(guestPhoneFocusNode);
  151. },
  152. ),
  153. //输入框 - 访客电话
  154. FormRequireText(
  155. text: S.current.guest_mobile_number,
  156. textColor: context.appColors.textBlack,
  157. fontSize: 17,
  158. fontWeight: FontWeight.w500,
  159. ).marginOnly(top: 14, bottom: 16, left: 15, right: 15),
  160. // 表单
  161. _buildInputLayout(
  162. context,
  163. "guest_phone",
  164. enable: state.enableEdit,
  165. textInputType: TextInputType.phone,
  166. textInputAction: TextInputAction.done,
  167. controller: guestPhoneController,
  168. focusNode: guestPhoneFocusNode,
  169. onSubmit: (formKey, value) {
  170. guestPhoneFocusNode.unfocus();
  171. },
  172. ),
  173. ],
  174. ),
  175. ),
  176. ).expanded(),
  177. //底部按钮
  178. MyButton(
  179. onPressed: () {
  180. vehicleNumFocusNode.unfocus();
  181. vehicleInfoFocusNode.unfocus();
  182. guestNameFocusNode.unfocus();
  183. guestPhoneFocusNode.unfocus();
  184. if (state.enableEdit) {
  185. String? vehicleNumber = vehicleNumController.text;
  186. String? vehicleMakeModelColour = vehicleInfoController.text;
  187. String? guestName = guestNameController.text;
  188. String? guestMobileNumber = guestPhoneController.text;
  189. //校验数据
  190. final dateOfEntry = state.formContentDetail.dateOfEntry;
  191. if (Utils.isEmpty(dateOfEntry)) {
  192. ToastEngine.show("Select date of entry");
  193. return;
  194. }
  195. if (Utils.isEmpty(vehicleNumber)) {
  196. ToastEngine.show("Enter vehicle number");
  197. return;
  198. }
  199. if (Utils.isEmpty(vehicleMakeModelColour)) {
  200. ToastEngine.show("Enter vehicle make/model/ colour");
  201. return;
  202. }
  203. if (Utils.isEmpty(guestName)) {
  204. ToastEngine.show("Enter guest name");
  205. return;
  206. }
  207. if (Utils.isEmpty(guestMobileNumber)) {
  208. ToastEngine.show("Enter guest mobile number");
  209. return;
  210. }
  211. //存入本表单数据
  212. viewModel.updateFormContentDetail((content) {
  213. content.vehicleNumber = vehicleNumber;
  214. content.vehicleMakeModelColour = vehicleMakeModelColour;
  215. content.guestName = guestName;
  216. content.guestMobileNumber = guestMobileNumber;
  217. });
  218. }
  219. viewModel.gotoNextPage();
  220. },
  221. text: S.current.next,
  222. textColor: Colors.white,
  223. backgroundColor: context.appColors.btnBgDefault,
  224. fontWeight: FontWeight.w500,
  225. type: ClickType.throttle,
  226. fontSize: 16,
  227. minHeight: 50,
  228. radius: 0,
  229. ),
  230. ],
  231. )),
  232. onWillPop: () async {
  233. viewModel.handlePopAction();
  234. return true;
  235. });
  236. }
  237. Widget _buildInputLayout(
  238. BuildContext context,
  239. String key, {
  240. double marginTop = 0,
  241. bool? showRightIcon = false, //是否展示右侧的布局
  242. Widget? rightWidget, //右侧的布局
  243. TextInputType textInputType = TextInputType.text,
  244. String? errorText,
  245. bool obscureText = false,
  246. required TextEditingController? controller,
  247. required FocusNode focusNode,
  248. bool enable = true,
  249. TextInputAction textInputAction = TextInputAction.done,
  250. Function? onSubmit,
  251. }) {
  252. return IgnoreKeyboardDismiss(
  253. child: MyTextField(
  254. key,
  255. fillBackgroundColor: context.appColors.authFiledBG,
  256. "",
  257. hintStyle: TextStyle(
  258. color: context.appColors.authFiledHint,
  259. fontSize: 16.0,
  260. fontWeight: FontWeight.w500,
  261. ),
  262. controller: controller,
  263. focusNode: focusNode,
  264. margin: EdgeInsets.only(top: marginTop, left: 15, right: 15),
  265. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 3),
  266. showDivider: false,
  267. height: 44,
  268. enabled: enable,
  269. style: TextStyle(
  270. color: context.appColors.authFiledText,
  271. fontSize: 16.0,
  272. fontWeight: FontWeight.w500,
  273. ),
  274. inputType: textInputType,
  275. textInputAction: textInputAction,
  276. onSubmit: onSubmit,
  277. cursorColor: context.appColors.authFiledText,
  278. obscureText: obscureText,
  279. errorText: errorText,
  280. showLeftIcon: true,
  281. showRightIcon: showRightIcon,
  282. rightWidget: rightWidget,
  283. ),
  284. );
  285. }
  286. }