title: Flutter: how to get parent widget variable?
body: <p>I used the example below to test and learn how to call parent functions from child widget.
  When you click the <code>TestButton</code>, the <code>countRefresh</code> function is called and the variable <code>count</code> increases by 1.
  Right now the button changes color each time it is clicked (either blue or red).</p>
  
  <p><strong>QUESTION</strong>: say that I want the color to change based on some logic around the <code>count</code> variable, how can I access the <code>count</code> variable from within the <code>TestButton</code> widget?
  E.g. if <code>count</code> is a multiple of three then the button should be red, otherwise blue.</p>
  
  <p>I read about InheritedWidgets, but it seems like variables must be final inside InheritedWidgets (if I don't put final before <code>int count = 0;</code> I get the 'this class is marked as immutable' message error).
  But based on this example I need <code>count</code> to change each time the button is clicked.
  What's the alternative to InheritedWidgets?</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  class Test extends StatefulWidget {
    static const String id = 'test';
    @override
    _TestState createState() =&gt; _TestState();
  }
  
  class _TestState extends State&lt;Test&gt; {
    int count = 0;
    Color color = Colors.red;
  
    void refreshCount() {
      setState(() {
        count += 1;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child:
              Text('The button was pressed $count time${count == 1 ? '' : 's'}'),
        ),
        floatingActionButton: TestButton(
          color: color,
          notifyParent: refreshCount,
        ),
      );
    }
  }
  
  class TestButton extends StatefulWidget {
    TestButton({
      @required this.color,
      @required this.notifyParent,
    });
    final Color color;
    final void Function() notifyParent;
    @override
    _TestButtonState createState() =&gt; _TestButtonState();
  }
  
  class _TestButtonState extends State&lt;TestButton&gt; {
    Color color;
    void initState() {
      color = widget.color;
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      return GestureDetector(
        onTap: () {
          widget.notifyParent();
          setState(() {
            color = color == Colors.red ? Colors.blue : Colors.red;
          });
        },
        child: Container(
          child: Icon(
            Icons.add,
            size: 80,
          ),
          color: color,
        ),
      );
    }
  }
  </code></pre>