mx_system_authority 0.0.4 copy "mx_system_authority: ^0.0.4" to clipboard
mx_system_authority: ^0.0.4 copied to clipboard

增加修改时间或者时区后主动监听修改

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mx_system_authority/check_time_model.dart';
import 'package:mx_system_authority/mx_system_authority.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _mxSystemAuthorityPlugin = MxSystemAuthority.instance;
  bool checkResult = false;
  bool isSetting = false;
  String observeMsg = "";

  @override
  void initState() {
    super.initState();
    initPlatformState();
    addObserver();
  }

  addObserver()async{
    _mxSystemAuthorityPlugin.onMessageRecive(
      callBack: (CheckTimeModel model) {
        print("isMoreThan:${model.isMoreThan},time:${model.time}");
        setState(() {
            observeMsg = model.isMoreThan ? "发生改变" : "没有发生改变";
        });
        if (model.isMoreThan) {
          _mxSystemAuthorityPlugin.showAlertView(
              title: "请调整日期和时间",
              message: "前往设置 > 通用 > 日期与时间,启用“自动设置”以确保时间准确",
              cancelTitle: "取消",
              okTitle: "确定");
        }
      },
    );

   _mxSystemAuthorityPlugin.addObserverListenterChange(interval: 300);
   
  //  print("isMoreThan:${model.isMoreThan}");
  //  if(model.isMoreThan){
  //   _mxSystemAuthorityPlugin.showAlertView(
  //     title: "请调整日期和时间",
  //    message: "前往设置 > 通用 > 日期与时间,启用“自动设置”以确保时间准确", 
  //    cancelTitle: "取消",
  //     okTitle: "确定");
  //  }
  }

  Future<void> initPlatformState() async {
    String platformVersion;

    try {
      platformVersion = await _mxSystemAuthorityPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Container(
          alignment: const Alignment(0, 0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: ()  {
                   
                     _mxSystemAuthorityPlugin.checkTime(interval: 500);
                    //     if (model.isMoreThan) {
                    //   _mxSystemAuthorityPlugin.showAlertView(
                    //       title: "请调整日期和时间",
                    //       message: "前往设置 > 通用 > 日期与时间,启用“自动设置”以确保时间准确",
                    //       cancelTitle: "取消",
                    //       okTitle: "确定");
                    // }
                    // setState(() {
                    //   checkResult = model.isMoreThan;
                    // });
                  },
                  child: const Text("检测时间")),
              const SizedBox(
                height: 20,
              ),
              Text("结果:$checkResult"),
              const SizedBox(
                height: 20,
              ),
              ElevatedButton(
                  onPressed: () async {
                    bool result = await _mxSystemAuthorityPlugin.showAlertView(
                      title: "请调整日期和时间",
                      message: "前往设置 > 通用 > 日期与时间,启用“自动设置”以确保时间准确",
                      cancelTitle: "取消",
                      okTitle: "去设置",
                    );
                    setState(() {
                      isSetting = result;
                    });
                    // print("是否去设置了:$result");
                  },
                  child: const Text("去设置")),
              const SizedBox(
                height: 20,
              ),
              Text("是否去设置了$isSetting"),
              const SizedBox(
                height: 20,
              ),
              ElevatedButton(
                  onPressed: () {
                    _mxSystemAuthorityPlugin.gotoSetting();
                  },
                  child: const Text("调用去设置的url")),
                   const SizedBox(
                height: 10,
              ),
               Text(observeMsg),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
0
points
22
downloads

Publisher

unverified uploader

Weekly Downloads

增加修改时间或者时区后主动监听修改

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on mx_system_authority