Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types
  • Functions
  • Oops
  • Collections
  • Sets
  • Dart Interview Questions
  • Fluter
  • Android
  • Kotlin
  • Kotlin Android
  • Android with Java
  • Android Studio
Open In App
Next Article:
Page View Animation in Flutter
Next article icon

Page View Animation in Flutter

Last Updated : 06 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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


Next Article
Page View Animation in Flutter

M

ms471841
Improve
Article Tags :
  • Dart
  • Flutter

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences