Last active
August 10, 2021 21:52
-
-
Save funwithflutter/4f148f6678ab39a040e407473ce20f82 to your computer and use it in GitHub Desktop.
A simple example demonstrating how to animate page transitions in Flutter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
title: 'Animating Route Transitions', | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
void _normalTransitionPressed() { | |
Navigator.of(context) | |
.push(MaterialPageRoute(builder: (_) => const SecondPage())); | |
} | |
void _customTransitionPressed() { | |
Navigator.of(context).push(_createRoute()); | |
} | |
/// To create a custom animation when doing a page transition, you need | |
/// to define a PageRouteBuilder | |
Route _createRoute() { | |
return PageRouteBuilder( | |
transitionDuration: const Duration(milliseconds: 300), | |
pageBuilder: (context, animation, secondaryAnimation) => | |
const SecondPage(), | |
transitionsBuilder: (context, animation, secondaryAnimation, child) { | |
// This can be any built in or custom transition. | |
// Or simply return the widget without doing any animation. | |
// | |
// Try to change this to a [SlideTransition] | |
return ScaleTransition( | |
scale: animation, | |
child: child, | |
); | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Home Page', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
ElevatedButton( | |
onPressed: _normalTransitionPressed, | |
child: const Text('Normal Transition'), | |
), | |
ElevatedButton( | |
onPressed: _customTransitionPressed, | |
child: const Text('Custom Scale Transition'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class SecondPage extends StatelessWidget { | |
const SecondPage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Second Page', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
ElevatedButton( | |
onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
child: const Text('Back'), | |
) | |
], | |
), | |
), | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment