sport_reference_page.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'package:sport/application.dart';
  4. import 'package:sport/bean/sport_detail.dart';
  5. import 'package:sport/utils/sport_utils.dart';
  6. import 'package:sport/widgets/button_cancel.dart';
  7. import 'package:sport/widgets/button_primary.dart';
  8. class SportReferencePage extends StatefulWidget {
  9. final RecordsTodaySum sum;
  10. const SportReferencePage({Key? key, required this.sum}) : super(key: key);
  11. @override
  12. State<StatefulWidget> createState() => _PageState();
  13. }
  14. class _PageState extends State<SportReferencePage> {
  15. int _index = 0;
  16. int _consume = 0;
  17. @override
  18. void initState() {
  19. super.initState();
  20. _consume = widget.sum.consume;
  21. SharedPreferences.getInstance().then((value) {
  22. setState(() {
  23. _index = value.getInt("sport_type") ?? 0;
  24. });
  25. });
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Dialog(
  30. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
  31. child: FutureBuilder<SharedPreferences>(
  32. future: SharedPreferences.getInstance(),
  33. builder: (BuildContext context, AsyncSnapshot<SharedPreferences> snapshot) => Column(
  34. mainAxisSize: MainAxisSize.min,
  35. children: [
  36. Container(
  37. width: double.infinity,
  38. margin: EdgeInsets.all(16.0),
  39. padding: EdgeInsets.only(top: 14),
  40. child: Center(
  41. child: Text(
  42. "消耗$_consume大卡,相当于",
  43. style: Theme.of(context).textTheme.headline1,
  44. ),
  45. ),
  46. ),
  47. ListView(
  48. padding: EdgeInsets.symmetric(horizontal: 0, vertical: 0),
  49. physics: NeverScrollableScrollPhysics(),
  50. shrinkWrap: true,
  51. children: SPORT_TYPE
  52. .map(
  53. (e) => InkWell(
  54. onTap: () async {
  55. setState(() {
  56. _index = SPORT_TYPE.indexOf(e);
  57. });
  58. },
  59. child: Container(
  60. decoration: BoxDecoration(border: Border.all(color: Color(0xffF3F3F3), width: 1), borderRadius: BorderRadius.circular(10.0)),
  61. padding: const EdgeInsets.symmetric(horizontal: 11, vertical: 10),
  62. margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
  63. child: Row(
  64. children: [
  65. Image.asset(
  66. "lib/assets/img/${e['i']}.png",
  67. height: 24,
  68. ),
  69. SizedBox(
  70. width: 10,
  71. ),
  72. Text(
  73. "${e['name']}运动",
  74. style: Theme.of(context).textTheme.subtitle1,
  75. ),
  76. Text(
  77. SPORT_TYPE.indexOf(e) == 0
  78. ? " ${SportUtils.calSportTypeDataStr(SPORT_TYPE.indexOf(e), SportUtils.calSportTypeData(SPORT_TYPE.indexOf(e), widget.sum.consume - widget.sum.consume_jog, weight) + widget.sum.distance_jog / 1000.0)} "
  79. : " ${SportUtils.calSportTypeDataStr(SPORT_TYPE.indexOf(e), SportUtils.calSportTypeData(SPORT_TYPE.indexOf(e), widget.sum.consume, weight))} ",
  80. style: Theme.of(context).textTheme.subtitle1,
  81. ),
  82. Text(
  83. "${e['unit']}",
  84. style: Theme.of(context).textTheme.subtitle1,
  85. ),
  86. Expanded(child: Container()),
  87. (SPORT_TYPE.indexOf(e) == _index)
  88. ? Image.asset(
  89. "lib/assets/img/pop_icon_connected.png",
  90. height: 20,
  91. )
  92. : Image.asset(
  93. "lib/assets/img/pop_icon_choose_normal.png",
  94. height: 20,
  95. ),
  96. ],
  97. ),
  98. )),
  99. )
  100. .toList(),
  101. ),
  102. Padding(
  103. padding: const EdgeInsets.only(
  104. bottom: 20,
  105. left: 12,
  106. right: 12,
  107. ),
  108. child: Row(
  109. children: [
  110. Expanded(
  111. child: Padding(
  112. padding: const EdgeInsets.all(8.0),
  113. child: CancelButton(
  114. content: "取消",
  115. callback: () {
  116. Navigator.maybePop(context);
  117. },
  118. ),
  119. )),
  120. Expanded(
  121. child: Padding(
  122. padding: const EdgeInsets.all(8.0),
  123. child: PrimaryButton(
  124. content: "确认",
  125. callback: () async {
  126. SharedPreferences prefs = await SharedPreferences.getInstance();
  127. await prefs.setInt("sport_type", _index);
  128. Navigator.maybePop(context, true);
  129. },
  130. ),
  131. )),
  132. ],
  133. ),
  134. ),
  135. ],
  136. ),
  137. ),
  138. );
  139. }
  140. }