import 'dart:io'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sport/services/app_lifecycle_state.dart'; import 'package:sport/widgets/appbar.dart'; import 'package:sport/widgets/button_primary.dart'; import 'package:sport/widgets/game_run.dart'; class SettingHealthKitPage extends StatefulWidget { @override State createState() => _PageState(); } class _PageState extends LifecycleState { bool _connected = false; @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) { setState(() { _connected = value.containsKey("HEALTH_KIT_CONNECTED"); }); }); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: buildAppBar(context, title: "连接苹果健康"), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ Padding( padding: const EdgeInsets.symmetric(vertical: 58.0), child: Center( child: Row( mainAxisSize: MainAxisSize.min, children: [ Image.asset( "lib/assets/img/logo_img.png", width: 60.0, ), Container( width: 6, height: 6, decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).accentColor), margin: EdgeInsets.only(left: 12.0), ), Container( width: 6, height: 6, decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).scaffoldBackgroundColor), margin: EdgeInsets.symmetric(horizontal: 12.0), ), Container( width: 6, height: 6, decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).scaffoldBackgroundColor), margin: EdgeInsets.only(right: 12.0), ), Image.asset( "lib/assets/img/icon_ioshealthy.png", width: 60.0, ), ], ), ), ), Text( "连接【健康】后,“趣动”会同步并更新你的活动能量和体能训练数据到苹果健康中心。你也可以进入【健康】的数据来源中,选择并修改“趣动”更新的健康数据类型", style: Theme.of(context).textTheme.subtitle1, ), SizedBox( height: 58.0, ), if (_connected) Padding( padding: const EdgeInsets.symmetric(vertical: 12.0), child: Text( "您已连接过【健康】,可以【健康】去查看设置", ), ), _connected ? PrimaryButton( content: "查看设置", callback: () async { openAppSettings(); }, ) : PrimaryButton( content: "前往连接", callback: () async { if (Platform.isIOS) { await connectHealthKit(); SharedPreferences p = await SharedPreferences.getInstance(); await p.setBool("HEALTH_KIT_CONNECTED", true); Navigator.pop(context); } }, ), SizedBox( height: 24.0, ), ], ), ), ), ); } }