moving_company_page.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/util.dart';
  10. import 'package:widgets/ext/ex_widget.dart';
  11. import 'package:widgets/my_appbar.dart';
  12. import 'package:widgets/my_button.dart';
  13. import 'package:widgets/my_text_field.dart';
  14. import 'package:widgets/my_text_view.dart';
  15. import 'package:widgets/shatter/form_require_text.dart';
  16. import 'package:widgets/widget_export.dart';
  17. import '../../router/page/form_page_router.dart';
  18. import 'vm/apply_view_model.dart';
  19. @RoutePage()
  20. class MovingCompanyPage extends HookConsumerWidget {
  21. const MovingCompanyPage({Key? key}) : super(key: key);
  22. //启动当前页面
  23. static void startInstance({BuildContext? context}) {
  24. if (context != null) {
  25. context.router.push(const MovingCompanyPageRoute());
  26. } else {
  27. appRouter.push(const MovingCompanyPageRoute());
  28. }
  29. }
  30. @override
  31. Widget build(BuildContext context, WidgetRef ref) {
  32. final viewModel = ref.watch(applyViewModelProvider.notifier);
  33. final state = ref.watch(applyViewModelProvider);
  34. // 使用 useState 来持久化 TextEditingController 和 FocusNode
  35. final companyController = useTextEditingController();
  36. final contactController = useTextEditingController();
  37. final phoneController = useTextEditingController();
  38. final addressController = useTextEditingController();
  39. final companyFocusNode = useFocusNode();
  40. final contactFocusNode = useFocusNode();
  41. final phoneFocusNode = useFocusNode();
  42. final addressFocusNode = useFocusNode();
  43. useEffect(() {
  44. //赋值State的值
  45. Future.microtask(() {
  46. companyController.text = state.formContentDetail.movingCompany ?? "";
  47. contactController.text = state.formContentDetail.personInCharge ?? "";
  48. phoneController.text = state.formContentDetail.mobileNumber ?? "";
  49. addressController.text = state.formContentDetail.companyAddress ?? "";
  50. });
  51. return () {
  52. // 释放控制器资源
  53. };
  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.moving_company,
  74. textColor: context.appColors.textBlack,
  75. fontSize: 17,
  76. fontWeight: FontWeight.w500,
  77. ).marginOnly(top: 14, bottom: 16, left: 15, right: 15),
  78. // 表单
  79. _buildInputLayout(
  80. context,
  81. "company",
  82. enable: state.enableEdit,
  83. textInputType: TextInputType.text,
  84. textInputAction: TextInputAction.next,
  85. controller: companyController,
  86. focusNode: companyFocusNode,
  87. onSubmit: (formKey, value) {
  88. companyFocusNode.unfocus();
  89. FocusScope.of(context).requestFocus(contactFocusNode);
  90. },
  91. ),
  92. //输入框 - 联系人
  93. FormRequireText(
  94. text: S.current.person_in_charge,
  95. textColor: context.appColors.textBlack,
  96. fontSize: 17,
  97. fontWeight: FontWeight.w500,
  98. ).marginOnly(top: 14, bottom: 16, left: 15, right: 15),
  99. // 表单
  100. _buildInputLayout(
  101. context,
  102. "contact",
  103. enable: state.enableEdit,
  104. textInputType: TextInputType.text,
  105. textInputAction: TextInputAction.next,
  106. controller: contactController,
  107. focusNode: contactFocusNode,
  108. onSubmit: (formKey, value) {
  109. contactFocusNode.unfocus();
  110. FocusScope.of(context).requestFocus(phoneFocusNode);
  111. },
  112. ),
  113. //输入框 - 联系电话
  114. FormRequireText(
  115. text: S.current.mobile_phone,
  116. textColor: context.appColors.textBlack,
  117. fontSize: 17,
  118. fontWeight: FontWeight.w500,
  119. ).marginOnly(top: 14, bottom: 16, left: 15, right: 15),
  120. // 表单
  121. _buildInputLayout(
  122. context,
  123. "phone",
  124. enable: state.enableEdit,
  125. textInputType: TextInputType.phone,
  126. textInputAction: TextInputAction.next,
  127. controller: phoneController,
  128. focusNode: phoneFocusNode,
  129. onSubmit: (formKey, value) {
  130. phoneFocusNode.unfocus();
  131. FocusScope.of(context).requestFocus(addressFocusNode);
  132. },
  133. ),
  134. //输入框 - 公司地址
  135. MyTextView(
  136. S.current.company_address,
  137. fontSize: 17,
  138. marginTop: 14,
  139. marginBottom: 16,
  140. marginLeft: 15,
  141. marginRight: 15,
  142. isFontMedium: true,
  143. textColor: context.appColors.textBlack,
  144. ),
  145. //文本框
  146. IgnoreKeyboardDismiss(
  147. child: Container(
  148. height: 160,
  149. margin: const EdgeInsets.symmetric(horizontal: 15),
  150. padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 15),
  151. decoration: BoxDecoration(
  152. color: context.appColors.authFiledBG,
  153. borderRadius: const BorderRadius.all(Radius.circular(5)),
  154. ),
  155. child: TextField(
  156. cursorColor: context.appColors.authFiledText,
  157. cursorWidth: 1.5,
  158. autofocus: false,
  159. enabled: state.enableEdit,
  160. focusNode: addressFocusNode,
  161. controller: addressController,
  162. decoration: InputDecoration(
  163. isDense: true,
  164. isCollapsed: true,
  165. border: InputBorder.none,
  166. hintStyle: TextStyle(
  167. color: context.appColors.authFiledHint,
  168. fontSize: 16.0,
  169. fontWeight: FontWeight.w500,
  170. ),
  171. ),
  172. style: TextStyle(
  173. color: context.appColors.authFiledText,
  174. fontSize: 16.0,
  175. fontWeight: FontWeight.w500,
  176. ),
  177. textInputAction: TextInputAction.done,
  178. onSubmitted: (value) {
  179. FocusScope.of(context).unfocus();
  180. },
  181. maxLines: null,
  182. expands: true,
  183. onChanged: (text) {
  184. // 当文本改变时
  185. },
  186. ),
  187. ),
  188. ),
  189. ],
  190. ),
  191. ),
  192. ).expanded(),
  193. //底部按钮
  194. MyButton(
  195. onPressed: () {
  196. companyFocusNode.unfocus();
  197. contactFocusNode.unfocus();
  198. phoneFocusNode.unfocus();
  199. addressFocusNode.unfocus();
  200. if (state.enableEdit) {
  201. String? movingCompany = companyController.text;
  202. String? personInCharge = contactController.text;
  203. String? mobileNumber = phoneController.text;
  204. String? companyAddress = addressController.text;
  205. if (Utils.isEmpty(movingCompany)) {
  206. ToastEngine.show("Enter Moving Company");
  207. return;
  208. }
  209. if (Utils.isEmpty(personInCharge)) {
  210. ToastEngine.show("Enter Person In Charge");
  211. return;
  212. }
  213. if (Utils.isEmpty(mobileNumber)) {
  214. ToastEngine.show("Enter Mobile Number");
  215. return;
  216. }
  217. //存入本表单数据
  218. viewModel.updateFormContentDetail((content) {
  219. content.movingCompany = movingCompany;
  220. content.personInCharge = personInCharge;
  221. content.mobileNumber = mobileNumber;
  222. content.companyAddress = companyAddress;
  223. });
  224. }
  225. viewModel.gotoNextPage();
  226. },
  227. text: S.current.next,
  228. textColor: Colors.white,
  229. backgroundColor: context.appColors.btnBgDefault,
  230. fontWeight: FontWeight.w500,
  231. type: ClickType.throttle,
  232. fontSize: 16,
  233. minHeight: 50,
  234. radius: 0,
  235. ),
  236. ],
  237. )),
  238. onWillPop: () async {
  239. viewModel.handlePopAction();
  240. return true;
  241. });
  242. }
  243. Widget _buildInputLayout(
  244. BuildContext context,
  245. String key, {
  246. double marginTop = 0,
  247. bool? showRightIcon = false, //是否展示右侧的布局
  248. Widget? rightWidget, //右侧的布局
  249. TextInputType textInputType = TextInputType.text,
  250. String? errorText,
  251. bool obscureText = false,
  252. required TextEditingController controller,
  253. required FocusNode focusNode,
  254. bool enable = true,
  255. TextInputAction textInputAction = TextInputAction.done,
  256. Function? onSubmit,
  257. }) {
  258. return IgnoreKeyboardDismiss(
  259. child: MyTextField(
  260. key,
  261. fillBackgroundColor: context.appColors.authFiledBG,
  262. "",
  263. hintStyle: TextStyle(
  264. color: context.appColors.authFiledHint,
  265. fontSize: 16.0,
  266. fontWeight: FontWeight.w500,
  267. ),
  268. controller: controller,
  269. focusNode: focusNode,
  270. margin: EdgeInsets.only(top: marginTop, left: 15, right: 15),
  271. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 3),
  272. showDivider: false,
  273. height: 44,
  274. enabled: enable,
  275. style: TextStyle(
  276. color: context.appColors.authFiledText,
  277. fontSize: 16.0,
  278. fontWeight: FontWeight.w500,
  279. ),
  280. inputType: textInputType,
  281. textInputAction: textInputAction,
  282. onSubmit: onSubmit,
  283. cursorColor: context.appColors.authFiledText,
  284. obscureText: obscureText,
  285. errorText: errorText,
  286. showLeftIcon: true,
  287. showRightIcon: showRightIcon,
  288. rightWidget: rightWidget,
  289. ),
  290. );
  291. }
  292. }