gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:39:46 30 4
gpt4 key购买 nike

我试图在 AppLifecycleState 事件中启动一个新屏幕,但没有任何反应。那是因为有 no context available在这种情况下,将包含 Navigator。

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

无论您在代码中的哪个位置,如何显示新屏幕?

我的代码不起作用:

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();
}));
}
}

关于flutter - 从 AppLifecycleState.resumed 事件导航到新屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57951490/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com