setting_healthkit_page.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:permission_handler/permission_handler.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'package:sport/services/app_lifecycle_state.dart';
  6. import 'package:sport/widgets/appbar.dart';
  7. import 'package:sport/widgets/button_primary.dart';
  8. import 'package:sport/widgets/game_run.dart';
  9. class SettingHealthKitPage extends StatefulWidget {
  10. @override
  11. State<StatefulWidget> createState() => _PageState();
  12. }
  13. class _PageState extends LifecycleState<SettingHealthKitPage> {
  14. bool _connected = false;
  15. @override
  16. void initState() {
  17. super.initState();
  18. SharedPreferences.getInstance().then((value) {
  19. setState(() {
  20. _connected = value.containsKey("HEALTH_KIT_CONNECTED");
  21. });
  22. });
  23. }
  24. @override
  25. void dispose() {
  26. super.dispose();
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. backgroundColor: Colors.white,
  32. appBar: buildAppBar(context, title: "连接苹果健康"),
  33. body: SingleChildScrollView(
  34. child: Padding(
  35. padding: const EdgeInsets.all(20.0),
  36. child: Column(
  37. children: [
  38. Padding(
  39. padding: const EdgeInsets.symmetric(vertical: 58.0),
  40. child: Center(
  41. child: Row(
  42. mainAxisSize: MainAxisSize.min,
  43. children: [
  44. Image.asset(
  45. "lib/assets/img/logo_img.png",
  46. width: 60.0,
  47. ),
  48. Container(
  49. width: 6,
  50. height: 6,
  51. decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).accentColor),
  52. margin: EdgeInsets.only(left: 12.0),
  53. ),
  54. Container(
  55. width: 6,
  56. height: 6,
  57. decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).scaffoldBackgroundColor),
  58. margin: EdgeInsets.symmetric(horizontal: 12.0),
  59. ),
  60. Container(
  61. width: 6,
  62. height: 6,
  63. decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).scaffoldBackgroundColor),
  64. margin: EdgeInsets.only(right: 12.0),
  65. ),
  66. Image.asset(
  67. "lib/assets/img/icon_ioshealthy.png",
  68. width: 60.0,
  69. ),
  70. ],
  71. ),
  72. ),
  73. ),
  74. Text(
  75. "连接【健康】后,“趣动”会同步并更新你的活动能量和体能训练数据到苹果健康中心。你也可以进入【健康】的数据来源中,选择并修改“趣动”更新的健康数据类型",
  76. style: Theme.of(context).textTheme.subtitle1,
  77. ),
  78. SizedBox(
  79. height: 58.0,
  80. ),
  81. if (_connected)
  82. Padding(
  83. padding: const EdgeInsets.symmetric(vertical: 12.0),
  84. child: Text(
  85. "您已连接过【健康】,可以【健康】去查看设置",
  86. ),
  87. ),
  88. _connected
  89. ? PrimaryButton(
  90. content: "查看设置",
  91. callback: () async {
  92. openAppSettings();
  93. },
  94. )
  95. : PrimaryButton(
  96. content: "前往连接",
  97. callback: () async {
  98. if (Platform.isIOS) {
  99. await connectHealthKit();
  100. SharedPreferences p = await SharedPreferences.getInstance();
  101. await p.setBool("HEALTH_KIT_CONNECTED", true);
  102. Navigator.pop(context);
  103. }
  104. },
  105. ),
  106. SizedBox(
  107. height: 24.0,
  108. ),
  109. ],
  110. ),
  111. ),
  112. ),
  113. );
  114. }
  115. }