1

I am trying to build navigation between screens and it doesn't work between two specific screens. My code on clicked button that should lead to the second screen:

    child: FilledButton(
                      onPressed: () => GoRouter.of(context).go('/level1'),
                                style: TextButton.styleFrom(
                                primary: Color(0xFF000000),
                                fixedSize: Size(157, 40),
                                backgroundColor: Color(0xFFFF8383)
                                 ), // Background Color
                        child: const Text('START')
                  ),

main.dart code with routes:

     GoRoute(
path: '/level1',
builder:(context, state) => Level1Screen(key: Key('level1')),
),

level1screen code:

   class Level1Screen extends StatefulWidget {

   const Level1Screen({super.key});

   @override
   State<Level1Screen> createState() => _Level1ScreenState();
   }

   class _Level1ScreenState extends State<Level1Screen> {
   static final _log = Logger('level1');
  • 1
    according to GoRouter documentation, you should use `MaterialApp.router` widget as the root Widget of application. are you using `MaterialApp.router` Widget? – Mahdi Dahouei Jun 29 '23 at 02:28

1 Answers1

1

You missing / in your GoRoute path definition.

UPD This example is working:

main.dart

final _router = GoRouter(
  initialLocation: '/',
  routes: [
    GoRoute(
      path: '/',
      builder:(context, state) => const Level2Screen(key: Key('level2')),
    ),
    GoRoute(
      path: '/level1',
      builder:(context, state) => const Level1Screen(key: Key('level1')),
    )
  ]
);

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Flutter Demo',
      routerConfig: _router,
    );
  }
}

Level1 class

class Level1Screen extends StatefulWidget {

  const Level1Screen({super.key});

  @override
  State<Level1Screen> createState() => _Level1ScreenState();
}

class _Level1ScreenState extends State<Level1Screen> {

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Text(
          'Level1 Screen'
        ),
      ),
    );
  }
}

Level2 class

class Level2Screen extends StatefulWidget {

  const Level2Screen({super.key});

  @override
  State<Level2Screen> createState() => _Level2ScreenState();
}

class _Level2ScreenState extends State<Level2Screen> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FilledButton(
            onPressed: () => GoRouter.of(context).go('/level1'),
            style: TextButton.styleFrom(
                primary: Color(0xFF000000),
                fixedSize: Size(157, 40),
                backgroundColor: Color(0xFFFF8383)
            ), // Background Color
            child: const Text('START')
        ),
      ),
    );
  }
}
Altair1908
  • 26
  • 1
  • 3