Mobile apps are ever changing in today's world and the ability to swipe between different pages and widgets has become one of it's most important aspects. Flutter provides a lot widgets to make many complex UI and to add scrolling pages to our app, we can use PageView() widget. This widget is so easy to implement and control. Without causing any further delay, let's start the tutorial.
Take a look at the screenshot below to understand the UI that we are gonna make in this tutorial.
The UI contains a PageView as the main element in the body of the Scaffold. The indigo color that you see in the middle is nothing else but a full sized Container which is a part of the Pageview(), I have three Containers each having different colors. You can place your screens in place of these Containers.
If you see the app bar, there are two arrows that can be used to change the pages. The video for the same is now uploaded on The Growing Developer youtube channel. I am embedding the video down below for your convenience.
PageView can be used at any place where you want different scrollable elements.In my case I am using the PageView directly inside the body parameter of the Scaffold widget. Copy the below code in your Scaffold's body parameter and I'll explain it in detail.
PageView(
pageSnapping: true,
controller: pageController,
onPageChanged: (index) {
setState(() {
pageChanged = index;
});
print(pageChanged);
},
children: [
Container(
color: Colors.indigo,
),
Container(
color: Colors.red,
),
Container(
color: Colors.brown,
),
],
),
The PageController can be instantiated as other objects like
PageController pageController = PageController();
This pageController was then passed to the controller property of the PageView(). To programmatically change the pages of the PageView we can use the animateToPage or animateTo methodsd of the pageController.
This method takes up three parameters :
AppBar(title: Text('PageView Demo'),
actions: [
IconButton(icon: Icon(Icons.arrow_back_ios), onPressed: (){
pageController.animateToPage(--pageChanged, duration: Duration(milliseconds: 250), curve: Curves.bounceInOut);
}),
IconButton(icon: Icon(Icons.arrow_forward_ios), onPressed: (){
pageController.animateToPage(++pageChanged, duration: Duration(milliseconds: 250), curve: Curves.bounceInOut);
}),
],
As you can see that we incremented and decremented the pageChanged value accordingly to change the Page index in our pageView and animateToPage method was used.
That'all for the this tutorial, Hope it helped in some manner and you are able to learn something from this. You can support me by subscribing to my youtube channel. You can also donate the money using the links available in the author section.
Happy Coding !
It's been over 2 years since Bloc was mentioned with respect to State Management in Flutter. While it is now widely used in some of the biggest applic...
Models are the core of the data flow in any of the MVC architecture. Learn how to make models in Flutter/Dart in a professional way....
Your Go-to Guide to decide which database to choose and why....
Let's create a Clean and Materialistic UI Design for Login and see how we can implement google signin using Firebase Auth...
In this video we will learn about fetching data from the Internet. Learn to make API calls from your flutter application...