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
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.
- How to Conduct YouTube Keyword Research Easily?Introduction YouTube is the second largest search engine in the world, making it a valuable platform for content creators and businesses alike. To maximize your visibility and reach on YouTube, it is crucial to conduct proper keyword research. By identifying the right keywords, you can optimize your videos for search and increase your chances of …
- How to upgrade the Dart SDK version in flutterIf you’re a Flutter developer, you know how important it is to stay up-to-date with the latest version of the Dart SDK. Upgrading the Dart SDK ensures that you have access to the latest features, bug fixes, and performance improvements. Step 1: Check Your Current Dart SDK Version Before you begin the upgrade process, it’s …
- Why Do Advertisers Use Testimonials?In the ever-competitive business landscape, nurturing strong connections with your customers remains paramount for enhancing your credibility and positioning yourself as an industry authority. Establishing credibility is the bedrock upon which trust is built, ultimately translating into increased sales. To bolster your company’s credibility, integrating testimonial advertising into your marketing strategy is essential. So, what …
- How To Earn $5 Fast Method!Looking to boost your income by $25 in less than an hour? This isn’t the start of a clichéd sales pitch; it’s about discovering several quick and simple ways to earn $5 online within minutes. For instance, you can make five bucks by completing tasks such as taking brief surveys, participating in short website usability …
- What Does BMF Mean On TikTok? Latest Social Media AcronymsIn the fast-paced world of social media, acronyms are the language of choice, enabling users to communicate swiftly without the need for lengthy phrases. While classics like LOL and OMG are still in use, the younger generation on TikTok has introduced a new acronym into the mix: BMF. So, what exactly does BMF mean, and …
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.