Flutter: Calling SetState() from another class

≡放荡痞女 提交于 2019-12-11 18:42:09

问题


I am trying to make a simple image that appears or disappears when a button is pushed. This button resides in a separate class to the image, so in Flutter this creates a massive headache of an issue.

I have read many forums on this and I have tried all the solutions posed but none of them are working for me.

What I am trying to do:

class SinglePlayerMode extends StatefulWidget {
  @override
  SinglePlayerModeParentState createState() => SinglePlayerModeParentState();
}

class SinglePlayerModeParentState extends State<SinglePlayerMode> {\
  bool coinVisible = false;

  toggleCoin() {
    setState(() {
      coinVisible = !coinVisible;
    });
  }

Widget topMenuRow() {
  return Stack(
    children: [
      Column(
        children: [
          coinVisible == true ?
          Padding(
            padding: EdgeInsets.all(50),
            child: Container(
              height: 60,
              width: 60,
              color: Colors.blueGrey[0],
              decoration: BoxDecoration(
                color: Colors.blueAccent,
                image: DecorationImage(
                  image: ExactAssetImage('lib/images/coin_head.jpg'),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ) : Container(
            height: 60,
            width: 60,
            color: Colors.black,
          ),
        ],
      ),
    ],
  );
 }

@override
Widget build(BuildContext context) {
  return Scaffold(
      child: ListView(
        padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
        children: [
          topMenuRow(),
          SizedBox(height: 40),
        ],
      ),
    ),
  );
}

And this is the separate class which I would like to trigger the SetState() on coinVisible from:

class dropDownMenu extends StatefulWidget {  @override
  _dropDownMenuState createState() => _dropDownMenuState();
}

class _dropDownMenuState extends State<dropDownMenu> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget> [
        Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            Container(
              child: Opacity(
                opacity: 0.0,
                child: FloatingActionButton(
                  heroTag: null,
                  onPressed:  (){
                    //SOMEHOW CALL SetState() ON coinVisble HERE!
                  },
                ),
              ),
          );
       }
  }

But nothing I have tried is working, and I have lost hours.


回答1:


It simple, you need to send your SinglePlayMode::toggleCoin function as callback to dropDownMenu class.

class dropDownMenu extends StatefulWidget {  
        final _callback; // callback reference holder
                                   //you will pass the callback here in constructor
    dropDownMenu( {@required void toggleCoinCallback() } ) :
       _callback = toggleCoinCallback;
        @override
      _dropDownMenuState createState() => _dropDownMenuState();
    }

    class _dropDownMenuState extends State<dropDownMenu> {
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: <Widget> [
            Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Container(
                  child: Opacity(
                    opacity: 0.0,
                    child: FloatingActionButton(
                      heroTag: null,
                      onPressed:  (){
                        widget?._callback(); // callback calling
                      },
                    ),
                  ),
              );
           }
      }

Then when you create a dropDownMenu class instance in your SinglePlayerMode class you will do

    dropDownMenu(
       toggleCoinCallback: toogleCoin,
    );


来源:https://stackoverflow.com/questions/54786946/flutter-calling-setstate-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!