Created
March 5, 2019 12:05
-
-
Save mrmitew/4a4f24b8544e4898b4844e46f0880c53 to your computer and use it in GitHub Desktop.
Inherited Stateful Widget
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 MyInherited extends StatefulWidget { | |
final Widget child; | |
MyInherited({Key key, this.child}) : super(key: key); | |
@override | |
MyInheritedState createState() => new MyInheritedState(); | |
static MyInheritedState of(BuildContext context) { | |
return (context.inheritFromWidgetOfExactType(_MyInherited) as _MyInherited) | |
.data; | |
} | |
} | |
class MyInheritedState extends State<MyInherited> { | |
String _myField; | |
String get myField => _myField; | |
void setNewFieldValue(String newValue) { | |
setState(() { | |
_myField = newValue; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new _MyInherited( | |
data: this, | |
child: widget.child, | |
); | |
} | |
} | |
class _MyInherited extends InheritedWidget { | |
final MyInheritedState data; | |
_MyInherited({Key key, this.data, Widget child}) | |
: super(key: key, child: child); | |
@override | |
bool updateShouldNotify(_MyInherited old) { | |
// TODO | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment