问题描述
我试图在启动应用程序时显示信息对话框。
关闭后,会出现另一个请求许可的窗口。我在 initState 函数中调用它。它有效,但我注意到第一个信息对话框也会在 15 秒过去后自行关闭。据我了解,这是因为应用程序已加载且上下文丢失。
当我将 runApp(MyApp())
更改为 runApp(MaterialApp(home: MyApp()))
时。它有效,弹出窗口不会消失。但是其他页面上的其他显示对话框没有自动关闭 (Navigator.of(context).pop()
并且 Navigator.pop(context)
不起作用。
如何正确地将上下文传递给我的初始显示对话框,以便在开始页面加载时它不会消失?
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
static final navKey = new GlobalKey<NavigatorState>();
const MyApp({Key navKey}) : super(key: navKey);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
final context = MyApp.navKey.currentState.overlay.context;
await showDialogIfFirstLoaded(context);
await initPlatformState();
});
}
showDialogIfFirstLoaded(BuildContext context,prefs) async {
bool isFirstLoaded = prefs.getBool(keyIsFirstLoaded);
if (isFirstLoaded == null) {
return showDialog(
context: context,builder: (BuildContext context) {
// return object of type Dialog
return new AlertDialog(
// title: new Text("title"),content: new Text("//"),actions: <Widget>[
new FlatButton(
child: new Text(".."),onpressed: () {
Navigator.of(context).pop();
},),],);
},);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey:MyApp.navKey,home: new SplashScreen(),}
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => new _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin {
Timer _timer;
bool _visible = true;
startTime() async {
_timer = Timer(new Duration(seconds: 5),navigationPage);
}
void navigationPage() {
Navigator.of(context).pushReplacementNamed('/home');
}
@override
void initState() {
_timer = Timer(Duration(seconds: 4),() => setState(
() {
_visible = !_visible;
},);
startTime();
super.initState();
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Stack(
children: <Widget>[
Container(
width: double.infinity,child: Image.asset('images/bg.jpg',fit: BoxFit.cover,height: 1200,Container(
width: double.infinity,color: Color.fromrGBO(0,0.8),Container(
alignment: Alignment.center,child: Row(
children: <Widget>[
Expanded(
flex: 2,child: Container(
child: Text(''),);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)