Sub topic 13
Custom pages and transitions
This chapter assumes you know Dart null safety, Flutter widgets, and basic Navigator navigation. It also assumes that you have configured go_router and are using MaterialApp.router.
Choosing `builder` or `pageBuilder`
A GoRoute can create a screen in two common ways:
| Option | Use it when | Result |
|---|---|---|
| `builder` | You need to return the screen widget | go_router supplies the default page behavior |
| `pageBuilder` | You need to control the page object or transition | You return a `Page`, such as `CustomTransitionPage` |
A widget describes the screen's content. A Page describes how that content enters the navigator. Use pageBuilder when the route needs a custom animation, a different page key, or page-level settings.
Building a fade and slide transition
The following complete example gives the /details route a fade-and-slide animation:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final GoRouter router = GoRouter(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/details',
pageBuilder: (context, state) {
return CustomTransitionPage<void>(
key: state.pageKey,
child: const DetailsScreen(),
transitionsBuilder:
(context, animation, secondaryAnimation, child) {
final Animation<double> curved = CurvedAnimation(
parent: animation,
curve: Curves.easeOut,
);
final Animation<Offset> position = Tween<Offset>(
begin: const Offset(0.15, 0),
end: Offset.zero,
).animate(curved);
return FadeTransition(
opacity: curved,
child: SlideTransition(
position: position,
child: child,
),
);
},
);
},
),
],
);
void main() {
runApp(const MyApp());
}
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 Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/details'),
child: const Text('Open details'),
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Details')),
);
}
}animation moves from zero to one while the page appears. The CurvedAnimation changes the timing. The Tween changes the value from a starting offset to the final position. Flutter automatically uses the reverse animation when the page is removed.
The secondaryAnimation represents the route that is moving away underneath the new page. Many transitions do not need it, but it is useful when both pages should animate together.
Common transition errors
- Returning the screen widget directly from
pageBuilderis invalid. Return aPage. - Forgetting the
childin the transition tree can remove the screen from the result. - Combining too many large animations can make navigation feel slow or distracting.
- Using a transition that conflicts with platform conventions can make Android and iOS apps feel inconsistent.
- A transition does not replace route state. It only controls how the page is displayed.
Exercise
Which callback should you use when a go_router route needs a custom transition, and what must it return?
Show answer
Use pageBuilder. It must return a Page, commonly a CustomTransitionPage whose child is the screen widget.