GoRoute#
All named parameters#
The correct and complete numbered list of named parameters of GoRoute in Flutter (go_router package). These are only the parameters that belong to the GoRoute constructor
GoRoute(
path: (required) β’ The URL path (e.g. β/β, β/loginβ, β/profile/:idβ)
name: β’ Optional route name for context.goNamed()
builder: β’ Builds a widget for the route
pageBuilder: β’ Builds a custom Page (advanced navigation)
routes: β’ Child/sub routes (nested routes)
redirect: β’ Redirect logic for this route only
onExit: β’ Runs when leaving the route (can prevent leaving)
parentNavigatorKey: β’ Tells the route which navigator to use (ShellRoute use)
)
β 1.path:#
Here is the simplest and cleanest Flutter example that demonstrates the path: parameter in GoRoute using MaterialApp.router().
I made it very short, no errors, and perfect for beginners.
β Simple Program β Using path: in GoRoute with MaterialApp.router()
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
// -------------------------------
// GoRouter with path:
// -------------------------------
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/', // π Home path
builder: (context, state) => const HomePage(),
),
GoRoute(
path: '/about', // π About page path
builder: (context, state) => const AboutPage(),
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/about'); // π navigate using path
},
child: const Text('Go to About'),
),
),
);
}
}
class AboutPage extends StatelessWidget {
const AboutPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('About')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/'); // π back to home
},
child: const Text('Go Back Home'),
),
),
);
}
}
β Explanation (Very Simple)
πΉ path: β/β
This defines the Home page.
πΉ path: β/aboutβ
This defines the About page.
πΉ Navigation
You go to a path using:
context.go('/about');
And go back:
context.go('/');
β 2.name:#
Here is a very simple, clean, error-free example showing how to use the name: parameter in GoRoute **with **MaterialApp.router().
β Simple Program β Using name: in GoRoute with MaterialApp.router()
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
// --------------------------------
// GoRouter using name:
// --------------------------------
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/',
name: 'home', // π route name
builder: (context, state) => const HomePage(),
),
GoRoute(
path: '/about',
name: 'about', // π route name
builder: (context, state) => const AboutPage(),
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.goNamed('about'); // π navigate using name
},
child: const Text('Go to About'),
),
),
);
}
}
class AboutPage extends StatelessWidget {
const AboutPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('About')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.goNamed('home'); // π navigate using name
},
child: const Text('Go Back Home'),
),
),
);
}
}
β Explanation (Very Easy)
πΉ name: βhomeβ
Gives a name to the route.
πΉ context.goNamed(βaboutβ)
Navigate using the route name instead of URL path.
πΉ Benefits of using name:
Cleaner navigation
Easy to pass parameters
Avoid hard-coding URLs
Works great for big apps
β 3.builder:#
Here is the simplest possible program that demonstrates the builder: parameter in GoRoute using MaterialApp.router().
Very clean, easy, and beginner-friendly.
β Simple Program β Using builder: in GoRoute with MaterialApp.router()
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
// -------------------------------
// GoRouter using builder:
// -------------------------------
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomePage(), // π using builder
),
GoRoute(
path: '/about',
builder: (context, state) => const AboutPage(), // π using builder
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router, // π connect router
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/about'); // π navigate
},
child: const Text('Go to About'),
),
),
);
}
}
class AboutPage extends StatelessWidget {
const AboutPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('About')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/'); // π back home
},
child: const Text('Go Back Home'),
),
),
);
}
}
β Explanation (Very Easy)
πΉ builder: (context, state) => Widget
The builder returns the Widget you want to show for this route.
πΉ Why use builder?
Easy
Clean
Best for simple static pages
No extra animation or advanced options
β 4.pageBuilder:#
Here is the cleanest, simplest, no-error example that demonstrates the pageBuilder: parameter in GoRoute using MaterialApp.router().
This example returns a custom transition page, which is what pageBuilder: is normally used for.
β Simple Program β Using pageBuilder: in GoRoute with MaterialApp.router()
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
// -------------------------------------
// GoRouter using pageBuilder:
// -------------------------------------
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/',
pageBuilder: (context, state) {
return MaterialPage(
child: const HomePage(),
);
},
),
GoRoute(
path: '/about',
pageBuilder: (context, state) {
return CustomTransitionPage(
child: const AboutPage(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeTransition(
opacity: animation,
child: child,
);
},
);
},
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/about');
},
child: const Text('Go to About (Fade Transition)'),
),
),
);
}
}
class AboutPage extends StatelessWidget {
const AboutPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('About')),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/');
},
child: const Text('Go Back Home'),
),
),
);
}
}
β Explanation (Very Easy)
πΉ pageBuilder:
This returns a Page object, not a widget.
You can return:
MaterialPage
CupertinoPage
CustomTransitionPage
NoTransitionPage
πΉ Why use pageBuilder?
Add animations (fade, slide, scale, rotate)
Use platform-specific pages (iOS/Android)
More control than builder:
πΉ In this example:
/ uses a simple MaterialPage
/about uses a CustomTransitionPage with fade animation
β 5.routes:#
Here is the simplest possible example **that demonstrates the **routes: parameter in GoRoute using MaterialApp.router().
This example shows:
β parent route
β child routes (nested)
β using routes: inside a GoRoute
β Simple Program β Using routes: in GoRoute with MaterialApp.router()
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
// ------------------------------------------------
// GoRouter using routes: (nested child routes)
// ------------------------------------------------
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
// π Child routes under "/"
routes: [
GoRoute(
path: 'about', // full path becomes: /about
builder: (context, state) => const AboutPage(),
),
GoRoute(
path: 'contact', // full path becomes: /contact
builder: (context, state) => const ContactPage(),
),
],
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => context.go('/about'),
child: const Text('Go to About'),
),
ElevatedButton(
onPressed: () => context.go('/contact'),
child: const Text('Go to Contact'),
),
],
),
);
}
}
class AboutPage extends StatelessWidget {
const AboutPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('About')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('Go Home'),
),
),
);
}
}
class ContactPage extends StatelessWidget {
const ContactPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Contact')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('Go Home'),
),
),
);
}
}
β Explanation (Very Easy)
πΉ routes: in GoRoute
The routes: parameter defines child routes (nested routes).
πΉ Structure in this program
,, code-block:: bash
/ β HomePage /about β AboutPage (child) /contact β ContactPage (child)
πΉ Why use child routes?
Organize large apps easily
Create nested pages under a parent
Cleaner URL structure
β 6.redirect:#
Here is a very simple, clean, beginner-friendly program demonstrating the redirect: parameter in GoRoute using MaterialApp.router().
π No authentication logic
π No complex code
π Only the basic redirect feature
β Simple Program β Using redirect: in GoRoute with MaterialApp.router()
This example redirects:
If user tries to go to /profile,
they get redirected to /login.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
// ---------------------------------------
// GoRouter using redirect:
// ---------------------------------------
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
),
GoRoute(
path: '/login',
builder: (context, state) => const LoginPage(),
),
GoRoute(
path: '/profile',
builder: (context, state) => const ProfilePage(),
// π redirect rule
redirect: (context, state) {
bool isLoggedIn = false; // π for demo only
if (!isLoggedIn) {
return '/login'; // redirect to login page
}
return null; // allow navigation
},
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/profile'),
child: const Text('Go to Profile'),
),
),
);
}
}
class LoginPage extends StatelessWidget {
const LoginPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Login')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('Back to Home'),
),
),
);
}
}
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: const Center(
child: Text('Welcome to your profile!'),
),
);
}
}
β Explanation (Very Easy)
πΉ redirect: (context, state) { β¦ }
This function decides:
β’ Should the user stay on this page?
β’ Or should they be redirected to another page?
πΉ In our example:
bool isLoggedIn = false;
ince the user is βnot logged in,β going to /profile automatically sends them to:
/login
πΉ return null;
Means βno redirect β allow user to enter the page.β