Using Scoped Model in a Multi-Page or Tabbed Application with 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 interface Scoped Model with multiple pages
  • You will learn how to force a UI update from Scoped Model
  • You will learn how to properly structure a model
  • You will learn about listeners in Scoped Model
  • You will learn how to use Routes and Tabs

Requirements

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

Required Knowledge

  • A basic state management
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • A simple understanding of data modeling

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 build an application that makes use of the Scoped Model State Management plugin. We advance our knowledge of this plugin by looking at how it should be used in situations where we have multiple pages and widgets which need access to the model. The application has a text box which reads data into the list of the model. We then are able to display and delete this data from the other page using ListTile widgets.

Outline for this Tutorial
Item 1: Brief Overview of Scoped Model

The Scoped Model Pattern originates from Google's Fuchsia OS repository. This pattern is used in many of the Native Dart widgets and applications in that repository. Scoped model extends the Listenable Widget and the Inherited Widget which is what makes it very powerful for state management in Flutter.

scoped-extends.jpg

Here you can see the two extensions that are inside of the Scoped Model Plugin. The Listenable Widget is an Object that maintains a list of Listeners. In this case, we are able to use this feature to schedule a microtask which leads flutter refreshing the widget tree. The Inherited Widget is what allows the ModelFinder and ScopedModelDescendant Widgets to access the model from any widget below where the ScopedModel is defined.

Item 2: Notifying the Listeners Manually

As mentioned above, the Listenable Widget gives Scoped Model the ability to schedule a microtask. We are able to manually do this by calling the notifyListeners function from our model. This function is extremely important when we are working with stateless widgets and widgets that can not call setState directly. It is also redundant when called from a widget that has access to the setState function.

model.jpg

Here is the AppModel that was using in our application. The deleteItem function calls to notifyListeners but the addItem function does not. This is because the deleteItem function is called from a stateless widget where as the addItem function is called from a stateful widget.

Item 3: Scoped Model with Multiple Pages

While the Scoped Model pattern is fairly intuitive in many cases, there are some that can be confusing. One of these cases is in the case where you have an application that uses multiple pages, tabs or slivers. This can be fairly tricky because it can be difficult to find the appropriate places to attach the scoped model widget to. There is a balance between optimization of performance and optimization of access that needs to be considered in these cases.

scoped-tree.jpg

Imagine that this image represents the flutter widget tree for our application. If the yellow circle is our scoped model. If we want to gain access to the model from all of the blue colored widgets the optimal place to put the model is at the base of our root widget; the green circle. This would give every widget in the application access to the same instance of this model.

The source code for this project can be found here

Video Tutorial

Related Videos

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:  

Thank you for your contribution @tensor. It has been reviewed and scored.


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

Thank you as always for moderating my contributions.

Hey @tensor

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.

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

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

it is very helpfull Content, thanks for shaire..

Nicely composed. All details are easily illustrated and understandable. Thanks for sharing

Glad you enjoyed the contribution. Thank you for the compliments.

Loading...

Uncomplicated article. I learned a lot of interesting and cognitive. I'm screwed up with you, I'll be glad to reciprocal subscription))

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64231.88
ETH 3128.59
USDT 1.00
SBD 3.95