Skip to content

Instantly share code, notes, and snippets.

@mrmitew
Created March 5, 2019 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmitew/4a4f24b8544e4898b4844e46f0880c53 to your computer and use it in GitHub Desktop.
Save mrmitew/4a4f24b8544e4898b4844e46f0880c53 to your computer and use it in GitHub Desktop.
Inherited Stateful Widget
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