go_navigator 2.0.1 go_navigator: ^2.0.1 copied to clipboard
Go navogator handles navigation by reducing class argument and exposing the top most route args to the context and navigation key where passed
import 'package:flutter/material.dart';
import 'package:go_navigator/go.dart';
/// The global key for the navigator, used to manage navigation from anywhere in the app.
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
/// The main entry point of the application.
void main() {
runApp(MyApp());
}
/// The main application widget.
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
/// The state for the main application widget.
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
navigatorObservers: [GoNavigatorObserver()],
onGenerateRoute: (RouteSettings routeSettings) => GoNavigator(
initialRoute: Page1(),
routes: {
/// Define the routes for the application using the `NavigationSystem`.
Page1.routeName: (context, args) => Page1(),
Page2.routeName: (context, args) => const Page2(),
Page3.routeName: (context, args) => const Page3(),
Page4.routeName: (context, args) => const Page4(),
},
).generateRoute(routeSettings),
);
}
}
/// The first page of the application.
class Page1 extends StatelessWidget {
/// The route name for this screen.
static const routeName = '/d1';
Page1({super.key});
/// The text editing controller for the input field on Page1.
TextEditingController p1Ctl = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(' Page 1'),
),
body: Center(
child: Column(
children: [
const Text('This is Page 1 content.'),
TextField(controller: p1Ctl)
],
),
),
floatingActionButton: FloatingActionButton(
heroTag: 'p1_f',
onPressed: () {
/// Navigate to Page2 when the floating action button is pressed.
navigatorKey.to(Page2.routeName, args: {'title': p1Ctl.text});
},
child: const Icon(Icons.arrow_forward_ios_rounded)),
);
}
}
/// The second page of the application.
class Page2 extends StatelessWidget {
/// The route name for this screen.
static const routeName = '/d2';
const Page2({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(' Page 2 - Data ${context.args['title']}'),
),
body: const Center(
child: Text('This is Page 2 content.'),
),
floatingActionButton: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
FloatingActionButton(
heroTag: 'p2_b',
onPressed: () {
/// Pop the current page to return to the previous page.
context.pop();
},
child: const Icon(Icons.arrow_back_ios_new_rounded),
),
const SizedBox(
width: 10,
),
FloatingActionButton(
heroTag: 'p2_f',
onPressed: () {
/// Navigate to Page3 when the second floating action button is pressed.
navigatorKey.to(Page3.routeName, args: {'context': context});
},
child: const Icon(Icons.arrow_forward_ios_rounded)),
],
),
);
}
}
/// The third page of the application.
class Page3 extends StatelessWidget {
/// The route name for this screen.
static const routeName = '/d3';
const Page3({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(' Page 3'),
),
body: Center(
child: Text(
'This is ${(context.args['context'] as BuildContext).args['title']} content.'),
),
floatingActionButton: FloatingActionButton(
heroTag: 'p3_b',
onPressed: () {
/// Pop the current page to return to the previous page.
context.pop();
},
child: const Icon(Icons.arrow_back_ios_new_rounded),
),
);
}
}
/// The fourth page of the application.
class Page4 extends StatelessWidget {
/// The route name for this screen.
static const routeName = '/e4';
const Page4({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(' Page 4'),
),
body: const Center(
child: Text('This is Page 4 content.'),
),
floatingActionButton: FloatingActionButton(
heroTag: 'p4_b',
onPressed: () {
/// Pop the current page to return to the previous page.
context.pop();
},
child: const Icon(Icons.arrow_back_ios_new_rounded),
),
);
}
}