Sub topic 8
Customizing page transitions
This chapter assumes you know Dart async and await, basic Flutter widgets, BuildContext, and Navigator.push and Navigator.pop. A page transition is the visual animation Flutter plays when a route enters or leaves the screen.
Flutter provides platform-appropriate transitions through MaterialPageRoute and CupertinoPageRoute. Use those defaults unless your product needs a distinct visual style. For a custom effect, create a PageRouteBuilder.
How a custom transition works
A PageRouteBuilder creates a route and gives you a transitionsBuilder. Flutter calls this builder during the route animation.
The builder receives:
context: the widget tree context.animation: the new route's animation. It usually moves from0.0to1.0as the page enters.secondaryAnimation: the animation for the route below the new route.child: the page widget being displayed.
The animation value drives effects such as fading, sliding, and scaling.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: HomeScreen()));
}
Route<void> _buildDetailsRoute() {
return PageRouteBuilder<void>(
pageBuilder: (context, animation, secondaryAnimation) {
return const DetailsScreen();
},
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(1.0, 0.0);
const end = Offset.zero;
const curve = Curves.easeOutCubic;
final curvedAnimation = CurvedAnimation(
parent: animation,
curve: curve,
);
return SlideTransition(
position: Tween<Offset>(
begin: begin,
end: end,
).animate(curvedAnimation),
child: child,
);
},
transitionDuration: const Duration(milliseconds: 350),
);
}
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: () {
Navigator.of(context).push(_buildDetailsRoute());
},
child: const Text('Open details'),
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details')),
body: const Center(child: Text('Custom transition')),
);
}
}SlideTransition expects an Animation<Offset>. The tween converts the animation's progress into positions. An offset of (1.0, 0.0) means one full screen width to the right. Offset.zero means the normal position.
You can replace SlideTransition with FadeTransition, ScaleTransition, or a combination of transitions.
Choosing the right route technique
| Technique | Best for | Main trade-off |
|---|---|---|
| `MaterialPageRoute` | Standard Material navigation | Little visual customization |
| `CupertinoPageRoute` | iOS-style navigation | Platform-specific behavior |
| `PageRouteBuilder` | A custom animation for one route | More code to maintain |
| A custom `Page` in Router or `go_router` | Declarative, reusable transitions | Requires router configuration |
flowchart TD
A[Need to navigate] --> B{Default transition is suitable?}
B -->|Yes| C[Use MaterialPageRoute or CupertinoPageRoute]
B -->|No| D{Is the route imperative?}
D -->|Yes| E[Use PageRouteBuilder]
D -->|No| F[Configure a custom Page transition]Common errors and trade-offs
Keep the page widget in the child parameter. Rebuilding the page manually inside transitionsBuilder can reset state and create unnecessary work.
Set a reasonable duration. A transition that is too slow makes navigation feel delayed. A transition that is too fast can appear abrupt.
Consider accessibility. Some users enable reduced motion at the operating-system level. Avoid animations that are essential for understanding the interface, and keep movement subtle when possible.
If several routes share the same animation, extract a reusable route or page class. Copying PageRouteBuilder code across the application makes later changes difficult.
Exercise
Which value should a right-to-left slide transition animate from, and which widget can display it?
Show answer
Animate from Offset(1.0, 0.0) to Offset.zero and use SlideTransition.