Flutter State Management
Flutter State Management
Jaymark A. Yambao
Introduction
Managing state in an application is one of the most important and necessary
process in the life cycle of an application.
Let us consider a simple shopping cart application.
• User will login using their credentials into the application.
• Once user is logged in, the application should persist the logged in user
detail in all the screen.
• Again, when the user selects a product and saved into a cart, the cart
information should persist between the pages until the user checked out
the cart.
• User and their cart information at any instance is called the state of the
application at that instance.
Introduction (cont.)
A state management can be divided into two categories based on the
duration the particular state lasts in an application.
• Ephemeral − Last for a few seconds like the current state of an
animation or a single page like current rating of a
product. Flutter supports its through StatefulWidget.
• app state − Last for entire application like logged in user details, cart
information, etc., Flutter supports its through scoped_model.
Navigation and Routing
In any application, navigating from one page / screen to another defines the work flow of the
application. The way that the navigation of an application is handled is called Routing. Flutter
provides a basic routing class – MaterialPageRoute and two methods - Navigator.push and
Navigator.pop, to define the work flow of an application.
MaterialPageRoute is a widget used to render its UI by replacing the entire screen with a platform
specific animation
Here, builder will accepts a function to build its content by suppling the current context of the application.
flutter: assets: -
Create a new Flutter application in Android studio, product_nav_app assets/appimages/floppy.png -
assets/appimages/iphone.png -
•Copy the assets folder from product_nav_app to product_state_app assets/appimages/laptop.png -
and add assets inside the pubspec.yaml file. assets/appimages/pendrive.png -
assets/appimages/pixel.png -
assets/appimages/tablet.png
•Let us write a method getProducts in the Product class to generate our dummy product
records.
Navigation and Routing
•Let us include our new widget, RatingBox.
Navigation and Routing
•Let us modify our ProductBox widget to work
with our new Product class.
Navigation and Routing
•Let us rewrite our MyHomePage widget to
work with Product model and to list all
products using ListView.
Navigation and Routing
Here, we have used MaterialPageRoute to
navigate to product details page.