123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:health_kit_reporter/health_kit_reporter.dart';
- import 'package:health_kit_reporter/model/type/quantity_type.dart';
- import 'package:health_kit_reporter/model/type/workout_type.dart';
- import 'package:permission_handler/permission_handler.dart';
- import 'package:provider/provider.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:sport/pages/home/step_realtime_page.dart';
- import 'package:sport/pages/run/run_start.dart';
- import 'package:sport/pages/run/setting_healthkit_page.dart';
- import 'package:sport/pages/run/setting_permission_page.dart';
- import 'package:sport/provider/user_model.dart';
- import 'package:sport/router/navigator_util.dart';
- import 'package:sport/utils/toast.dart';
- import 'package:sport/widgets/button_primary.dart';
- import 'package:sport/widgets/dialog/alert_dialog.dart';
- connectHealthKit() async {
- try {
- final readTypes = <String>[
- QuantityType.stepCount.identifier,
- QuantityType.distanceWalkingRunning.identifier,
- ];
- final writeTypes = <String>[
- QuantityType.activeEnergyBurned.identifier,
- QuantityType.distanceWalkingRunning.identifier,
- WorkoutType.workoutType.identifier,
- ];
- final isRequested = await HealthKitReporter.requestAuthorization(readTypes, writeTypes);
- if (isRequested) {}
- } catch (e) {
- print(e);
- }
- }
- Future<bool> requestBackground(BuildContext context) async {
- var preferences = await SharedPreferences.getInstance();
- bool _background = preferences.getBool("RUN_BACKGROUND") ?? false;
- if (!_background) {
- var result = await showDialog(
- context: context,
- builder: (context) => CustomAlertDialog(
- title: "提示",
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 20.0),
- child: Text(
- "由于您当前的操作系统版本会管控后台设置,可能会在运动记录过程中误杀趣动进程,为避免运动记录异常、距离不准确、轨迹点漂移等问题,建议您将趣动后台配置更改为【无限制】,以便我们能为您更准确地记录运动数据",
- style: TextStyle(fontSize: 16.0, color: Color(0xff333333), height: 1.8),
- ),
- ),
- textCancel: "稍后",
- textOk: "立即设置",
- ok: () {
- Navigator.pop(context, true);
- },
- ));
- if (result == true) {
- preferences.setBool("RUN_BACKGROUND", true);
- if (!await jumpBackgroundPermission()) {
- await NavigatorUtil.goPage(context, (context) => SettingPermissionPage());
- }
- _background = true;
- }
- }
- return _background;
- // bool checkPermissions = await SystemAlertWindow.checkPermissions;
- // if (!checkPermissions) {
- // var result = await showDialog(
- // context: context,
- // builder: (context) => CustomAlertDialog(
- // title: "提示",
- // child: Padding(
- // padding: const EdgeInsets.symmetric(horizontal: 20.0),
- // child: Text(
- // "由于您当前的操作系统版本会管控后台设置,可能会在运动记录过程中误杀趣动进程,请您开启悬浮窗权限以保证正常运动。",
- // style: TextStyle(fontSize: 16.0, color: Color(0xff333333)),
- // ),
- // ),
- // textCancel: "稍后",
- // textOk: "立即设置",
- // ok: () {
- // Navigator.pop(context, true);
- // },
- // ));
- // if (result == true) {
- // await SystemAlertWindow.requestPermissions;
- // }
- // return false;
- // }
- // return true;
- }
- Future<bool> runCheck(BuildContext context) async {
- PermissionStatus status = await Permission.locationWhenInUse.request();
- if (status.isGranted != true) {
- ToastUtil.show("请授权位置信息后使用该功能!");
- openAppSettings();
- return false;
- }
- var preferences = await SharedPreferences.getInstance();
- if (Platform.isAndroid) {
- //1111 status PermissionStatus.denied false PermissionStatus.granted false false
- // 1111 status false
- if ((await Permission.locationAlways.status).isGranted != true) {
- if ((preferences.getBool("locationAlways_isPermanentlyDenied") ?? false) != true) {
- var result = await showDialog(
- context: context,
- builder: (context) => CustomAlertDialog(
- title: "地理位置权限申请",
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 20.0),
- child: Text(
- "运动中的轨迹记录相关功能需要您授权地理位置权限。建议开户【始终允许】地理位置权限,否则锁屏后可能无法记录位置。",
- style: TextStyle(fontSize: 16.0, color: Color(0xff333333), height: 1.8),
- ),
- ),
- textOk: "立即设置",
- cancelable: false,
- ok: () {
- Navigator.pop(context, true);
- },
- ));
- if (result) {
- PermissionStatus status = await Permission.locationAlways.request();
- if (status.isGranted == true) {
- } else if (status.isPermanentlyDenied == true) {
- preferences.setBool("locationAlways_isPermanentlyDenied", true);
- } else {
- ToastUtil.show("请授权位置信息【始终允许】后再尝试");
- return false;
- }
- } else {
- return false;
- }
- }
- }
- await Permission.activityRecognition.request();
- await requestBackground(context);
- } else if (Platform.isIOS) {
- await Permission.sensors.request();
- if (!preferences.containsKey("HEALTH_KIT")) {
- await NavigatorUtil.goPage(context, (context) => SettingHealthKitPage());
- preferences.setBool("HEALTH_KIT", true);
- }
- }
- return true;
- }
- Future startRun(BuildContext context) async {
- bool result = await runCheck(context);
- if (result == true) await NavigatorUtil.goPage(context, (context) => RunStartPage());
- // await NavigatorUtil.goPage(context, (context) => RunPage());
- //
- // var package = "com.ouj.hiyd";
- // var componentName = "com.ouj.hiyd.SplashActivity_";
- //
- // if (Platform.isAndroid) {
- // var sport = await app.DeviceApps.getApp(package);
- // if (sport == null) {
- // ToastUtil.show("你还没安装 Hi运动 app");
- // return;
- // }
- // AndroidIntent intent = AndroidIntent(
- // action: "android.intent.action.MAIN",
- // package: package,
- // componentName: componentName,
- // flags: [Flag.FLAG_ACTIVITY_NEW_TASK],
- // arguments: {});
- // intent.launch();
- // }
- }
- class GameRun extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Row(
- children: <Widget>[
- Container(
- width: 50.0,
- height: 50.0,
- margin: const EdgeInsets.all(12.0),
- child: Image.asset("lib/assets/img/home_game_run.png"),
- // child: Image.asset("lib/assets/img/game_icon_function.png")
- ),
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Text(
- "跑步训练",
- style: Theme.of(context).textTheme.subtitle1!.copyWith(fontSize: 16.0, fontWeight: FontWeight.w600),
- ),
- SizedBox(
- height: 3,
- ),
- ValueListenableBuilder(
- valueListenable: Provider.of<UserModel>(context, listen: false).runUserCount,
- builder: (BuildContext context, int value, Widget? child) => Text("$value人在练", style: Theme.of(context).textTheme.bodyText1!, maxLines: 1),
- ),
- SizedBox(
- height: 6,
- )
- ],
- ),
- ),
- Padding(
- padding: const EdgeInsets.all(12.0),
- child: PrimaryButton(
- width: 93,
- height: 35,
- content: "开始跑步",
- callback: () {
- startRun(context);
- },
- ),
- ),
- // Text(
- // "小功能合集",
- // style: Theme.of(context)
- // .textTheme
- // .subtitle1
- // .copyWith(fontSize: 16.0, fontWeight: FontWeight.w600),
- // ),
- // Expanded(
- // child: Padding(
- // padding: EdgeInsets.only(right: 12.0),
- // child: Row(
- // mainAxisAlignment: MainAxisAlignment.end,
- // children: <Widget>[arrowRight5()],
- // ),
- // )),
- ],
- );
- }
- }
- class FunctionSet extends StatelessWidget {
- List map = [
- {
- "name": "户外跑步",
- "url": "function_icon_run.png",
- "callBack": (context) {
- startRun(context);
- },
- },
- {
- "name": "实时计步",
- "url": "function_icon_steps.png",
- "callBack": (context) {
- NavigatorUtil.goPage(context, (context) => StepRealTimePage());
- },
- },
- // {
- // "name": "体重填写",
- // "url": "function_icon_weight.png",
- // "callBack": (context) {
- // NavigatorUtil.goPage(context, (context) => WeightPage(update: true,));
- // },
- // }
- ];
- @override
- Widget build(BuildContext context) {
- return Expanded(
- child: Padding(
- padding: EdgeInsets.fromLTRB(15.0, 0, 15.0, 0),
- child: GridView(
- shrinkWrap: true,
- physics: NeverScrollableScrollPhysics(),
- padding: EdgeInsets.zero,
- gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: 3,
- crossAxisSpacing: 12.0,
- ),
- children: map
- .map((e) => GestureDetector(
- onTap: () {
- e['callBack'](context);
- },
- child: Container(
- decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(5.0))),
- child: Column(
- children: <Widget>[
- Padding(
- padding: EdgeInsets.fromLTRB(22.0, 12.0, 22.0, 8.0),
- child: Image.asset(
- "lib/assets/img/${e["url"]}",
- width: 56.0,
- height: 56.0,
- ),
- ),
- Text(
- "${e['name']}",
- style: TextStyle(color: Color(0xff000000)),
- )
- ],
- ),
- ),
- ))
- .toList(),
- // itemBuilder: (context, index) {
- // return Container(
- // decoration: BoxDecoration(
- // color: Colors.white,
- // borderRadius: BorderRadius.all(Radius.circular(5.0))),
- // height: 116,
- // child: Column(
- // children: <Widget>[
- // Padding(
- // padding: EdgeInsets.fromLTRB(22.0, 12.0, 22.0, 8.0),
- // child: Image.asset(
- // "lib/assets/img/function_icon_run.png",
- // width: 56.0,
- // height: 56.0,
- // ),
- // ),
- // Text(
- // "户外跑步",
- // style: TextStyle(color: Color(0xff000000)),
- // )
- // ],
- // ),
- // );
- // },
- // itemCount: 3,
- ),
- ),
- );
- }
- }
|