Flutter:从另一个 class 调用 SetState()

Flutter: Calling SetState() from another class

我正在尝试制作一个简单的图像,当按下按钮时它会出现或消失。此按钮位于图像的单独 class 中,因此在 Flutter 中,这会造成令人头疼的问题。

我阅读了很多关于此的论坛,并且尝试了所有提出的解决方案,但 none 对我有用。

我想做什么:

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),
        ],
      ),
    ),
  );
}

这是单独的 class,我想在 coinVisible 上触发 SetState() 来自:

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!
                  },
                ),
              ),
          );
       }
  }

但我尝试过的都没有用,而且我浪费了时间。

很简单,您需要将 SinglePlayMode::toggleCoin 函数作为回调发送到 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
                      },
                    ),
                  ),
              );
           }
      }

然后当你在 SinglePlayerMode 中创建一个 dropDownMenu class 实例时 class 你会做

    dropDownMenu(
       toggleCoinCallback: toogleCoin,
    );