Using Scoped Model in a Multi-Page or Tabbed Application with Dart's Flutter Framework
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:
- IDEA intellij, Visual Studio Code with the Dart/Flutter Plugins, Android Studio or Xcode
- The Flutter SDK on the latest Master Build
- An Android or iOS Emulator or device for testing
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:
- Flutter Website: https://flutter.io/
- Flutter Official Documentation: https://flutter.io/docs/
- Flutter Installation Information: https://flutter.io/get-started/install/
- Flutter GitHub repository: https://github.com/flutter/flutter
- Flutter Dart 2 Preview Information: https://github.com/flutter/flutter/wiki/Trying-the-preview-of-Dart-2-in-Flutter
- Flutter Technical Overview: https://flutter.io/technical-overview/
- Dart Website: https://www.dartlang.org/
- Flutter Awesome GitHub Repository: https://github.com/Solido/awesome-flutter
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.
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.
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.
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:
- Dart Flutter Cross Platform Chat Application Tutorial
- Building a Temperature Conversion Application using Dart's Flutter Framework
- Managing State with Flutter Flux and Building a Crypto Tracker Application with Dart's Flutter Framework
Building a Calculator
- Building a Calculator Layout using Dart's Flutter Framework
- Finishing our Calculator Application with Dart's Flutter Framework
Movie Searcher Application
- Building a Movie Searcher with RxDart and SQLite in Dart's Flutter Framework (Part 1)
- Building a Movie Searcher with RxDart and SQLite in Dart's Flutter Framework (Part 2)
- Building a Movie Searcher with RxDart and SQLite in Dart's Flutter Framework (Part 3, Final)
Minesweeper Game
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 1)
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 2)
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 3)
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 4, Final)
Weather Application
- Building a Weather Application with Dart's Flutter Framework (Part 1, Handling Complex JSON with Built Code Generation)
- Building a Weather Application with Dart's Flutter Framework (Part 2, Creating a Repository and Model)
- Building a Weather Application with Dart's Flutter Framework (Part 3, RxCommand (RxDart) and Adding an Inherited Widget)
- Building a Weather Application with Dart's Flutter Framework (Part 4, Using RxWidget to Build a Reactive User Interface)
- Localize and Internationalize Applications with Intl and the Flutter SDK in Dart's Flutter Framework
Curriculum
- Building a Multi-Page Application with Dart's Flutter Mobile Framework
- Making Http requests and Using Json in Dart's Flutter Framework
- Building Dynamic Lists with Streams in Dart's Flutter Framework
- Using GridView, Tabs, and Steppers in Dart's Flutter Framework
- Using Global Keys to get State and Validate Input in Dart's Flutter Framework
- The Basics of Animation with Dart's Flutter Framework
- Advanced Physics Based Animations in Dart's Flutter Framework
- Building a Drag and Drop Application with Dart's Flutter Framework
- Building a Hero Animation and an Application Drawer in Dart's Flutter Framework
- Using Inherited Widgets and Gesture Detectors in Dart's Flutter Framework
- Using Gradients, Fractional Offsets, Page Views and Other Widgets in Dart's Flutter Framework
- Making use of Shared Preferences, Flex Widgets and Dismissibles with Dart's Flutter framework
- Using the Different Style Widgets and Properties in Dart's Flutter Framework
- Composing Animations and Chaining Animations in Dart's Flutter Framework
- Building a Countdown Timer with a Custom Painter and Animations in Dart's Flutter Framework
- Reading and Writing Data and Files with Path Provider using Dart's Flutter Framework
- Exploring Webviews and the Url Launcher Plugin in Dart's Flutter Framework
- Adding a Real-time Database to a Flutter application with Firebase
- Building a List in Redux with Dart's Flutter Framework
- Authenticating Guest Users for Firebase using Dart's Flutter Framework
- How to Monetize Your Flutter Applications Using Admob
- Using Geolocator to Communicate with the GPS and Build a Map in Dart's Flutter Framework
- Managing the App Life Cycle and the Screen Orientation in Dart's Flutter Framework
- Making use of General Utility Libraries for Dart's Flutter Framework
- Interfacing with Websockets and Streams in Dart's Flutter Framework
- Playing Local, Network and YouTube Videos with the Video Player Plugin in Dart's Flutter Framework
- Building Custom Scroll Physics and Simulations with Dart's Flutter Framework
- Making Dynamic Layouts with Slivers in Dart's Flutter Framework
- Building a Sketch Application by using Custom Painters in Dart's Flutter Framework
- Using Dart Isolates, Dependency Injection and Future Builders in Dart's Flutter Framework
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.
Uncomplicated article. I learned a lot of interesting and cognitive. I'm screwed up with you, I'll be glad to reciprocal subscription))