select_unit_page.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:cs_resources/generated/l10n.dart';
  3. import 'package:cs_resources/theme/app_colors_theme.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:auto_route/auto_route.dart';
  6. import 'package:flutter_hooks/flutter_hooks.dart';
  7. import 'package:hooks_riverpod/hooks_riverpod.dart';
  8. import 'package:router/ext/auto_router_extensions.dart';
  9. import 'package:shared/utils/log_utils.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_load_image.dart';
  14. import 'package:widgets/my_text_field.dart';
  15. import 'package:widgets/my_text_view.dart';
  16. import 'package:widgets/widget_export.dart';
  17. import '../../router/page/auth_page_router.dart';
  18. import 'select_unit_state.dart';
  19. import 'select_unit_view_model.dart';
  20. @RoutePage()
  21. class SelectUnitPage extends HookConsumerWidget {
  22. SelectUnitPage({Key? key}) : super(key: key);
  23. //启动当前页面
  24. static void startInstance({BuildContext? context}) {
  25. if (context != null) {
  26. context.router.push(SelectUnitPageRoute());
  27. } else {
  28. appRouter.push(SelectUnitPageRoute());
  29. }
  30. }
  31. // 为需要测量的控件创建 GlobalKey
  32. final GlobalKey _appbarKey = GlobalKey();
  33. final GlobalKey _topImageKey = GlobalKey();
  34. final GlobalKey _inputKey = GlobalKey();
  35. final GlobalKey _description1Key = GlobalKey();
  36. final GlobalKey _description2Key = GlobalKey();
  37. final GlobalKey _description3Key = GlobalKey();
  38. final GlobalKey _buttonKey = GlobalKey();
  39. @override
  40. Widget build(BuildContext context, WidgetRef ref) {
  41. final viewModel = ref.watch(selectUnitViewModelProvider.notifier);
  42. final state = ref.watch(selectUnitViewModelProvider);
  43. // 获取屏幕高度
  44. final screenHeight = MediaQuery.of(context).size.height;
  45. final statusBarHeight = MediaQuery.of(context).padding.top;
  46. final navigationBarHeight = MediaQuery.of(context).padding.bottom;
  47. useEffect(() {
  48. double usedHeight = 0;
  49. // 组件挂载时执行,获取控件高度
  50. WidgetsBinding.instance.addPostFrameCallback((_) {
  51. // 获取各个控件的高度
  52. usedHeight += _appbarKey.currentContext?.size?.height ?? 0;
  53. usedHeight += _topImageKey.currentContext?.size?.height ?? 0;
  54. usedHeight += _inputKey.currentContext?.size?.height ?? 0;
  55. usedHeight += _description1Key.currentContext?.size?.height ?? 0;
  56. usedHeight += _description2Key.currentContext?.size?.height ?? 0;
  57. usedHeight += _description3Key.currentContext?.size?.height ?? 0;
  58. usedHeight += _buttonKey.currentContext?.size?.height ?? 0;
  59. // 计算剩余空间
  60. double remainingSpace = screenHeight - statusBarHeight - navigationBarHeight - usedHeight - 28 - 18 - 25 - 20 - 20;
  61. Log.d("计算剩余空间:$remainingSpace");
  62. if (remainingSpace > 0) {
  63. // 设置一个状态来存储剩余空间的高度
  64. viewModel.setRemainingSpace(remainingSpace);
  65. }
  66. });
  67. return () {
  68. // 组件卸载时执行
  69. };
  70. }, []);
  71. return Scaffold(
  72. appBar: MyAppBar.appBar(context, S.current.yy_home_accounts, key: _appbarKey),
  73. backgroundColor: context.appColors.backgroundDefault,
  74. body: SingleChildScrollView(
  75. scrollDirection: Axis.vertical,
  76. physics: const BouncingScrollPhysics(),
  77. child: Container(
  78. padding: const EdgeInsets.symmetric(horizontal: 20),
  79. width: double.infinity,
  80. child: Column(
  81. mainAxisSize: MainAxisSize.max,
  82. crossAxisAlignment: CrossAxisAlignment.center,
  83. children: [
  84. //顶部图片
  85. MyAssetImage(
  86. key: _topImageKey,
  87. Assets.authSignUpUnitImg,
  88. width: 266.5,
  89. height: 162,
  90. ).marginOnly(top: 28, bottom: 18),
  91. Row(
  92. key: _inputKey,
  93. mainAxisSize: MainAxisSize.min,
  94. children: [
  95. //街区
  96. Column(
  97. crossAxisAlignment: CrossAxisAlignment.start,
  98. children: [
  99. MyTextView(
  100. S.current.block,
  101. marginBottom: 9,
  102. textColor: context.appColors.textBlack,
  103. fontSize: 16,
  104. isFontMedium: true,
  105. ),
  106. // 表单 - 街区
  107. _buildInputLayout(
  108. context,
  109. state,
  110. "block",
  111. textInputAction: TextInputAction.next,
  112. onSubmit: (formKey, value) {
  113. state.formData[formKey]!['focusNode'].unfocus();
  114. FocusScope.of(context).requestFocus(state.formData['unit']!['focusNode']);
  115. },
  116. ).constrained(width: 88),
  117. ],
  118. ),
  119. MyTextView(
  120. "#",
  121. marginTop: 20,
  122. marginLeft: 8.5,
  123. marginRight: 8.5,
  124. textColor: context.appColors.textBlack,
  125. fontSize: 16,
  126. isFontMedium: true,
  127. ),
  128. //单元
  129. Column(
  130. crossAxisAlignment: CrossAxisAlignment.start,
  131. children: [
  132. MyTextView(
  133. S.current.unit_number,
  134. marginBottom: 9,
  135. textColor: context.appColors.textBlack,
  136. fontSize: 16,
  137. isFontMedium: true,
  138. ),
  139. Row(
  140. children: [
  141. // 表单 - 单元
  142. _buildInputLayout(
  143. context,
  144. state,
  145. "unit",
  146. textInputAction: TextInputAction.next,
  147. onSubmit: (formKey, value) {
  148. state.formData[formKey]!['focusNode'].unfocus();
  149. FocusScope.of(context).requestFocus(state.formData['room']!['focusNode']);
  150. },
  151. ).constrained(width: 83),
  152. MyTextView(
  153. "-",
  154. textColor: context.appColors.textBlack,
  155. marginLeft: 4,
  156. marginRight: 4,
  157. isFontMedium: true,
  158. ),
  159. // 表单 - 房号
  160. _buildInputLayout(
  161. context,
  162. state,
  163. "room",
  164. textInputAction: TextInputAction.done,
  165. onSubmit: (formKey, value) {
  166. state.formData[formKey]!['focusNode'].unfocus();
  167. },
  168. ).constrained(width: 83),
  169. ],
  170. ),
  171. ],
  172. ),
  173. ],
  174. ),
  175. MyTextView(
  176. key: _description1Key,
  177. S.current.block_desc,
  178. fontSize: 15,
  179. marginTop: 25,
  180. textAlign: TextAlign.center,
  181. isFontMedium: true,
  182. textColor: context.appColors.textBlack,
  183. ),
  184. MyTextView(
  185. key: _description2Key,
  186. S.current.block_example,
  187. fontSize: 15,
  188. marginTop: 20,
  189. textAlign: TextAlign.center,
  190. isFontMedium: true,
  191. textColor: context.appColors.textBlack,
  192. ),
  193. MyTextView(
  194. key: _description3Key,
  195. S.current.block_example_desc,
  196. fontSize: 15,
  197. marginTop: 20,
  198. textAlign: TextAlign.center,
  199. isFontMedium: true,
  200. textColor: context.appColors.textBlack,
  201. ),
  202. SizedBox(
  203. height: state.remainingSpace, // 使用剩余空间的值
  204. ),
  205. MyButton(
  206. key: _buttonKey,
  207. onPressed: viewModel.submitUnit,
  208. text: S.current.next,
  209. textColor: Colors.white,
  210. backgroundColor: context.appColors.btnBgDefault,
  211. fontWeight: FontWeight.w500,
  212. type: ClickType.throttle,
  213. fontSize: 16,
  214. minHeight: 50,
  215. radius: 5,
  216. ).marginOnly(top: 40, bottom: 30, left: 18, right: 18),
  217. ],
  218. ),
  219. ),
  220. ),
  221. );
  222. }
  223. /// 输入框
  224. Widget _buildInputLayout(
  225. BuildContext context,
  226. SelectUnitState state,
  227. String key, {
  228. double marginTop = 0,
  229. bool? showRightIcon = false, //是否展示右侧的布局
  230. Widget? rightWidget, //右侧的布局
  231. TextInputType textInputType = TextInputType.number,
  232. String? errorText,
  233. bool obscureText = false,
  234. TextInputAction textInputAction = TextInputAction.done,
  235. Function? onSubmit,
  236. }) {
  237. return IgnoreKeyboardDismiss(
  238. child: MyTextField(
  239. key,
  240. fillBackgroundColor: context.appColors.authFiledBG,
  241. state.formData[key]!['value'],
  242. hintText: state.formData[key]!['hintText'],
  243. hintStyle: TextStyle(
  244. color: context.appColors.authFiledHint,
  245. fontSize: 16.0,
  246. fontWeight: FontWeight.w500,
  247. ),
  248. controller: state.formData[key]!['controller'],
  249. focusNode: state.formData[key]!['focusNode'],
  250. margin: EdgeInsets.only(top: marginTop),
  251. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 3),
  252. showDivider: false,
  253. height: 44,
  254. style: TextStyle(
  255. color: context.appColors.authFiledText,
  256. fontSize: 16.0,
  257. fontWeight: FontWeight.w500,
  258. ),
  259. inputType: textInputType,
  260. textInputAction: textInputAction,
  261. onSubmit: onSubmit,
  262. cursorColor: context.appColors.authFiledText,
  263. obscureText: obscureText,
  264. errorText: errorText,
  265. showLeftIcon: true,
  266. showRightIcon: showRightIcon,
  267. rightWidget: rightWidget,
  268. ),
  269. );
  270. }
  271. }