The Wayback Machine - https://web.archive.org/web/20240709142456/https://www.geeksforgeeks.org/page-view-animation-in-flutter/
Open In App

Page View Animation in Flutter

Last Updated : 06 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
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



Similar Reads

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 integrate and implement the page_transition package
6 min read
Flutter - Animation in Route Transition
Routes are simply Pages in Flutter applications. An application often needs to move from one page to another. But to make these transitions smoother, Animations can be used. These animations can be used to curve or tween the Animation object of the PageRouteBuilder class to alter the transition animation. We will discuss them in detail here. Let's
4 min read
Flutter - Physics Simulation in Animation
Physics simulation in Flutter is a beautiful way to Animate components of the flutter app to make it look more realistic and interactive. These can be used to create a range of animations like falling objects due to gravity to making a container seem attached to a spring. In this article, we will explore the same by building a simple application. F
4 min read
Flutter - Radial Hero Animation
A Radial transformation means turning a circular shape into a square shape. In Radial Hero animation the same is done with a Hero. In Flutter, a Hero Widget is used to create a hero animation. A hero in this context refers to a widget that moves in between screens. This is one of the most fundamental types of animation used in the application, espe
3 min read
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 Widget In this article, we will be working with the Animated Builder widget. A
3 min read
Flutter - Lottie Animation
Visualization is an integral part of any application. Animations can highly glorify the UI of an app, but animations can be hectic to implement for an application. This is where the Lottie animation comes in. Lottie is a JSON-based animation file. It can be used both as a network asset and a static asset throughout the platform. In this article, we
3 min read
Flutter - Liquid Swipe Animation
Liquid Swipe animation is used to slide the page like water which show different design and pattern on the screen. It creates a floating state. Liquid Swipe animation is a significantly trending design procedure. Movement can help keep clients inspired by your UI design longer and more motivated to collaborate with content. This method provides the
2 min read
Flutter - Circular reveal Animation
The Circular Reveal Animation in Flutter is inspired by ViewAnimationUtils.createCircularReveal(...). It does exactly what the name suggests, meaning it is used to reveal content generally an image circularly where the circle grows bigger and makes the image visible. In this article, we will implement a simple Circular Reveal Animation through a si
4 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 duration, we will show the loading animation or loading
3 min read
Flutter - Card Flip Animation
Flutter is an open-source UI toolkit by Google. It is the favorite framework of many developers to build cross-platform apps because of its easy-to-learn curve. In this article, we are going to code a card flip animation in Flutter. Card flip animation is used in many places where you have to show additional information related to the card item. Ap
2 min read
Article Tags :
three90RightbarBannerImg