Skip to content

Instantly share code, notes, and snippets.

@mehmetf
Last active October 20, 2017 04:09
Show Gist options
  • Save mehmetf/c7679643991927b53471348ab1a516d4 to your computer and use it in GitHub Desktop.
Save mehmetf/c7679643991927b53471348ab1a516d4 to your computer and use it in GitHub Desktop.
With Inherited Widget
class MyInheritedWidget extends InheritedWidget {
final int accountId;
final int scopeId;
MyInheritedWidget(accountId, scopeId, child): super(child);
@override
bool updateShouldNotify(MyInheritedWidget old) =>
accountId != old.accountId || scopeId != old.scopeId;
}
class MyPage extends StatelessWidget {
final int accountId;
final int scopeId;
MyPage(this.accountId, this.scopeId);
Widget build(BuildContext context) {
return new MyInheritedWidget(
accountId,
scopeId,
const MyWidget(),
);
}
}
class MyWidget extends StatelessWidget {
const MyWidget();
Widget build(BuildContext context) {
// somewhere down the line
const MyOtherWidget();
...
}
}
class MyOtherWidget extends StatelessWidget {
const MyOtherWidget();
Widget build(BuildContext context) {
final myInheritedWidget = MyInheritedWidget.of(context);
print(myInheritedWidget.scopeId);
print(myInheritedWidget.accountId);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment