【发布时间】:2019-12-21 16:34:01
【问题描述】:
我正在尝试在初始应用启动时显示启动画面,直到我正确检索到所有数据。一旦它出现,我想导航到应用程序的主屏幕。
不幸的是,我找不到触发运行这种导航的方法的好方法。
这是我用来测试这个想法的代码。具体来说,我想在变量shouldProceed 变为真时运行命令Navigator.pushNamed(context, 'home');。现在,我能想到的唯一方法是显示一个我需要按下以触发导航代码的按钮:
import 'package:flutter/material.dart';
import 'package:catalogo/src/navigationPage.dart';
class RouteSplash extends StatefulWidget {
@override
_RouteSplashState createState() => _RouteSplashState();
}
class _RouteSplashState extends State<RouteSplash> {
ValueNotifier<bool> buttonTrigger;
bool shouldProceed = false;
_fetchPrefs() async { //this simulates the asynchronous function
await Future.delayed(Duration(
seconds:
1)); // dummy code showing the wait period while getting the preferences
setState(() {
shouldProceed = true; //got the prefs; ready to navigate to next page.
});
}
@override
void initState() {
super.initState();
_fetchPrefs(); // getting prefs etc.
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: shouldProceed
? RaisedButton(
onPressed: () {
print("entered Main");
Navigator.pushNamed(context, 'home'); // <----- I want this to be triggered by shouldProceed directly
},
child: Text("Continue"),
)
: CircularProgressIndicator(), //show splash screen here instead of progress indicator
),
);
}
}
那么,简而言之,当 shouldProceed 发生变化时,如何触发运行 Navigation 代码的函数?
【问题讨论】: