Persisting User Interface State and Building Bottom Navigation Bars in Dart's Flutter Framework

in #utopian-io6 years ago

flutter-logo.jpg

Repository

https://github.com/flutter/flutter

What Will I Learn?

  • You will learn how to persist User Interface State in Flutter
  • You will learn about the Page Storage widget
  • You will learn about the Page Storage Bucket key value storage widget
  • You will learn how to use the Page Storage Key widgets to automate state storage
  • You will learn about Bottom Navigation Bar widgets
  • You will learn about Value Keys and Bottom Navigation Items

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A little understanding of key-value storage
  • Some understanding of tree objects
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Flutter Video tutorial, we take a look at how we can build a BottomNavigationBar widget. We also look at the various ways that we can persist state from page to page after that page has been removed from the widget tree. This includes looking at PageStorage widgets, PageStorageKey widgets, ValueKey widgets, and the PageStorageBucket key-value storage widget. We also look at BottomNavigationBarItems and how we can persist ExpansionTile state.

Outline for this Tutorial
Navigating from the Bottom to the Top

Flutter offers multiple ways to navigate from one widget tree to another. You can use routers, tab bars, and buttons and you can also use drawers and menus. In this example we take a look at the BottomNavigationBar widget as the main source of navigation.

bottom-nav.png

You can see the basic setup of the BottomNavigationBar widget. We have a list of BottomNavigationBarItem widgets in the items property which defines the aesthetics of our BottomNavigationBar. We are able to connect this BottomNavigationBar to a list of pages via the currentIndex property and the onTap callback function. This onTap callback function is what gives the BottomNavigationBar the touch functionality and allows the user to traverse from one page to the next.

Storing State in a Bucket

Due to the way that navigation works in Flutter, when one tree is removed and replaced in the widget tree, it needs to be rebuilt. During this rebuild process, the state of this widget is lost because the new widget is assigned a different runtime then the first. Luckily, Flutter offers multiple ways for us to save the state of this widget tree and inject it into the newly rebuilt tree. The lowest level and the most flexible persistence widgets are the PageStorage and PageStorageBucket key-value storage.

expansion-bucket.png

This piece of code shows how we can read and write from the PageStorageBucket widget through the PageStorage widget. This PageStorage acts like an inherited widget; we can access it using the build context from anywhere that is below its declaration on the widget tree. The two methods, writeState and readState, let us set and get the data we want to save in the PageStorageBucket through the PageStorage widget. We can associate this data in the key-value storage by using a ValueKey as the key and the state as the value.

Automating the State Bucket

Sometimes the PageStorageBucket way of doing things is a bit too extravagant and complex for what we want to achieve in our application. In these cases, we can use a widget called the PageStorageKey. The PageStorageKey is a special type of key that allows us to associate the User Interface state with a PageStorageBucket in a less direct and more automated way.

expansion-key.png

In this code, we are creating multiple unique PageStorageKey widgets for each of our ExpansionTile widgets. This allows us to then save the boolean state of the expansion tile even if the entire page falls out of scope. This works because the PageStorageKey is referenced directly in the widget tree. Even if a new instance of this widget is inflated by the user, the application knows that this widget is attached to the associated state.

The source code for this project can be found here

Video Tutorial

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Sort:  

Again, another super video tutorial @tensor.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for moderating my contributions.

I have had learning Flutter on my todo list for a while now. I love how it feels familiar enough as a front-end developer to pick up, but also shares similarities in how React Native allows you to build native mobile applications. I dare say it, Flutter looks a lot easier to learn than React Native was. Nice work on the tutorial, I've bookmarked it so I can follow along later on.

That's good to hear.

Here are a few things worth noting, Flutter does share some similarities with React Native and it is extremely easy to learn (especially if you already know a c-like language). That being said, unlike RN, flutter actually compiles completely into native bytecode for the target platform. This goes with the whole "build once run anywhere" philosophy of Flutter.

Hey @tensor
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.029
BTC 74993.05
ETH 2818.21
USDT 1.00
SBD 2.50