是否可以重绘屏幕或导航到Flutter中暂停的AppLifecycleState.pause上的其他屏幕

时间:2019-03-22 21:09:36

标签: flutter

我有一个Flutter应用程序,其中包含敏感的中间信息,并且企业希望在将应用程序置于后台(包括“最近使用的应用程序”屏幕)时隐藏此信息。

我添加了WidgetsBindingObserver,并且正在正确地监听事件。恢复状态正确触发,并将用户发送回登录页面;但是,暂停的事件在收到状态后不做任何事情。作为参考,我尝试将一个新屏幕推送到堆栈上,并弹出所有屏幕,直到到达登录名为止,但这两个都不起作用。

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.paused:
        Navigator.of(context).push(new PageRouteBuilder(
          pageBuilder: (_, __, ___) => new Splash(
                inBackground: true,
              ),
        ));
        break;
      case AppLifecycleState.resumed:
        Navigator.of(context).pushNamed('/login');
        break;
      default:
        break;
    }
  }

我希望在收到暂停事件后能够更改屏幕以保护此敏感信息。任何想法都欢迎!

编辑:最新代码。

import 'package:boxview_mobile_flutter/screens/splash/index.dart';
import 'package:boxview_mobile_flutter/services/shared_prefs.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';

class PushNotifications extends StatefulWidget {
  @override
  _PushNotificationsState createState() => _PushNotificationsState();
}

class _PushNotificationsState extends State<PushNotifications> with WidgetsBindingObserver {
  bool loggedOut = false;
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  @override
  Widget build(BuildContext context) {
    return Container(child: Splash(loggedOut: this.loggedOut));
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.paused:
        print("Paused");
        break;
      case AppLifecycleState.inactive:
        print("inactive");
        setState(() {
          loggedOut = true;
        });
        break;
      case AppLifecycleState.suspending:
        print("suspending");
        break;
      case AppLifecycleState.resumed:
        setState(() {
          loggedOut = false;
        });
        print("resumed");
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      SharedPreferencesHelper.setFirebaseToken(token);
    });
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

启动只是一个启动页面,布尔值loginOut参数只是说不要转发到登录页面。

1 个答案:

答案 0 :(得分:0)

不要忘记将with WidgetsBindingObserver添加到您的州级课程

class YourClass extends StatefulWidget { ....

class _YourClassState extends State<BottomNavigator> with WidgetsBindingObserver { ...

 @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  AppLifecycleState _notification;

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.paused:
        print("Paused");
        setState(() {
          _notification = state;
        });
        break;
      case AppLifecycleState.inactive:
        setState(() {
          _notification = state;
        });
        print("inactive");
        break;
      case AppLifecycleState.suspending:
        setState(() {
          _notification = state;
        });
        print("suspending");
        break;
      case AppLifecycleState.resumed:
        setState(() {
          _notification = state;
        });
        print("resumed");
        break;
    }
  }

并使用_notification

更改您的应用状态

例如

@override
  Widget build(BuildContext context) {
    return _notification == AppLifecycleState.inactive
        ? Scaffold(
            body: Text("inactive"),
          )
        : YourRealWidget()

enter image description here

您可以查看有关WidgetsBindingObserver from here的更多信息