【发布时间】:2023-02-14 21:18:57
【问题描述】:
在顶层设置 Flutter gorouter redirect 属性不会让导航转到/推送任何其他页面。它在按下路由按钮而不是预期页面(ItemOne())时重定向到 initialLocation。
日志:
[GoRouter] going to /one
[GoRouter] redirecting to RouteMatchList(/)
协程代码:
void main() => runApp(const NavApp());
const isAuth = true;
class NavApp extends StatelessWidget {
const NavApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: GoRouter(
debugLogDiagnostics: true,
initialLocation: '/',
redirect: (context, state) => isAuth ? '/' : '/one',
routes: [
GoRoute(
path: '/',
builder: (context, state) => const NavHome(),
),
GoRoute(
path: '/one',
builder: (context, state) => const ItemOne(),
),
],
),
);
}
}
首页代码:
class NavHome extends StatelessWidget {
const NavHome({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Nav Home'),
),
body: Center(
child: IconButton(
onPressed: () => context.push('/one'),
icon: const Text('Push One'),
),
),
);
}
}
我们路由到使用按钮的页面:
class ItemOne extends StatelessWidget {
const ItemOne({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Item 1'),
),
body: const Text('This is page for Item One'),
);
}
}
【问题讨论】:
标签: flutter dart flutter-go-router