【问题标题】:Having Flutter gorouter redirect property doesn't let navigation work拥有 Flutter gorouter redirect 属性不会让导航工作
【发布时间】: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


    【解决方案1】:

    请这样使用

    Center(
        child: IconButton(
           onPressed: () => context.go('/one'),
          icon: const Text('Push One'),
        ),
    

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多