duration_setting_page.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import 'dart:ui';
  2. import 'package:dartin/dartin.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  6. import 'package:sport/services/api/rest_client.dart';
  7. import 'package:sport/utils/toast.dart';
  8. import 'package:sport/widgets/appbar.dart';
  9. import 'package:sport/widgets/button_primary.dart';
  10. import 'package:sport/widgets/dialog/alert_dialog.dart';
  11. import 'package:sport/widgets/dialog/request_dialog.dart';
  12. import 'package:sport/widgets/misc.dart';
  13. class DurationSettingPage extends StatefulWidget {
  14. @override
  15. State<StatefulWidget> createState() => _PageState();
  16. }
  17. class _PageState extends State<DurationSettingPage> with SingleTickerProviderStateMixin {
  18. TabController _controller;
  19. @override
  20. void initState() {
  21. super.initState();
  22. _controller = new TabController(length: 2, vsync: this)..addListener(() {
  23. setState(() {
  24. });
  25. });
  26. }
  27. @override
  28. void dispose() {
  29. super.dispose();
  30. _controller?.dispose();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. backgroundColor: Colors.white,
  36. appBar: buildAppBar(context, title: "设定目标"),
  37. body: Padding(
  38. padding: const EdgeInsets.all(12.0),
  39. child: Column(
  40. children: <Widget>[
  41. Center(
  42. child: TabBar(
  43. controller: _controller,
  44. isScrollable: true,
  45. indicatorPadding: EdgeInsets.symmetric(horizontal: 10.0),
  46. indicatorWeight: 3,
  47. unselectedLabelColor: Color(0xff999999),
  48. labelStyle: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600),
  49. unselectedLabelStyle: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600),
  50. labelPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 30.0),
  51. // indicator: const BoxDecoration(),
  52. // labelPadding: EdgeInsets.all(20.0),
  53. tabs: <Widget>[
  54. Tab(
  55. icon: Image.asset("lib/assets/img/setgoals_icon_duration_${_controller.index == 0 ? "press" : "normal"}.png"),
  56. text: "时长",
  57. iconMargin: EdgeInsets.only(bottom: 3.0),
  58. ),
  59. Tab(
  60. icon: Image.asset("lib/assets/img/setgoals_icon_consume_${_controller.index == 1 ? "press" : "normal"}.png"),
  61. text: "卡路里",
  62. iconMargin: EdgeInsets.only(bottom: 3.0),
  63. )
  64. ],
  65. ),
  66. ),
  67. Divider(),
  68. Expanded(
  69. child: Container(
  70. padding: const EdgeInsets.only(top: 30.0),
  71. child: TabBarView(
  72. controller: _controller,
  73. physics: NeverScrollableScrollPhysics(),
  74. children: <Widget>[
  75. _Form(type: 0, unit: "分钟", items: [20, 40, 60, 80, 100, 120]),
  76. _Form(type: 1, unit: "卡", items: [100, 150, 200, 250, 300, 350]),
  77. ],
  78. ),
  79. ),
  80. )
  81. ],
  82. ),
  83. ),
  84. );
  85. }
  86. }
  87. class _Form extends StatefulWidget {
  88. final int type;
  89. final String unit;
  90. final List<int> items;
  91. const _Form({Key key, this.type, this.unit, this.items}) : super(key: key);
  92. @override
  93. State<StatefulWidget> createState() => _FormState();
  94. }
  95. class _FormState extends State<_Form> {
  96. ValueNotifier<int> _valueTotal = ValueNotifier(0);
  97. ValueNotifier<int> _valueIndex = ValueNotifier(0);
  98. TextEditingController _controller;
  99. @override
  100. void initState() {
  101. super.initState();
  102. _controller = TextEditingController();
  103. if (widget.type == 0) {
  104. _valueTotal.value = 20;
  105. } else {
  106. _valueTotal.value = 100;
  107. }
  108. }
  109. @override
  110. void dispose() {
  111. super.dispose();
  112. _valueIndex?.dispose();
  113. }
  114. @override
  115. Widget build(BuildContext context) {
  116. return Column(
  117. children: <Widget>[
  118. Row(
  119. mainAxisSize: MainAxisSize.min,
  120. crossAxisAlignment: CrossAxisAlignment.end,
  121. children: <Widget>[
  122. ValueListenableBuilder(
  123. valueListenable: _valueTotal,
  124. builder: (_, value, ___) => Text(
  125. "$value",
  126. style: Theme.of(context).textTheme.headline1.copyWith(fontSize: 40.0),
  127. ),
  128. ),
  129. Padding(
  130. padding: const EdgeInsets.all(6.0),
  131. child: Text(
  132. widget.unit,
  133. style: Theme.of(context).textTheme.subtitle2,
  134. ),
  135. ),
  136. ],
  137. ),
  138. GestureDetector(
  139. onTap: () async {
  140. if (await showDialog(
  141. context: context,
  142. builder: (context) => CustomAlertDialog(
  143. child: Padding(
  144. padding: const EdgeInsets.fromLTRB(16.0, 32.0, 16.0, 0),
  145. child: TextField(
  146. textAlign: TextAlign.center,
  147. controller: _controller,
  148. keyboardType: TextInputType.number,
  149. autofocus: true,
  150. maxLength: 4,
  151. inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
  152. decoration: InputDecoration(
  153. counterText: '',
  154. helperText: '',
  155. helperMaxLines: 1,
  156. focusedBorder: UnderlineInputBorder(
  157. borderSide: BorderSide(color: Theme.of(context).accentColor),
  158. ))),
  159. ),
  160. ok: () => Navigator.of(context).pop(true)),
  161. ) ==
  162. true) {
  163. var val = _controller.text.trim();
  164. if (val.isEmpty == true) return;
  165. _valueTotal.value = int.parse(val);
  166. _valueIndex.value = -1;
  167. }
  168. },
  169. child: Container(
  170. width: 80.0,
  171. height: 30.0,
  172. margin: const EdgeInsets.all(8.0),
  173. decoration: BoxDecoration(
  174. borderRadius: BorderRadius.circular(20),
  175. border: Border.all(
  176. color: Theme.of(context).accentColor,
  177. width: .5,
  178. ),
  179. ),
  180. child: Center(child: Text("自定义", style: Theme.of(context).textTheme.subtitle1.copyWith(color: Theme.of(context).accentColor), strutStyle: fixedLine,)),
  181. ),
  182. ),
  183. const SizedBox(
  184. height: 15.0,
  185. ),
  186. Padding(
  187. padding: const EdgeInsets.symmetric(vertical: 20.0),
  188. child: ValueListenableBuilder(
  189. valueListenable: _valueIndex,
  190. builder: (_, value, __) => StaggeredGridView.countBuilder(
  191. padding: EdgeInsets.zero,
  192. shrinkWrap: true,
  193. physics: NeverScrollableScrollPhysics(),
  194. crossAxisCount: 3,
  195. itemCount: widget.items.length,
  196. itemBuilder: (BuildContext context, int index) => GestureDetector(
  197. onTap: () {
  198. _valueIndex.value = index;
  199. _valueTotal.value = widget.items[index];
  200. },
  201. child: Center(
  202. child: Container(
  203. height: 66.0,
  204. decoration: BoxDecoration(
  205. image:
  206. value == index ? DecorationImage(image: AssetImage("lib/assets/img/control_img_selected.png"), alignment: Alignment.bottomRight) : null,
  207. borderRadius: BorderRadius.circular(10),
  208. border: Border.all(
  209. color: value == index ? Theme.of(context).accentColor : const Color(0xffCECECE),
  210. width: .5,
  211. ),
  212. ),
  213. child: Center(
  214. child: Text("${widget.items[index]}${widget.unit}",
  215. style: value == index
  216. ? Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 16.0, color: Theme.of(context).accentColor)
  217. : Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 16.0))),
  218. )),
  219. ),
  220. crossAxisSpacing: 12.0,
  221. mainAxisSpacing: 12.0,
  222. staggeredTileBuilder: (int index) => StaggeredTile.fit(1),
  223. ),
  224. ),
  225. ),
  226. PrimaryButton(
  227. height: 44.0,
  228. content: "确定",
  229. callback: () async {
  230. await request(context, () async {
  231. if (widget.type == 0) {
  232. await inject<RestClient>().setSportTargetToday("duration", durationMinute: _valueTotal.value);
  233. } else {
  234. await inject<RestClient>().setSportTargetToday("consume", consume: _valueTotal.value);
  235. }
  236. ToastUtil.show("提交信息成功");
  237. });
  238. Navigator.pop(context, true);
  239. },
  240. )
  241. ],
  242. );
  243. }
  244. }