Page View Animation in Flutter
Last Updated : 06 Apr, 2022
Page View is a list that works page by page. In this article, we will gonna do How to Animate the Page when sliding. A sample video is given below to get an idea about what we are going to do in this article.
We will use the Transform widget to animate the page.
Syntax
Creating a Page controller that is used to control pages and listen to swipes.
Dart // page controller instance PageController controller = PageController();
Creating a Variable currentPageValue used to set the number of the selected pages.
Dart // variable to store // current value of page var currentPageValue = 0.0;
Adding Listener to the controller to change the selected page index when the page is changed.
Dart controller.addListener(() { // setState method to // rebuild the widget setState(() { currentPageValue = controller.page; }); });
Create Page list content in a List Variable.
Dart List PageViewItem = [ // item1,item2,item3 ]
Create a sample Page, pageno, and color as parameters so that we can change every page's text and color.
Dart Widget page(var pageno, Color color) { return Container( width: double.infinity, height: double.infinity, color: color, child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon( Icons.pages, color: Colors.white, ), Text("${pageno}, Swipe Right or left"), Icon(Icons.arrow_right, color: Colors.white), ], ), ); }
Building the Page View
Dart PageView.builder( itemCount: pageViewItem.length, scrollDirection: Axis.horizontal, controller: controller, itemBuilder: (context, position) { return Transform( transform: Matrix4.identity() ..rotateX(currentPageValue - position), child: pageViewItem[position], ); }),
Note:
Position values goes between 0 1 2, currentPageValue value goes between 0 and 1 decimals.
Examples 0.255,1.897,2.232
Current Position value - position will return decimal numbers Between 0 and 1 negative rotate left, positive rotate right.
Code Examples
Animation using RotateX
Dart import 'package:flutter/material.dart'; void main() { runApp(PageviewAnimation()); } class PageviewAnimation extends StatefulWidget { PageviewAnimation({Key? key}) : super(key: key); @override State<PageviewAnimation> createState() => _PageviewAnimationState(); } class _PageviewAnimationState extends State<PageviewAnimation> { PageController controller = PageController(); static dynamic currentPageValue = 0.0; List pageViewItem = [ page(currentPageValue, Colors.tealAccent), page(2, Colors.amber), page(3, Colors.cyan) ]; @override void initState() { super.initState(); controller.addListener(() { setState(() { currentPageValue = controller.page; }); }); } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: Text("Page View Animation 1"), ), body: PageView.builder( itemCount: pageViewItem.length, scrollDirection: Axis.horizontal, controller: controller, itemBuilder: (context, position) { return Transform( transform: Matrix4.identity() ..rotateX(currentPageValue - position), child: pageViewItem[position], ); }), ), ); } } Widget page(var pageno, Color color) { return Container( width: double.infinity, height: double.infinity, color: color, child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon( Icons.pages, color: Colors.white, ), Text("${pageno}, Swipe Right or left"), Icon(Icons.arrow_right, color: Colors.white), ], ), ); }
Output
Animation using RotateZ
Dart import 'package:flutter/material.dart'; void main() { runApp(PageviewAnimation()); class PageviewAnimation extends StatefulWidget { PageviewAnimation({Key? key}) : super(key: key); @override State<PageviewAnimation> createState() => _PageviewAnimationState(); } class _PageviewAnimationState extends State<PageviewAnimation> { PageController controller = PageController(); static dynamic currentPageValue = 0.0; // list of pages List pageViewItem = [ page(currentPageValue, Colors.tealAccent), page(currentPageValue, Colors.amber), page(currentPageValue, Colors.cyan) ]; @override void initState() { super.initState(); controller.addListener(() { setState(() { currentPageValue = controller.page; }); }); } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: Text("Page View Animation 1"), ), // PageView builder builds the page. body: PageView.builder( itemCount: pageViewItem.length, scrollDirection: Axis.horizontal, controller: controller, itemBuilder: (context, position) {\ // Transform using for animation return Transform( transform: Matrix4.identity() ..rotateZ(currentPageValue - position), child: pageViewItem[position], ); }), ), ); } } // this widget makes the page Widget page(var pageno, Color color) { return Container( width: double.infinity, height: double.infinity, color: color, child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon( Icons.pages, color: Colors.white, ), Text("${pageno}, Swipe Right or left"), Icon(Icons.arrow_right, color: Colors.white), ], ), ); }
Output
Similar Reads
Flutter - Hinge Animation Animations are a big part of the Flutter application. It makes an app sweet to the eyes and equally user-friendly. In this article, we will discuss in detail the Hinge animations. In Flutter there are two ways to work with animations namely:A pub packageAnimated Builder WidgetIn this article, we wil
4 min read
Rive animations in Flutter Rive is a very useful animation tool that can create beautiful animations and we can add these in our Application. In flutter, we can add animations by writing so many lines of code but this is not a good practice for a developer. Instead of writing lines of code to create animation, we can create o
2 min read
Flutter - Wave Animation In this article, we are going to make a Flutter application that demonstrates the creation of a dynamic wave animation. This animation simulates the motion of water waves, creating an engaging visual effect that can be used in various Flutter applications to add styling effects. A sample video is gi
5 min read
Flutter - Loading Animation Widget In every mobile application, there is a loading animation with different colors and styles, basically, we use the loading animation when we are waiting for something. Like if we are fetching the data from the database then we have to wait for some time until the data is not fetched. So in this durat
3 min read
Flutter - Page Transition Animation In Flutter, we can easily animate or handle the page transitions by using the page_transition package. The page_transition package is a valuable addition to the Flutter package, offering a variety of transition effects that can elevate the app's UI. In this article, we are going to explore how to in
6 min read