How To Implement Flutter INHERITED WIDGET

Introduction

 
This article explains how the state is managed in Flutter. There are methods you already know about, such as provider, scoped model, redux, etc. Inherited Widget is also a state management technique. Inherited Widget is fit in some cases where we want to manage a state but in the complex structure, it is not recommended to use. Now, let’s see Inherited Widgets in Flutter in detail.
 

Steps

 
Step 1
 
The first and most basic step is to create a new application in Flutter. If you are a beginner in Flutter, you can check my blog Create a first app in Flutter. I have created an app named as “flutter_inherited_widget”.
 
Step 2
 
Now, you can see that you have a counter app by default. Here, our purpose is to make the same app using the inherited widget.
 
Step 3
 
Now, create a new file named count_provider.dart. In this file, we will implement a class named as Counter which stores count variable and an increment() which increments the count variable. There is another class which inherited widget calls, i.e., CountProvider, which will be accessible to other classes where we need count variable. Following is the programming implementation of that.
  1. import 'package:flutter/widgets.dart';  
  2.    
  3. class CountProvider extends InheritedWidget {  
  4.  final Widget child;  
  5.  final Counter counter;  
  6.  CountProvider({Key key, this.child, this.counter})  
  7.      : super(key: key, child: child);  
  8.    
  9.  static CountProvider of(BuildContext context) {  
  10.    return (context.inheritFromWidgetOfExactType(CountProvider)  
  11.        as CountProvider);  
  12.  }  
  13.    
  14.  @override  
  15.  bool updateShouldNotify(CountProvider oldWidget) {  
  16.    return true;  
  17.  }  
  18. }  
  19.    
  20. class Counter {  
  21.  int count;  
  22.  Counter(this.count);  
  23.  increment() {  
  24.    count++;  
  25.  }  
  26. }  
Step 4
 
Now, in main.dart file, we will access the inherited widget using counterProvider = CountProvider.of(context); following is the programming implementation of that.
  1. import 'package:flutter/material.dart';  
  2. import 'package:flutter_inherited_widget/count_provider.dart';  
  3.    
  4. void main() => runApp(MyApp());  
  5.    
  6. class MyApp extends StatelessWidget {  
  7.  @override  
  8.  Widget build(BuildContext context) {  
  9.    return MaterialApp(  
  10.      theme: ThemeData(  
  11.        primarySwatch: Colors.blue,  
  12.      ),  
  13.      home: CountProvider(  
  14.        counter: Counter(0),  
  15.        child: MyHomePage(),  
  16.      ),  
  17.    );  
  18.  }  
  19. }  
  20.    
  21. class MyHomePage extends StatefulWidget {  
  22.  MyHomePage({Key key, this.title}) : super(key: key);  
  23.  final String title;  
  24.    
  25.  @override  
  26.  _MyHomePageState createState() => _MyHomePageState();  
  27. }  
  28.    
  29. class _MyHomePageState extends State<MyHomePage> {  
  30.  var counterProvider;  
  31.  void _incrementCounter() {  
  32.    setState(() {  
  33.      counterProvider.counter.increment();  
  34.    });  
  35.  }  
  36.    
  37.  @override  
  38.  Widget build(BuildContext context) {  
  39.    counterProvider = CountProvider.of(context);  
  40.    return Scaffold(  
  41.      appBar: AppBar(  
  42.        title: Text('Flutter Inherited Widget'),  
  43.      ),  
  44.      body: Center(  
  45.        child: Column(  
  46.          mainAxisAlignment: MainAxisAlignment.center,  
  47.          children: <Widget>[  
  48.            Text(  
  49.              'You have pushed the button this many times:',  
  50.            ),  
  51.            Text(  
  52.              '${counterProvider.counter.count}',  
  53.              style: Theme.of(context).textTheme.display1,  
  54.            ),  
  55.          ],  
  56.        ),  
  57.      ),  
  58.      floatingActionButton: FloatingActionButton(  
  59.        onPressed: _incrementCounter,  
  60.        tooltip: 'Increment',  
  61.        child: Icon(Icons.add),  
  62.      ),  
  63.    );  
  64.  }  
  65. }  
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 Inherited Widget is one of the approaches for it.


Similar Articles