Flutter PageView Widget – Flutter Tutorial



Flutter’s PageView Widget is a versatile and dynamic tool for creating swipeable, horizontally scrollable pages in your mobile applications. With its seamless customization options and smooth page transitions, it significantly enhances user experience.

Flutter PageView

Import the necessary packages:

import 'package:flutter/material.dart';

Flutter PageView Widget

PageView(

        controller: _myController,
        scrollDirection:
            Axis.horizontal, // page horizontaly scroll
        pageSnapping:
            false, 
        children: [
         
          Container(
            color: Colors.amber,
            child: const Center(child: Text("Page 1")),
          ),
          Container(
            color: Colors.red,
            child: const Center(child: Text("Page 2")),
          ),
          Container(
            color: Colors.blue,
            child: const Center(child: Text("Page 3")),
          ),

        ],
      ),

Create a PageView controller

 PageController _myController = PageController(
    initialPage: 0,
  );

  @override
  void dispose() {
    _myController.dispose();
    super.dispose();
  }

Flutter PageView Widget Example

PageView Widget

Tutorial source code:

import 'package:flutter/material.dart';

main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstPage(),
    );
  }
}

class FirstPage extends StatefulWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  State<FirstPage> createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  // HEy guys welcome to my channel....
  // IN this tutorial we discus...about PageView Widget... let's started... ok
  PageController _myController = PageController(
    initialPage: 0,
  );

  @override
  void dispose() {
    _myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Instructive Tech"),
        backgroundColor: Colors.green,
      ),
      body: PageView(
        // now create a controller...
        controller: _myController,
        scrollDirection:
            Axis.horizontal, // page horizontaly show now ....test it...
        pageSnapping:
            false, // see what happen??? you can open half page... interesting...whatever...
        children: [
          // here you add your pages... ok... now i create container for example... ok
          Container(
            color: Colors.amber,
            child: const Center(child: Text("Page 1")),
          ),
          Container(
            color: Colors.red,
            child: const Center(child: Text("Page 2")),
          ),
          Container(
            color: Colors.blue,
            child: const Center(child: Text("Page 3")),
          ),
          // If you like this video please SUBSCRIBE .... Code link in the description...below...
        ],
      ),
    );
  }
}

Flutter pageview animation

A simple example of a PageView animation in Flutter involves creating a PageView with multiple children and a PageController to manage animations. Here’s a step-by-step guide:

Create a StatefulWidget:

class AnimatedPageViewExample extends StatefulWidget {
  @override
  _AnimatedPageViewExampleState createState() => _AnimatedPageViewExampleState();
}

Create the StatefulWidget’s corresponding State class

class _AnimatedPageViewExampleState extends State<AnimatedPageViewExample> {
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 0, viewportFraction: 0.8);
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

Build the PageView and apply animations:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Animated PageView Example')),
      body: Center(
        child: SizedBox(
          height: 200,
          child: PageView.builder(
            controller: _pageController,
            itemBuilder: (context, index) {
              return AnimatedBuilder(
                animation: _pageController,
                builder: (context, child) {
                  double value = 1;
                  if (_pageController.position.haveDimensions) {
                    value = _pageController.page - index;
                    value = (1 - (value.abs() * 0.3)).clamp(0.0, 1.0);
                  }
                  return Center(
                    child: SizedBox(
                      height: Curves.easeOut.transform(value) * 200,
                      width: Curves.easeOut.transform(value) * 300,
                      child: child,
                    ),
                  );
                },
                child: Container(
                  margin: EdgeInsets.all(10),
                  color: index % 2 == 0 ? Colors.blue : Colors.red,
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

Finally, run the app by placing the StatefulWidget as the home property of the MaterialApp:

void main() {
  runApp(MaterialApp(home: AnimatedPageViewExample()));
}

This example demonstrates a basic PageView with animated transitions between pages, altering the size of the page as it becomes the active page.

Read Also, FLutter Login Form Example and Flutter Navigator.

  • Boost WooCommerce Security: Set Up OTP Login in WordPress
    Passwords, while essential for security, can be vulnerable to hacking and breaches. To add an extra layer of protection for your WooCommerce store, consider implementing an OTP (One-Time Password) login. This guide walks you through the process of setting up an OTP login in WordPress for enhanced security. Why Use OTP Login? Enhanced Security: OTPs …

    Read more …

  • How to Create Woocommerce Flutter App for WordPress free
    Absolutely! Let’s break down how to create a WooCommerce Flutter app for your WordPress store. Here’s a comprehensive outline of the process and essential considerations: 1. Setup Your Development Environment 2. Project Structure 3. Dependencies 4. Core Functionality 5. User Interface (UI) and User Experience 6. Additional Features (Optional) Example Code Snippet (Fetching Products) Important …

    Read more …

  • How to Add a Link to Your Facebook Story in 2024
    Facebook Stories have become an integral part of our social media experience, allowing us to share moments and updates with our friends and followers. But did you know that you can also add links to your Facebook Stories? Adding a link to your story can be a powerful way to drive traffic to your website, …

    Read more …

  • Troubleshooting “Failed to download Laravel from dist” Error
    If you encounter the “Failed to download Laravel from dist” error while working with Laravel 10, it is likely due to the absence of the zip extension and unzip/7z commands on your system. This error prevents the successful download of Laravel from the distribution (dist) repository. What causes this error? The error occurs when the …

    Read more …

  • Solutions for “The file is too large for the destination file system” Error on Pendrive
    If you have ever encountered the frustrating error message “The file is too large for the destination file system” when trying to transfer files to your pendrive, don’t worry! There are a few solutions you can try to resolve this issue. 1. Format the Pendrive: One of the easiest ways to fix this error is …

    Read more …

Conclusion

In summary, the Flutter PageView Widget is an indispensable tool for creating engaging mobile applications. Its simplicity, flexibility, and user-friendly design make it an ideal choice for developers looking to add an interactive element to their projects.

Leave a Comment