title: How to call into a flutter widget's state from the widget
body: <p>I'm new to dart/flutter and having a little trouble getting my head around communication patterns.</p>
  
  <p>One reoccurring problem is that I keep looking to expose a public method on a widget so it can be called by other widgets. </p>
  
  <p>The problem is with stateful widgets. In these cases, I need to call down to the widgets state to do the actual work.</p>
  
  <p>The problem is that the widget doesn't have a copy of the state.
  I have been saving a copy of the state in the widget but of course this throws a warning as it makes the widget mutable.</p>
  
  <p>Let me give a specific example:</p>
  
  <p>I have a specialised menu which can have a set of menu items.
  Each are stateful.</p>
  
  <p>When the menu is closing it needs to iterate over the list of menu items that it owns and tell each one to hide (the menu items are not visually contained within the menu so hiding the menu doesn't work).</p>
  
  <p>So the menu has the following code:</p>
  
  <pre><code>class Menu{
    closeMenu() {
      for (var menuItem in menuItems) {
        menuItem.close();
      }
    }
  </code></pre>
  
  <p>So that works fine, but of course in the MenuItem class I need to:</p>
  
  <pre><code>class MenuItem {
    MenuItemState state;
  
    close()
    {
      state.close();
    }
  </code></pre>
  
  <p>But of course having the state object stored In the MenuItem is a problem given that MenuItem is meant to be immutable. (It is only a warning so the code works, but its clearly not the intended design pattern).</p>