You can use ShellRoute. For example:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MyApp());
}
final _router = GoRouter(
initialLocation: '/1',
routes: [
ShellRoute(
builder: (
BuildContext context,
GoRouterState state,
Widget child,
) {
return Scaffold(
appBar: AppBar(
title: const Text('Scaffold Common'),
),
body: child,
);
},
routes: [
GoRoute(
path: '/1',
pageBuilder: (context, state) => const NoTransitionPage(
child: PageOne(),
),
),
GoRoute(
path: '/2',
pageBuilder: (context, state) => const NoTransitionPage(
child: PageTwo(),
),
),
GoRoute(
path: '/3',
pageBuilder: (context, state) => const NoTransitionPage(
child: PageThree(),
),
),
],
),
],
);
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class PageOne extends StatelessWidget {
const PageOne({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text('PageOne'),
ControlCommonWidget(),
],
);
}
}
class PageTwo extends StatelessWidget {
const PageTwo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text('PageTwo'),
ControlCommonWidget(),
],
);
}
}
class PageThree extends StatelessWidget {
const PageThree({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text('PageThree'),
ControlCommonWidget(),
],
);
}
}
class ControlCommonWidget extends StatelessWidget {
const ControlCommonWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
TextButton(
onPressed: () {
context.go('/1');
},
child: const Text('Go to Page One'),
),
TextButton(
onPressed: () {
context.go('/2');
},
child: const Text('Go to Page Two'),
),
TextButton(
onPressed: () {
context.go('/3');
},
child: const Text('Go to Page Three'),
)
],
);
}
}