Flutter相关

Flutter怎么实现类似于Android页面onResume的

2022-07-28  本文已影响0人  码农朱同学

在用flutter改造Android app中,遇到这样的问题,如下图,推送通知,已开启获取原生方法,告知是否开启,
点击箭头那边进入原生方法,打开系统通知设置页面。

推送通知状态展示
              _getModuleDivider(),
              _getItem("推送通知", () {
                FlutterMethodChannel().pushSystemNotice();
              }, content: pushNoticeOpen),
              _getDivider(),
              _getItem("版本号", () {
                FlutterMethodChannel().versionCheckUpdate(false, true);
              }, content: versionName, isShow: false),

进入系统页面切换通知状态时,再返回,如何及时刷新推送通知状态?

Flutter的onResume方法
class _DoubleListPageState extends State<DoubleListPage>
    with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    _getAppVersion();
    _getPushNotice();
    WidgetsBinding.instance?.addObserver(this);
  }
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      _getPushNotice();
    }
  }
  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance?.removeObserver(this);
  }
  /// 获取是否有通知权限方法
  _getPushNotice() {
    FlutterMethodChannel()
        .getAppNotice((result) => {pushNoticeOpen = result, setState(() {})});
  }

当然,如果引用了 flutter_boost 库(git://github.com/alibaba/flutter_boost.git),那将会更简单。

  @override
  void onForeground() {
    super.onForeground();
    Future.delayed(
        Duration(seconds: 1),
        () => {
              _getPushNotice() //延迟一秒刷新,避免闪屏问题
            });
  }
上一篇下一篇

猜你喜欢

热点阅读