go_navigator 2.0.3 go_navigator: ^2.0.3 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>();
void main() {
runApp(MyApp());
}
/// The main application widget.
///
/// This widget initializes the [MaterialApp] and configures the [GoNavigator]
/// for handling routes within the app.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
navigatorObservers: [
GoNavigatorObserver() // Uncomment this if you have GoNavigatorObserver
],
onGenerateRoute: (settings) {
return GoNavigator(
initialRoute: HomePage(),
routes: {
'/': (context, args) => HomePage(),
'home': (context, args) => HomePage(),
'dashboard': {
'/': (context, args) => DashboardPage(),
'settings': {
'/': (context, args) => SettingsPage(),
'profile': {
'/': (context, args) => ProfilePage(),
'edit': (context, args) {
return const EditProfilePage();
},
},
}
},
'profile': {
'/': (context, args) => ProfilePage(),
'edit': (context, args) {
return const EditProfilePage();
},
},
},
).generateRoute(settings);
},
);
}
}
/// The HomePage widget.
///
/// This widget displays the home screen of the app with buttons to navigate
/// to different pages.
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
context.to('dashboard/settings/profile/edit',
args: {'userId': '123'});
},
child: const Text('Go to Dashboard'),
),
ElevatedButton(
onPressed: () {
context.to('profile');
},
child: const Text('Go to Profile'),
),
],
),
),
);
}
}
/// The DashboardPage widget.
///
/// This widget displays the dashboard screen with a button to navigate to the settings page.
class DashboardPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Dashboard')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.to('dashboard/settings');
},
child: const Text('Go to Settings'),
),
),
);
}
}
/// The SettingsPage widget.
///
/// This widget displays the settings screen.
class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: const Center(
child: Text('Settings Page'),
),
);
}
}
/// The ProfilePage widget.
///
/// This widget displays the profile screen with a button to navigate to the edit profile page.
class ProfilePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.to('profile/edit', args: {'userId': '123'});
},
child: const Text('Edit Profile'),
),
),
);
}
}
/// The EditProfilePage widget.
///
/// This widget displays the edit profile screen and shows the user ID passed as an argument.
class EditProfilePage extends StatelessWidget {
const EditProfilePage();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Edit Profile')),
body: Center(
child: Text('Editing Profile for User ${context.args['userId']}'),
),
);
}
}