title: Flutter StatefulWidget - State class inheritance?
body: <p>The point of this question, is how to handle state changes, preferably automatically by the original ancestor.
  It seems to me that it is not possible to keep extending these classes beyond
  the first child with a StatefulWidget as an ancestor (example 1)?</p>
  
  <p>The only way seems to me, to use mixins.
  However this has the drawback, that you have to manage state changes manually (example 2).</p>
  
  <p>Am I overlooking something ?</p>
  
  <p>// Example 1 - not working as BarState dose not inherit from Bar</p>
  
  <pre><code>class Foo extends StatefulWidget {
    final String stringFoo;
  
    Foo({Key key, this.stringFoo}) : super(key: key);
  
    @override
    FooState createState() =&gt; new FooState();
  }
  
  class FooState extends State&lt;Foo&gt; {
    bool updating = false;
  
    Future&lt;void&gt; _update() async {
      // await something(stringFoo)
      setState(() {
        updating = false;
      });
    }
  
    void update() {
      setState(() {
        updating = true;
      });
      _update();
    }
  
    // Dummy build, always override
    @override
    Widget build(BuildContext context) {
      return null;
    }
  
    @override
    void initState() {
      super.initState();
      update();
    }
  }
  
  class Bar extends Foo {
    final String stringBar;
  
    Bar({Key key, String stringFoo, this.stringBar}) : super(key: key, stringFoo: stringFoo);
  
    @override
    BarState createState() =&gt; new BarState();
  }
  
  class BarState extends FooState {
  
    @override
    Widget build(BuildContext context) {
      if(updating) {
        return Text('Im updating: ' + widget.stringFoo);
      } else {
        return Text('All done!' + widget.stringBar);
      }
    }
  
  }
  </code></pre>
  
  <p>// Example 2 - Works but state changes are handled manually</p>
  
  <pre><code>class Foo {
    String stringFoo;
    bool updating = false;
  
    Widget getFoo() {
      return new Text(stringFoo);
    }
  
    Future&lt;void&gt; _update() async {
      // await something
      // stringFoo = await result
      updating = false;
      stateChanged();
    }
  
    void update() {
      updating = true;
      stateChanged();
      _update();
    }
  
    void stateChanged() {}
  }
  
  class Bar extends StatefulWidget {
    final stringBar;
  
    Bar({Key key, this.stringBar}) : super(key: key);
  
    @override
    BarState createState() =&gt; new BarState();
  }
  
  class BarState extends State&lt;Bar&gt; with Foo {
    bool dummy = true;
  
    Widget getBar() {
      return new Text(widget.stringBar);
    }
  
    @override
    void stateChanged() {
      setState(() {
        if(dummy) {
          dummy = false;
        } else {
          dummy = true;
        }
      });
    }
  
    @override
    Widget build(BuildContext context) {
      if(updating) {
        return getBar();
      } else {
        return getFoo();
      }
    }
  
    @override
    void initState() {
      super.initState();
      update();
    }
  }
  </code></pre>