Skip to content

Instantly share code, notes, and snippets.

@esDotDev
Last active August 5, 2021 18:25

Revisions

  1. esDotDev revised this gist Jun 15, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions inherited_widget.dart
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@ class MyFoo extends StatefulWidget {

    @override
    MyFooState createState() => MyFooState();

    static MyFooState of(BuildContext context) =>
    (context.dependOnInheritedWidgetOfExactType<_MyInheritedFoo>() as _MyInheritedFoo).state;
    }

    class MyFooState extends State<MyFoo> {
  2. esDotDev revised this gist Apr 12, 2021. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions inherited_widget.dart
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,17 @@

    class MyFoo extends StatefulWidget {
    const MyFoo({Key? key, required this.child}) : super(key: key);
    final Widget child;

    @override
    MyFooState createState() => MyFooState();

    static MyFooState of(BuildContext context) =>
    (context.dependOnInheritedWidgetOfExactType<_MyInheritedFoo>() as _MyInheritedFoo).state;
    }

    class MyFooState extends State<MyFoo> {
    @override
    Widget build(BuildContext context) => Container();
    Widget build(BuildContext context) {
    return _MyInheritedFoo(child: widget.child, state: this);
    }
    }

    class _MyInheritedFoo extends InheritedWidget {
  3. esDotDev created this gist Apr 12, 2021.
    19 changes: 19 additions & 0 deletions inherited_widget.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    class MyFoo extends StatefulWidget {
    @override
    MyFooState createState() => MyFooState();

    static MyFooState of(BuildContext context) =>
    (context.dependOnInheritedWidgetOfExactType<_MyInheritedFoo>() as _MyInheritedFoo).state;
    }

    class MyFooState extends State<MyFoo> {
    @override
    Widget build(BuildContext context) => Container();
    }

    class _MyInheritedFoo extends InheritedWidget {
    _MyInheritedFoo({Key? key, required Widget child, required this.state}) : super(key: key, child: child);
    final MyFooState state;
    @override
    bool updateShouldNotify(covariant InheritedWidget oldWidget) => true;
    }