从 AppLifecycleState.resumed 事件导航到新屏幕?

Navigating to a new screen from AppLifecycleState.resumed event?

我试图在 AppLifecycleState 事件中启动一个新屏幕,但没有任何反应。那是因为在这个事件中有 会包含 Navigator.

每次应用从恢复状态返回时,应用都必须打开锁屏 (AppLifecycleState.resumed)。最简单的例子就是每次打开都有锁屏保护的银行应用。

如何在代码的任何位置显示新屏幕?

我的代码不起作用:

import 'package:alarm_prevozi/screens/home_screen/home_screen.dart';
import 'package:alarm_prevozi/screens/lock_screen/lock_screen.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:alarm_prevozi/helpers/translations.dart';
import 'package:flutter/material.dart';

void main() async {
  // Then start the application
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  BuildContext myContext;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

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

  // Listen for when the app enter in background or foreground state.
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      // user returned to our app
      _showLockScreenDialog();
    } else if (state == AppLifecycleState.inactive) {
      // app is inactive
    } else if (state == AppLifecycleState.paused) {
      // user is about quit our app temporally
    } else if (state == AppLifecycleState.suspending) {
      // app suspended (not used in iOS)
    }
  }

  // Main initialization
  @override
  Widget build(BuildContext context) {

          return MaterialApp(
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
            // Tells the system which are the supported languages
            supportedLocales: translationService.supportedLocales(),
            home: HomeScreen()
          );
  }

  void _showLockScreenDialog() {
    Navigator.of(context)
        .pushReplacement(new MaterialPageRoute(builder: (BuildContext context) {
      return PassCodeScreen();
    }));
  }
}

您可以使用流来处理简历“事件”。 由于您的观察者位于 MaterialApp 之上,因此您的 MyApp 上下文中没有 Navigator,因此您需要使用 GlobalKey 来访问 MaterialApp 提供的 Navigator。

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  StreamController<bool> _showLockScreenStream = StreamController();
  StreamSubscription _showLockScreenSubs;
  GlobalKey<NavigatorState> _navigatorKey = GlobalKey();

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

    _showLockScreenSubs = _showLockScreenStream.stream.listen((bool show){
      if (mounted && show) {
        _showLockScreenDialog();
      }
    });
  }

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

  // Listen for when the app enter in background or foreground state.
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      // user returned to our app, we push an event to the stream
      _showLockScreenStream.add(true);
    } else if (state == AppLifecycleState.inactive) {
      // app is inactive
    } else if (state == AppLifecycleState.paused) {
      // user is about quit our app temporally
    } else if (state == AppLifecycleState.detached) {
      // detached from any host views
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: _navigatorKey,
      ...
    );
  }

  void _showLockScreenDialog() {
    _navigatorKey.currentState.
        .pushReplacement(new MaterialPageRoute(builder: (BuildContext context) {
      return PassCodeScreen();
    }));
  }
}