Flutter: Calling SetState() from another class

后端 未结 1 863
鱼传尺愫
鱼传尺愫 2020-12-19 22:56

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 m

相关标签:
1条回答
  • 2020-12-19 23:29

    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,
        );
    
    0 讨论(0)
提交回复
热议问题