home_save_user_info_view.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import 'package:flutter/material.dart';
  2. import 'package:trackoffical_app/generated/assets.dart';
  3. import 'package:trackoffical_app/model.dart';
  4. import 'package:trackoffical_app/route.dart';
  5. import 'package:trackoffical_app/screen.dart';
  6. import 'package:trackoffical_app/service/api.dart';
  7. import 'package:trackoffical_app/service/app.dart';
  8. import 'package:trackoffical_app/widget/app_top_bar.dart';
  9. import 'package:trackoffical_app/widget/ruler_picker.dart';
  10. import 'package:get/get.dart';
  11. import 'package:trackoffical_app/service/mock.dart';
  12. import '../../styles/theme.dart';
  13. import '../../utils.dart';
  14. class HomeSaveUserInfoController extends GetxController {
  15. final weightKg = 45.0.obs;
  16. final heightCm = 150.obs;
  17. final sex = Sex.Male.obs;
  18. final rhr = 60.obs;
  19. final age = 12.obs;
  20. onSave(){
  21. final userProfile = App.to.userProfile;
  22. userProfile.age.val = age.value;
  23. userProfile.sex = sex.value;
  24. userProfile.weightKg.val = weightKg.value;
  25. userProfile.heightCm.val = heightCm.value.toDouble();
  26. userProfile.rhr.val = rhr.value;
  27. tryCatchApi(()async{
  28. await ApiService.to.saveUserInfo();
  29. Get.offAllNamed(RouteName.home);
  30. });
  31. }
  32. }
  33. class HomeSaveUserInfoView extends GetView<HomeSaveUserInfoController> {
  34. const HomeSaveUserInfoView({super.key});
  35. static Bindings bindings() {
  36. return BindingsBuilder(() {
  37. Get.put(HomeSaveUserInfoController());
  38. });
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return Scaffold(
  43. body: Stack(
  44. alignment: Alignment.topCenter,
  45. children: [
  46. Container(
  47. height: context.height,
  48. ),
  49. Image.asset(Assets.imagesBkLogin, width: context.width, fit: BoxFit.fitWidth),
  50. Positioned(
  51. top: MediaQuery.of(context).padding.top + 6.0.wp,
  52. child: Text('下列身体数据计算运动消耗', style: TextStyle(color: Colors.white, fontSize: 6.6.wp),)),
  53. Positioned(
  54. top: MediaQuery.of(context).padding.top + 24.0.wp,
  55. left: 0,
  56. child: Container(
  57. height: context.height - MediaQuery.of(context).padding.top - 24.0.wp,
  58. width: context.width,
  59. decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(
  60. topLeft: Radius.circular(3.3.wp),
  61. topRight: Radius.circular(3.3.wp),
  62. )),
  63. child: _wContent(context)
  64. ) )
  65. ],
  66. ),
  67. floatingActionButton: FloatingActionButton(
  68. backgroundColor: context.theme.colorScheme.primary,
  69. foregroundColor: Colors.white,
  70. onPressed: controller.onSave,
  71. child: const Text('确定')),
  72. );
  73. }
  74. Widget _wContent(BuildContext context){
  75. const color = Color(0xffff870d);
  76. return ListView(children: [
  77. _listTitle(context, '您的性别是?',),
  78. Row(
  79. children: Sex.values
  80. .map((sex) => Obx(() => Expanded(
  81. child: ListTile(
  82. title: Text(sex.display()),
  83. leading: Radio<Sex>(
  84. value: sex,
  85. groupValue: controller.sex.value,
  86. onChanged: (value) {
  87. controller.sex.value = value!;
  88. },
  89. )))))
  90. .toList()),
  91. Obx(() => _EditValue(
  92. title: '您的身高是?',
  93. value: controller.heightCm.value.toString(),
  94. unit: 'CM',
  95. )),
  96. RulerPicker(
  97. beginValue: 80,
  98. endValue: 300,
  99. initValue: controller.heightCm.value,
  100. onValueChange: (value) {
  101. controller.heightCm.value = value;
  102. },
  103. ),
  104. Obx(() => _EditValue(
  105. title: '您的体重是?',
  106. value: controller.weightKg.value.toStringAsFixed(1),
  107. unit: 'KG',
  108. )),
  109. RulerPicker(
  110. beginValue: 250,
  111. endValue: 4000,
  112. initValue: (controller.weightKg.value * 10).toInt(),
  113. onBuildRulerScaleText: (index, scaleValue) {
  114. return (scaleValue ~/ 10).toString();
  115. },
  116. onValueChange: (value) {
  117. controller.weightKg.value = value.toDouble() / 10;
  118. },
  119. ),
  120. Obx(() => _EditValue(
  121. title: '您的年龄是?',
  122. value: controller.age.value.toString(),
  123. unit: '岁',
  124. )),
  125. RulerPicker(
  126. beginValue: 4,
  127. endValue: 150,
  128. initValue: controller.age.value,
  129. onValueChange: (value) {
  130. controller.age.value = value;
  131. },
  132. ),
  133. Obx(() => _EditValue(
  134. title: '您的静息心率(RHR)是?',
  135. value: controller.rhr.value.toString(),
  136. unit: '次/分钟',
  137. )),
  138. RulerPicker(
  139. beginValue: 30,
  140. endValue: 180,
  141. initValue: controller.rhr.value,
  142. onValueChange: (value) {
  143. controller.rhr.value = value;
  144. },
  145. ),
  146. ]);
  147. }
  148. }
  149. Widget _listTitle(BuildContext context, String title, {Widget? end}){
  150. return ListTile(leading: Icon(Icons.arrow_right, color: context.theme.colorScheme.primary),
  151. title: Text(title, style: TextStyle(color: Colors.black, fontSize: 4.4.wp),), trailing: end);
  152. }
  153. class _EditValue extends StatelessWidget {
  154. const _EditValue({
  155. required this.title,
  156. required this.value,
  157. required this.unit,
  158. });
  159. final String title;
  160. final String value;
  161. final String unit;
  162. @override
  163. Widget build(BuildContext context) {
  164. final primary = context.theme.colorScheme.primary;
  165. return Column(
  166. crossAxisAlignment: CrossAxisAlignment.center,
  167. children: [
  168. Padding(
  169. padding: EdgeInsets.only(top: 0, bottom:0),
  170. child: _listTitle(context, title)),
  171. RichText(
  172. text: TextSpan(
  173. text: value,
  174. style: context.textTheme.titleLarge
  175. ?.copyWith(fontSize: 4.4.wp, color: primary),
  176. children: [
  177. TextSpan(text: unit, style: TextStyle(fontSize: 3.3.wp))
  178. ]),
  179. ),
  180. ],
  181. );
  182. }
  183. }
  184. void main() async {
  185. Mock.initServices();
  186. var c = HomeSaveUserInfoController();
  187. Get.put(c);
  188. runPreview(const HomeSaveUserInfoView());
  189. }