Page transition animations make moving between screens smooth and clear. They help users understand where they are going in the app.
Page transition animations in Flutter
Introduction
Syntax
Flutter
Navigator.push(context, MaterialPageRoute(builder: (context) => NewPage()));
Use
Navigator.push to go to a new page with a default animation.Wrap the new page in
MaterialPageRoute to get the standard slide animation.Examples
SecondPage onto the stack with a slide animation from right to left.Flutter
Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage()));
Flutter
Navigator.pop(context);
ThirdPage.Flutter
Navigator.push(context, PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => ThirdPage(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeTransition(opacity: animation, child: child);
},
));Sample App
This app has two pages. Pressing the button on the first page slides to the second page. Pressing the button on the second page slides back.
Flutter
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: FirstPage(), ); } } class FirstPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('First Page')), body: Center( child: ElevatedButton( child: Text('Go to Second Page'), onPressed: () { Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage())); }, ), ), ); } } class SecondPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Second Page')), body: Center( child: ElevatedButton( child: Text('Go Back'), onPressed: () { Navigator.pop(context); }, ), ), ); } }
Important Notes
Page transitions help users feel the app is alive and easy to follow.
You can customize animations using PageRouteBuilder for unique effects.
Always test animations on real devices to check smoothness.
Summary
Use Navigator.push and Navigator.pop for page navigation with default animations.
Wrap new pages in MaterialPageRoute for standard slide transitions.
Customize animations with PageRouteBuilder and transition widgets like FadeTransition.
Practice
1. What does
Navigator.push(context, MaterialPageRoute(builder: (context) => NewPage())) do in Flutter?easy
Solution
Step 1: Understand Navigator.push usage
Navigator.pushadds a new page on top of the current stack, showing it.Step 2: Recognize MaterialPageRoute effect
UsingMaterialPageRoutewraps the new page with a default slide animation from right to left.Final Answer:
It navigates toNewPagewith a default slide animation. -> Option AQuick Check:
Navigator.push + MaterialPageRoute = Slide animation [OK]
Hint: Navigator.push with MaterialPageRoute slides new page in [OK]
Common Mistakes:
- Thinking Navigator.push closes current page
- Confusing MaterialPageRoute with dialog display
- Assuming no animation happens
2. Which of the following is the correct syntax to pop the current page in Flutter?
easy
Solution
Step 1: Recall Navigator.pop method
The correct method to remove the top page from the navigation stack isNavigator.pop(context).Step 2: Verify method names
Other options likepopPage,popRoute, andpopScreendo not exist in Flutter's Navigator API.Final Answer:
Navigator.pop(context); -> Option AQuick Check:
Pop current page = Navigator.pop(context) [OK]
Hint: Use Navigator.pop(context) to go back [OK]
Common Mistakes:
- Using non-existent Navigator methods
- Confusing pop with push
- Missing the context parameter
3. What will be the visual effect of this code snippet when navigating to
NewPage()?
Navigator.push(context, PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => NewPage(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeTransition(opacity: animation, child: child);
},
));medium
Solution
Step 1: Analyze transitionsBuilder
ThetransitionsBuilderreturns aFadeTransitionusing the animation's opacity.Step 2: Understand FadeTransition effect
FadeTransitionanimates the opacity from transparent to fully visible, creating a fade-in effect.Final Answer:
The new page fades in smoothly. -> Option DQuick Check:
FadeTransition = fade in animation [OK]
Hint: FadeTransition means page fades in [OK]
Common Mistakes:
- Assuming slide or zoom animation instead of fade
- Ignoring transitionsBuilder parameter
- Confusing PageRouteBuilder with MaterialPageRoute
4. Identify the error in this Flutter code for custom page transition:
Navigator.push(context, PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => NewPage(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return SlideTransition(
position: Tween(begin: Offset(1, 0), end: Offset.zero),
child: child,
);
},
));medium
Solution
Step 1: Check Tween usage in SlideTransition
Thepositionproperty requires anAnimation<Offset>, but Tween alone is not an animation.Step 2: Fix Tween to animate with animation
We must call.animate(animation)on the Tween to convert it into an animation.Final Answer:
Missing.animate(animation)on the Tween. -> Option BQuick Check:
Tween must be animated with .animate(animation) [OK]
Hint: Tween needs .animate(animation) for SlideTransition [OK]
Common Mistakes:
- Using Tween without .animate(animation)
- Confusing SlideTransition properties
- Thinking MaterialPageRoute is required for custom animation
5. You want to create a page transition where the new page slides in from the bottom and fades in simultaneously. Which code snippet correctly achieves this?
hard
Solution
Step 1: Understand combined animations
To combine slide and fade, wrapFadeTransitioninsideSlideTransitionor vice versa, both animated by the same animation.Step 2: Check code correctness
Navigator.push(context, PageRouteBuilder( pageBuilder: (context, animation, secondaryAnimation) => NewPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { final slide = Tween(begin: Offset(0, 1), end: Offset.zero).animate(animation); final fade = Tween(begin: 0.0, end: 1.0).animate(animation); return SlideTransition( position: slide, child: FadeTransition(opacity: fade, child: child), ); }, )); correctly creates two Tweens animated byanimationand nestsFadeTransitioninsideSlideTransition.Final Answer:
Creates a page transition where the new page slides in from the bottom and fades in simultaneously. -> Option CQuick Check:
Combine SlideTransition + FadeTransition with animated Tweens [OK]
Hint: Nest FadeTransition inside SlideTransition with animated Tweens [OK]
Common Mistakes:
- Using only one animation type instead of both
- Not animating Tweens properly
- Using MaterialPageRoute for custom animations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
