【发布时间】:2021-08-22 01:08:03
【问题描述】:
编辑:我最终使它工作,更改了代码。
这是我启动应用程序的方式:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test Demo',
home: LoginPage(),
onGenerateRoute: (settings) {
return CustomPageRouteBuilder.getPageRouteBuilder(Routing.openRoute(settings), settings.name);
},
);
}
}
class CustomPageRouteBuilder {
static PageRouteBuilder getPageRouteBuilder(Widget widget, [String? name]) {
return PageRouteBuilder(
settings: RouteSettings(name: name),
transitionDuration: Duration(
milliseconds: 100,
),
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation, Widget child) {
animation = CurvedAnimation(
parent: animation,
curve: Curves.elasticIn,
);
return FadeTransition(
opacity: animation,
child: child,
);
},
pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation) {
return Scaffold(
body: Stack(children: [
BiometricAuthentication(widget),
Align(
alignment: Alignment.bottomLeft,
child: Opacity(
opacity: 0.5,
child: Text("Version: ${Constants.buildVersion}",
style: TextStyle(
color: Constants.FONT_COLOR,
fontSize: 12,
)),
),
)
]),
);
});
}
}
然后我让我的 Routing 类处理路由:
enum Routes {
LOGIN,
FIRST_PAGE,
SECOND_PAGE,
THIRD_PAGE,
}
extension GetRoute on Routes {
String route() {
return '/' + this.toString().split('.').last.toLowerCase().replaceAll('_', '-');
}
}
class Routing {
static openRoute(RouteSettings settings) {
return getRoute(settings);
}
static getRoute(RouteSettings settings) {
String? name = settings.name;
if (name == Routes.LOGIN.route()) {
return LoginPage();
}
if (name == Routes.FIRST_PAGE.route()) {
return FirstPage();
}
if (name == Routes.SECOND_PAGE.route()) {
return SecondPage();
}
if (name == Routes.THIRD_PAGE.route()) {
return ThirdPage();
}
}
}
我在我的ThirdPage 上使用以下代码:登录 --> FirstPage --> SecondPage --> ThirdPage:
await Navigator.pushNamed(context, Routes.FIRST_PAGE.route()); // Login
await Navigator.pushNamed(context, Routes.SECOND_PAGE.route()); // FirstPage
await Navigator.pushNamed(context, Routes.THIRD_PAGE.route()); // SecondPage
所以我的导航器堆栈中应该有 4 页。
现在我尝试 popUntil Routes.FIRST_PAGE.route(),所以 Login 和 FirstPage 应该在导航器中:
Navigator.popUntil(context, ModalRoute.withName(Routes.FIRST_PAGE.route())); // ThirdPage
在这里,我希望在堆栈中有 Login -> FirstPage,但一切都变黑了。
如果我只使用Navigator.pop(context),它会从ThirdPage 返回到SecondPage。但在我的实际应用程序中,如果使用按钮,我必须返回 5 个屏幕,所以我不想在每个页面中都处理它......
有什么想法吗?提前致谢。
【问题讨论】: