Normally you would have a Scaffold with its own AppBar on every page, but why is that needed with go_router? Why can't you just have a single Scaffold with an AppBar and let that handle navigation.
Going to the SecondScreen in this example won't update the AppBar and show the back button.
Why is this not possible?
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() => runApp(const MyApp());
final GoRouter _router = GoRouter(
routes: <RouteBase>[
ShellRoute(
builder: (context, state, child) {
return Scaffold(
body: child,
appBar: AppBar(
title: const Text('Test'),
),
);
},
routes: [
GoRoute(
path: '/',
pageBuilder: ((context, state) => const NoTransitionPage(
child: HomeScreen(),
)),
routes: [
GoRoute(
path: 'second',
pageBuilder: ((context, state) => const NoTransitionPage(
child: SecondScreen(),
)),
),
],
),
],
),
],
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () => context.go('/second'),
child: const Text('Go to the Second screen'),
),
);
}
}
class SecondScreen extends StatelessWidget {
const SecondScreen({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Text('Second screen'),
);
}
}