Communication between widgets using InheritedWidget (Flutter)

I started using flutter and came accross the problem where I need communication between two widgets of my application.  I found InheritedWidget  easy and serve this purpose.

Let us make the problem simple.

  1. Button in App footer (MyFancyButton ) :On click on button increment the counter
  2. Counter widget (MyBox): It is in center of App.

Concept is simple. First create a StatefulWidget (MyApp/MyAppState) class that has count variable and a method (onTap) that increments the variable count.
In the build method of MyAppState return the InheritedWidget  (MyInheritedWidget)

Associate HomePage widget as a child to MyInheritedWidget.

  @override  Widget build(BuildContext context) {
     return new MyInheritedWidget(
      count: count,
      onTap: onTap,
      child: new HomePage(),
    );
  }
Now any change in count variable in State widget will result in rebuilding of child widget wherever MyInheritedWidget.

Complete code:
import 'package:flutter/material.dart';

void main() =>
    runApp(new MaterialApp(title: "Count Click App", home: new MyApp()));

class MyApp extends StatefulWidget {
  @override  MyAppState createState() {
    return new MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  int count = 0;
  void onTap() {
    setState(() {
      count++;
    });
  }

  @override  Widget build(BuildContext context) {
     return new MyInheritedWidget(
      count: count,
      onTap: onTap,
      child: new HomePage(),
    );
  }
}

class MyInheritedWidget extends InheritedWidget {
  final int count;
  final Function onTap;

  const MyInheritedWidget({Key key, this.count, this.onTap, child})
      : super(key: key, child: child);

  @override  bool updateShouldNotify(MyInheritedWidget oldWidget) {
    return oldWidget.count != count;
  }

  static MyInheritedWidget of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(MyInheritedWidget);
  }
}

class HomePage extends StatelessWidget {
  HomePage();

  @override  build(BuildContext context) {
    return new Scaffold(
      appBar:
          new AppBar(backgroundColor: Colors.amber, title: new Text("Count Click Demo")),
      body: new Center(child: new MyBox()),
      persistentFooterButtons: <Widget>[
        new MyFancyButton(),
      ],
    );
  }
}

class MyBox extends StatelessWidget {
  @override  Widget build(BuildContext context) {
    return new Center(
      child: new Text(
        "Counter: " + MyInheritedWidget.of(context).count.toString(),
        style: new TextStyle(fontWeight: FontWeight.w900, fontSize: 34.0),
      ),
    );
  }
}

class MyFancyButton extends StatelessWidget {
  @override  Widget build(BuildContext context) {
    return new RaisedButton(
      onPressed: () {
        MyInheritedWidget.of(context).onTap();
      },
      child: new Text("Click Me"),
      color: Colors.red,
      textColor: Colors.white,
    );
  }
}

4 comments

So when you and your associate agree that getting intercourse toys collectively is one thing you need to do, then you definitely cheap wholesale don’t have to look wherever else aside from Spencer Gifts. And don’t fear; our discreet transport will make certain your nosey neighbors will never know your naughty secrets and techniques. The first providing Fine developed to fill this hole was a vibrator designed to fit snugly contained in the labia for hand-free clitoral stimulation. “I had an thought for a vibrator that could be be} worn throughout penetrative intercourse to assist shut the pleasure hole,” she explains.

Between games, feel free to wander to any of the Soboba Rewards areas around the on line casino, where you'll enjoy pleasant, first-class service. You do not pay a 5% commission whenever you beat the supplier; however, when the supplier plays ace-high, you mechanically push. Casino warfare is a on line casino card game based mostly on the kids's game of War. The game is 바카라사이트 arguably one of the simply understood on line casino card games, but it also has a relatively massive house edge compared to with} other games. Games embody Limit Hold 'Em, No Limit Hold'Em, and Omaha High/Low. Be positive to current your mychoice® card to earn comps and special presents.

Many of the casinos featured right here in fact most of them supply lucrative VIP or loyalty programs. On the other hand, seems that|it appears that} the next has gained widespread favour. Fortunately, casinos similar to LeoVegas Casino count with that possibility. Below you will find a full overview of the best no deposit bonus presents for gamers out of your location. A critical strategy to figuring out a good banking system is a key factor for 메리트카지노 avoiding any potential unpleasant conditions. Online gambling is a critical business, and it’s important to discover a trusted web site that gives an excellent experience.


Emoticon Emoticon