select_estate_page.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import 'package:cpt_auth/modules/select_estate/select_estate_state.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:cs_resources/generated/l10n.dart';
  4. import 'package:cs_resources/theme/app_colors_theme.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:auto_route/auto_route.dart';
  7. import 'package:flutter_hooks/flutter_hooks.dart';
  8. import 'package:hooks_riverpod/hooks_riverpod.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:shared/utils/log_utils.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_load_image.dart';
  15. import 'package:widgets/my_text_field.dart';
  16. import 'package:widgets/my_text_view.dart';
  17. import 'package:widgets/widget_export.dart';
  18. import '../../router/page/auth_page_router.dart';
  19. import 'select_estate_view_model.dart';
  20. @RoutePage()
  21. class SelectEstatePage extends HookConsumerWidget {
  22. SelectEstatePage({Key? key}) : super(key: key);
  23. //启动当前页面
  24. static void startInstance({BuildContext? context}) {
  25. if (context != null) {
  26. context.router.push(SelectEstatePageRoute());
  27. } else {
  28. appRouter.push(SelectEstatePageRoute());
  29. }
  30. }
  31. // 为需要测量的控件创建 GlobalKey
  32. final GlobalKey _appbarKey = GlobalKey();
  33. final GlobalKey _topImageKey = GlobalKey();
  34. final GlobalKey _titleKey = GlobalKey();
  35. final GlobalKey _inputKey = GlobalKey();
  36. final GlobalKey _descriptionKey = GlobalKey();
  37. final GlobalKey _buttonKey = GlobalKey();
  38. @override
  39. Widget build(BuildContext context, WidgetRef ref) {
  40. final viewModel = ref.watch(selectEstateViewModelProvider.notifier);
  41. final state = ref.watch(selectEstateViewModelProvider);
  42. // 获取屏幕高度
  43. final screenHeight = MediaQuery.of(context).size.height;
  44. final statusBarHeight = MediaQuery.of(context).padding.top;
  45. final navigationBarHeight = MediaQuery.of(context).padding.bottom;
  46. useEffect(() {
  47. double usedHeight = 0;
  48. // 组件挂载时执行,获取控件高度
  49. WidgetsBinding.instance.addPostFrameCallback((_) {
  50. // 获取各个控件的高度
  51. usedHeight += _appbarKey.currentContext?.size?.height ?? 0;
  52. usedHeight += _topImageKey.currentContext?.size?.height ?? 0;
  53. usedHeight += _titleKey.currentContext?.size?.height ?? 0;
  54. usedHeight += _inputKey.currentContext?.size?.height ?? 0;
  55. usedHeight += _descriptionKey.currentContext?.size?.height ?? 0;
  56. usedHeight += _buttonKey.currentContext?.size?.height ?? 0;
  57. // 计算剩余空间
  58. double remainingSpace = screenHeight - statusBarHeight - navigationBarHeight - usedHeight - 38 - 28 - 20 - 20;
  59. Log.d("计算剩余空间:$remainingSpace");
  60. if (remainingSpace > 0) {
  61. // 设置一个状态来存储剩余空间的高度
  62. viewModel.setRemainingSpace(remainingSpace);
  63. }
  64. });
  65. return () {
  66. // 组件卸载时执行
  67. };
  68. }, []);
  69. return Scaffold(
  70. appBar: MyAppBar.appBar(context, S.current.yy_home_accounts, key: _appbarKey),
  71. backgroundColor: context.appColors.backgroundDefault,
  72. body: SingleChildScrollView(
  73. scrollDirection: Axis.vertical,
  74. physics: const BouncingScrollPhysics(),
  75. child: Container(
  76. padding: const EdgeInsets.symmetric(horizontal: 38),
  77. width: double.infinity,
  78. child: Column(
  79. mainAxisSize: MainAxisSize.max,
  80. crossAxisAlignment: CrossAxisAlignment.center,
  81. children: [
  82. //顶部图片
  83. MyAssetImage(
  84. key: _topImageKey,
  85. Assets.authChooseEstateBuilding,
  86. width: 267,
  87. height: 158,
  88. ).marginOnly(top: 28, bottom: 38),
  89. MyTextView(
  90. key: _titleKey,
  91. S.current.estate_or_building_name,
  92. fontSize: 23,
  93. marginBottom: 20,
  94. textAlign: TextAlign.center,
  95. isFontMedium: true,
  96. textColor: context.appColors.textBlack,
  97. ),
  98. //输入资产的名称
  99. _buildInputLayout(
  100. context,
  101. state,
  102. "estate",
  103. key: _inputKey,
  104. textInputAction: TextInputAction.done,
  105. onSubmit: (formKey, value) {
  106. state.formData[formKey]!['focusNode'].unfocus();
  107. },
  108. ),
  109. MyTextView(
  110. key: _descriptionKey,
  111. S.current.estate_name_desc,
  112. fontSize: 15,
  113. marginTop: 19,
  114. isFontMedium: true,
  115. textColor: context.appColors.textBlack,
  116. ),
  117. SizedBox(
  118. height: state.remainingSpace, // 使用剩余空间的值
  119. ),
  120. MyButton(
  121. key: _buttonKey,
  122. onPressed: viewModel.submitEstate,
  123. text: S.current.next,
  124. textColor: Colors.white,
  125. backgroundColor: context.appColors.btnBgDefault,
  126. fontWeight: FontWeight.w500,
  127. type: ClickType.throttle,
  128. fontSize: 16,
  129. minHeight: 50,
  130. radius: 5,
  131. ).marginOnly(top: 48, bottom: 32),
  132. ],
  133. ),
  134. ),
  135. ),
  136. );
  137. }
  138. /// 输入框
  139. Widget _buildInputLayout(
  140. BuildContext context,
  141. SelectEstateState state,
  142. String formKey, {
  143. Key? key,
  144. double marginTop = 0,
  145. bool? showRightIcon = false, //是否展示右侧的布局
  146. Widget? rightWidget, //右侧的布局
  147. TextInputType textInputType = TextInputType.text,
  148. String? errorText,
  149. bool obscureText = false,
  150. TextInputAction textInputAction = TextInputAction.done,
  151. Function? onSubmit,
  152. }) {
  153. return IgnoreKeyboardDismiss(
  154. child: MyTextField(
  155. formKey,
  156. key: key,
  157. fillBackgroundColor: context.appColors.authFiledBG,
  158. state.formData[formKey]!['value'],
  159. hintText: state.formData[formKey]!['hintText'],
  160. hintStyle: TextStyle(
  161. color: context.appColors.authFiledHint,
  162. fontSize: 16.0,
  163. fontWeight: FontWeight.w500,
  164. ),
  165. controller: state.formData[formKey]!['controller'],
  166. focusNode: state.formData[formKey]!['focusNode'],
  167. margin: EdgeInsets.only(top: marginTop),
  168. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 3),
  169. showDivider: false,
  170. height: 44,
  171. style: TextStyle(
  172. color: context.appColors.authFiledText,
  173. fontSize: 16.0,
  174. fontWeight: FontWeight.w500,
  175. ),
  176. inputType: textInputType,
  177. textInputAction: textInputAction,
  178. onSubmit: onSubmit,
  179. cursorColor: context.appColors.authFiledText,
  180. obscureText: obscureText,
  181. errorText: errorText,
  182. showLeftIcon: true,
  183. showRightIcon: showRightIcon,
  184. rightWidget: rightWidget,
  185. ),
  186. );
  187. }
  188. }