BasicInfoRecordingPage.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_habit/common/BaseArchitectural.dart';
  3. import 'package:flutter_habit/common/I18N.dart';
  4. import 'package:flutter_habit/common/components/PopMenus.dart';
  5. import 'package:flutter_habit/common/provider/DataProvider.dart';
  6. import 'package:flutter_habit/common/provider/UserProvider.dart';
  7. import 'package:flutter_habit/common/utils/ConvertUtils.dart';
  8. import 'package:flutter_habit/database/entity/BasicInfo.dart';
  9. import 'package:flutter_habit/database/mapper/BasicInfoMapper.dart';
  10. import 'package:flutter_habit/network/Repository.dart';
  11. import 'package:flutter_habit/view/record/AdjustButtonRow.dart';
  12. import 'package:provider/provider.dart';
  13. class BasicInfoRecordingPage extends StatelessWidget {
  14. @override
  15. Widget build(BuildContext context) {
  16. return MultiProvider(
  17. providers: [
  18. ChangeNotifierProvider<BasicInfoRecordingPageService>(
  19. create: (_) => BasicInfoRecordingPageService(context)),
  20. ChangeNotifierProvider<BasicInfoRecordingPageModel>(
  21. create: (_) => BasicInfoRecordingPageModel(context)),
  22. ],
  23. child: _BasicInfoRecordingPageView(),
  24. );
  25. }
  26. }
  27. // model
  28. class BasicInfoRecordingPageModel extends BaseModel {
  29. BasicInfoRecordingPageModel(BuildContext context) : super(context);
  30. double height;
  31. double weight;
  32. double breastLine;
  33. double waistLine;
  34. double hipLine;
  35. @override
  36. void init(BuildContext context) {
  37. super.init(context);
  38. DataProvider dataProvider =
  39. Provider.of<DataProvider>(context, listen: false);
  40. height = dataProvider.height ?? 165;
  41. weight = dataProvider.weight ?? 55;
  42. breastLine = dataProvider.breastLine ?? 86;
  43. waistLine = dataProvider.waistLine ?? 66;
  44. hipLine = dataProvider.hipLine ?? 90;
  45. }
  46. }
  47. // service
  48. class BasicInfoRecordingPageService extends BaseService {
  49. BasicInfoRecordingPageService(BuildContext context) : super(context);
  50. Future<void> record(BuildContext context) async {
  51. BasicInfoRecordingPageModel model =
  52. Provider.of<BasicInfoRecordingPageModel>(context, listen: false);
  53. // 今日是否记录过
  54. DateTime now = DateTime.now();
  55. List<BasicInfo> list = await BasicInfoMapper().selectWhere(
  56. "date >= ${ConvertUtils.dateOfDateTime(now).millisecondsSinceEpoch}");
  57. BasicInfo basicInfo = BasicInfo();
  58. basicInfo.setHeight(model.height);
  59. basicInfo.setWeight(model.weight);
  60. basicInfo.setBreastLine(model.breastLine);
  61. basicInfo.setWaistLine(model.waistLine);
  62. basicInfo.setHipLine(model.hipLine);
  63. basicInfo.setDate(now.millisecondsSinceEpoch);
  64. if (list.isNotEmpty) {
  65. await PopMenus.sliderConfirm(
  66. context: context,
  67. content: Text(I18N.of("滑动来覆盖今日数据")),
  68. function: () async {
  69. basicInfo.setId(list.last.getId());
  70. await BasicInfoMapper().updateByFirstKeySelective(basicInfo);
  71. await PopMenus.attention(
  72. context: context, content: Text(I18N.of("记录成功")));
  73. await Provider.of<DataProvider>(context, listen: false)
  74. .loadBasicInfoData();
  75. Navigator.of(context).pop();
  76. },
  77. );
  78. } else {
  79. await BasicInfoMapper().insert(basicInfo);
  80. await PopMenus.attention(
  81. context: context, content: Text(I18N.of("记录成功")));
  82. await Provider.of<DataProvider>(context, listen: false)
  83. .loadBasicInfoData();
  84. UserProvider userProvider =
  85. Provider.of<UserProvider>(context, listen: false);
  86. if (userProvider.token != null) {
  87. // 增加金币
  88. int increasedCoin = await Repository.getInstance()
  89. .increaseCoin(context, userProvider.uid, userProvider.token);
  90. if (increasedCoin != null) {
  91. await PopMenus.coinAdd(context: context, addedCoins: increasedCoin);
  92. userProvider.coins += increasedCoin;
  93. userProvider.refresh();
  94. }
  95. }
  96. Navigator.of(context).pop();
  97. }
  98. }
  99. }
  100. // view
  101. class _BasicInfoRecordingPageView extends StatelessWidget {
  102. @override
  103. Widget build(BuildContext context) {
  104. BasicInfoRecordingPageService service =
  105. Provider.of<BasicInfoRecordingPageService>(context, listen: false);
  106. BasicInfoRecordingPageModel model =
  107. Provider.of<BasicInfoRecordingPageModel>(context, listen: true);
  108. BasicInfoRecordingPageModel modelListenFalse =
  109. Provider.of<BasicInfoRecordingPageModel>(context, listen: true);
  110. return Scaffold(
  111. appBar: AppBar(
  112. title: Text(I18N.of("基本信息记录")),
  113. ),
  114. body: Padding(
  115. padding: EdgeInsets.only(left: 16, right: 16),
  116. child: ListView(
  117. children: <Widget>[
  118. // TextButton(
  119. // child: Text("测试数据"),
  120. // onPressed: () async {
  121. // await _test(context);
  122. // },
  123. // ),
  124. SlideAdjuster(
  125. titleString: "${I18N.of("身高")} (cm)",
  126. startValue: modelListenFalse.height,
  127. onValueChange: (v) => modelListenFalse.height = v,
  128. ),
  129. SlideAdjuster(
  130. titleString: "${I18N.of("体重")} (kg)",
  131. startValue: modelListenFalse.weight,
  132. onValueChange: (v) => modelListenFalse.weight = v,
  133. ),
  134. SlideAdjuster(
  135. titleString: "${I18N.of("胸围")} (cm)",
  136. startValue: modelListenFalse.breastLine,
  137. onValueChange: (v) => modelListenFalse.breastLine = v,
  138. ),
  139. SlideAdjuster(
  140. titleString: "${I18N.of("腰围")} (cm)",
  141. startValue: modelListenFalse.waistLine,
  142. onValueChange: (v) => modelListenFalse.waistLine = v,
  143. ),
  144. SlideAdjuster(
  145. titleString: "${I18N.of("臀围")} (cm)",
  146. startValue: modelListenFalse.hipLine,
  147. onValueChange: (v) => modelListenFalse.hipLine = v,
  148. ),
  149. Padding(
  150. padding: EdgeInsets.all(16),
  151. child: ElevatedButton(
  152. child: Text(I18N.of("记录")),
  153. style: ButtonStyle(
  154. backgroundColor: MaterialStateProperty.all(
  155. Theme.of(context).colorScheme.secondary),
  156. foregroundColor:
  157. MaterialStateProperty.all(Theme.of(context).cardColor)),
  158. onPressed: () => service.record(context),
  159. ),
  160. )
  161. ],
  162. ),
  163. ),
  164. );
  165. }
  166. }