How To Implement SCOPED MODEL In Flutter

Introduction

 
This article explains how the state is managed in Flutter. There are methods you already know about, i.e., provider, Inherited Widget, Redux, etc. Scoped Model is also a state management technique. Scoped Model is somehow advanced compared to Inherited Widget and it’s also better in performance as well. Now, let’s see the Scoped Model implementation in Flutter in detail.
 
Plugin Required
 
scoped_model: ^1.0.1
 

Steps 

 
Step 1
 
The first and most basic step is to create a new application in Flutter. If you are a beginner in Flutter, then you can check my blog Create a first app in Flutter. I have created an app named as “flutter_scoped_model”.
 
Step 2
 
Now, you can see that we have a counter app by default. Our purpose here is to make the same app using the inherited widget.
 
Step 3
 
Add dependency in pubspec.yaml file as below.
  1. dependencies:  
  2.  flutter:  
  3.    sdk: flutter  
  4.  cupertino_icons: ^0.1.2  
  5.  scoped_model: ^1.0.1  
Step 4
 
Now, create a new file named as counter_model.dart. In this file, we will implement a class named as CounterModel which contains count variable and increment() which increment count variable the notifyListeners() will notify to all the connected listeners when increment will be occurred. Following is the programming implementation of that.
  1. import 'package:scoped_model/scoped_model.dart';  
  2.    
  3. class CounterModel extends Model {  
  4.  int count = 0;  
  5.  int get counts => count;  
  6.  increment() {  
  7.    count++;  
  8.    notifyListeners();  
  9.  }  
  10. }  
Step 5
 
Now, in main.dart, we will define the scoped model scope using ScopedModel widget and all the child elements have access to scoped model class using ScopedModelDescedant<Dynamic> widget. Following is the programming implementation of that.
  1. import 'package:flutter/material.dart';  
  2. import 'package:flutter_scoped_model/counter_model.dart';  
  3. import 'package:scoped_model/scoped_model.dart';  
  4.    
  5. void main() => runApp(MyApp());  
  6.    
  7. class MyApp extends StatelessWidget {  
  8.  @override  
  9.  Widget build(BuildContext context) {  
  10.    return MaterialApp(  
  11.      title: 'Flutter Demo',  
  12.      theme: ThemeData(  
  13.        primarySwatch: Colors.blue,  
  14.      ),  
  15.      home: MyHomePage(title: 'Flutter Demo Home Page'),  
  16.    );  
  17.  }  
  18. }  
  19.    
  20. class MyHomePage extends StatefulWidget {  
  21.  MyHomePage({Key key, this.title}) : super(key: key);  
  22.  final String title;  
  23.  @override  
  24.  _MyHomePageState createState() => _MyHomePageState();  
  25. }  
  26.    
  27. class _MyHomePageState extends State<MyHomePage> {  
  28.  @override  
  29.  Widget build(BuildContext context) {  
  30.    return ScopedModel(  
  31.      model: CounterModel(),  
  32.      child: Scaffold(  
  33.        appBar: AppBar(  
  34.          title: Text(widget.title),  
  35.        ),  
  36.        body: Center(  
  37.          child: Column(  
  38.            mainAxisAlignment: MainAxisAlignment.center,  
  39.            children: <Widget>[  
  40.              Text(  
  41.                'You have pushed the button this many times:',  
  42.              ),  
  43.              ScopedModelDescendant<CounterModel>(  
  44.                builder: (context, _, model) => Text(  
  45.                  '${model.count}',  
  46.                  style: Theme.of(context).textTheme.display1,  
  47.                ),  
  48.              ),  
  49.            ],  
  50.          ),  
  51.        ),  
  52.        floatingActionButton: ScopedModelDescendant<CounterModel>(  
  53.          builder: (context, _, model) => FloatingActionButton(  
  54.            onPressed: (){  
  55.              model.increment();  
  56.            },  
  57.            tooltip: 'Increment',  
  58.            child: Icon(Icons.add),  
  59.          ),  
  60.        ),  
  61.      ),  
  62.    );  
  63.  }  
  64. }  
Hurry…. Run the app and test it on emulator/simulator or device :)))
 

Conclusion

 
State Management is one of the key parts of performance improvement of the app and Scoped Model is one of the approaches that offer better performance than the inherited widget.


Similar Articles