Created
July 31, 2018 21:46
-
-
Save HansMuller/a983d6e079be90947209dade80c46f3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class ABCModel extends InheritedModel<String> { | |
ABCModel({ | |
Key key, | |
this.a, | |
this.b, | |
this.c, | |
Widget child, | |
}) : super(key: key, child: child); | |
final int a; | |
final int b; | |
final int c; | |
@override | |
bool updateShouldNotify(ABCModel old) { | |
return a != old.a || b != old.b || c != old.c; | |
} | |
@override | |
bool updateShouldNotifyDependent(ABCModel old, Set<String> dependencies) { | |
return (a != old.a && dependencies.contains('a')) | |
|| (b != old.b && dependencies.contains('b')) | |
|| (c != old.c && dependencies.contains('c')); | |
} | |
static ABCModel of(BuildContext context, { String token }) { | |
return context.inheritFromWidgetOfExactType(ABCModel, token: token); | |
} | |
} | |
class ShowABCField extends StatefulWidget { | |
ShowABCField({ Key key, this.fieldName }) : super(key: key); | |
final String fieldName; | |
_ShowABCFieldState createState() => new _ShowABCFieldState(); | |
} | |
class _ShowABCFieldState extends State<ShowABCField> { | |
int _buildCount = 0; | |
@override | |
Widget build(BuildContext context) { | |
final ABCModel abc = ABCModel.of(context, token: widget.fieldName); | |
final int value = widget.fieldName == 'a' ? abc.a : (widget.fieldName == 'b' ? abc.b : abc.c); | |
return new Padding( | |
padding: const EdgeInsets.symmetric(vertical: 8.0), | |
child: new Text('${widget.fieldName} : $value [${_buildCount++}]', style: Theme.of(context).textTheme.title), | |
); | |
} | |
} | |
class ABCPage extends StatefulWidget { | |
@override | |
_ABCPageState createState() => new _ABCPageState(); | |
} | |
class _ABCPageState extends State<ABCPage> { | |
int _a = 0; | |
int _b = 1; | |
int _c = 2; | |
@override | |
Widget build(BuildContext context) { | |
final Widget showA = new ShowABCField(fieldName: 'a'); | |
final Widget showB = new ShowABCField(fieldName: 'b'); | |
final Widget showC = new ShowABCField(fieldName: 'c'); | |
return new Scaffold( | |
appBar: new AppBar(title: const Text('InheritedModel')), | |
body: new StatefulBuilder( | |
builder: (BuildContext context, StateSetter setState) { | |
return new ABCModel( | |
a: _a, | |
b: _b, | |
c: _c, | |
child: new Center( | |
child: new Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
showA, | |
showB, | |
showC, | |
const SizedBox(height: 24.0), | |
new RaisedButton( | |
child: const Text('Increment a'), | |
onPressed: () { | |
setState(() { _a += 1; }); | |
}, | |
), | |
new RaisedButton( | |
child: const Text('Increment b'), | |
onPressed: () { | |
setState(() { _b += 1; }); | |
}, | |
), | |
new RaisedButton( | |
child: const Text('Increment c'), | |
onPressed: () { | |
setState(() { _c += 1; }); | |
}, | |
), | |
], | |
), | |
), | |
); | |
}, | |
), | |
); | |
} | |
} | |
void main() { | |
runApp( | |
new MaterialApp( | |
home: new ABCPage(), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment