我是新手,我已经制作了大约6-8 pages.All的应用程序,我希望从用户离开或完全终止应用程序的最后一个屏幕上继续。
还有可能使用mobx吗??
发布于 2019-05-18 18:44:34
每次打开新路径时,您都可以保留路径名,然后每次打开应用程序时都要查找最后一次失败:
String lastRouteKey = 'last_route';
void main() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
String lastRoute = preferences.getString(lastRouteKey);
runApp(MyApp(lastRoute));
}
class MyApp extends StatelessWidget {
final String lastRoute;
MyApp(this.lastRoute);
@override
Widget build(BuildContext context) {
bool hasLastRoute = getWidgetByRouteName(lastRoute) != null;
return MaterialApp(
home: Foo(),
initialRoute: hasLastRoute ? lastRoute : '/',
onGenerateRoute: (RouteSettings route) {
persistLastRoute(route.name);
return MaterialPageRoute(
builder: (context) => getWidgetByRouteName(route.name),
);
},
);
}
Widget getWidgetByRouteName(String routeName) {
switch (routeName) {
case '/': return MainWidget();
// Put all your routes here.
default: return null;
}
}
void persistLastRoute(String routeName) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setString(lastRouteKey, routeName);
}
}
请注意,这并不是100%的精确性,因为持久化是异步的,用户可能会在应用程序完成之前关闭它。然而,它通常发生得非常快,几乎所有的时间都应该起作用。
发布于 2019-05-18 18:57:32
也就是说,当用户第一次打开应用程序时,第一页是page1。然后,假设用户导航到page5,一个值'page5‘将存储在共享首选项中,并在下次用户打开应用程序时检索。因此,“page5”将成为下一次打开应用程序的第一页。
https://stackoverflow.com/questions/56201714
复制相似问题