0

I have a MainPage, which has an AppBar, a BottomNavigationBar, and multiple bodies :

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: resourcesBar(),
      body: _bodies[_selectedIndex],
      bottomNavigationBar: bottomNavigationBar(),
    );
  }

Of course, the body changes with the selected index of the bottomNavigationBar.
In my AppBar, I have some double variables.

I want to update these double variables when the user clicks on a button in one of the bodies.

The bodies are also StatefulWidgets, for example :

class SubView extends StatefulWidget {
  SubView({Key key, this.planet, this.upgradeCallback}) : super(key: Key(planet.name));

  final Planet planet ;
  final Function(Building) upgradeCallback ;

  @override
  _SubViewState createState() => _SubViewState();
}

Here is how I give my upgradeCallback in the MainPage :

  List<Widget> _bodies = <Widget>[
    SubView(planet: userData.planetsList[0],
      upgradeCallback: updateVariables,
    ),
  ];

Here is my updateVariables function :

  static void updateVariables(Building current) {
    setState(() {
      userData.variable_one += 10;
    });
  }

However, my IDE show me an error : Instance members can't be accessed from a static method.
If I set my function as non-static, I have, on my _bodies list SubView declaration : Only static members can be accessed in initializers.

I want to be able to dynamically update MainPage AppBar variables (using setState) from a click in the SubView, but I do not know how to do that.

Any ideas ?

Thanks in advance.

Mathieu
  • 1,435
  • 3
  • 16
  • 35

0 Answers0