Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
The application model is shared by an InheritedWidget: ModelBinding.
ModelBinding has-a Model. All of the descendants of ModelBinding can get
the model with Model.of(context) and in so doing, register themselves
as dependents which will be rebuilt when ModelBinding is rebuilt.
To change the shared model, the ModelBinding must be rebuilt with a new Model.
Descendants of ModelBinding must be provided with a callback that rebuilds
ModelBinding's stateful parent.
import 'package:flutter/material.dart';
class Model {
const Model({ this.value = 0 });
final int value;
bool operator ==(Object other) {
if (identical(this, other))
return true;
if (other.runtimeType != runtimeType)
return false;
final Model otherModel = other;
return otherModel.value == value;
int get hashCode => value.hashCode;
static Model of(BuildContext context) {
final ModelBinding binding = context.inheritFromWidgetOfExactType(ModelBinding);
return binding.model;
class ModelBinding extends InheritedWidget {
Key key,
this.model = const Model(),
Widget child,
}) : assert(model != null), super(key: key, child: child);
final Model model;
bool updateShouldNotify(ModelBinding oldWidget) => model != oldWidget.model;
class ViewController extends StatelessWidget {
const ViewController({ Key key, this.updateModel }) : super(key: key);
final ValueChanged<Model> updateModel;
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
updateModel(Model(value: Model.of(context).value + 1));
child: Text('Hello World ${Model.of(context).value}'),
class App extends StatefulWidget {
_AppState createState() => _AppState();
class _AppState extends State<App> {
Model currentModel = Model();
void updateModel(Model newModel) {
if (newModel != currentModel) {
setState(() {
currentModel = newModel;
Widget build(BuildContext context) {
return MaterialApp(
home: ModelBinding(
model: currentModel,
child: Scaffold(
body: Center(
child: ViewController(updateModel: updateModel),
void main() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment