【问题标题】:Flutter: Calling SetState() from another classFlutter:从另一个类调用 SetState()
【发布时间】:2019-07-14 04:17:12
【问题描述】:

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

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

我想做什么:

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

这是我想在 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!
                  },
                ),
              ),
          );
       }
  }

但我尝试过的任何方法都不起作用,而且我已经浪费了几个小时。

【问题讨论】:

    标签: flutter widget setstate


    【解决方案1】:

    很简单,您需要将 SinglePlayMode::toggleCoin 函数作为回调发送到 dropDownMenu 类。

    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 类实例时,您将这样做

        dropDownMenu(
           toggleCoinCallback: toogleCoin,
        );
    

    【讨论】:

    • 天哪!你是我真正的英雄。非常感谢,马科斯。
    • 作为一个附加组件,dropDownMenu 是一个包含许多按钮的菜单。如何向此类添加额外的回调?我需要能够使用许多不同的回调。这可能吗?
    • 这取决于您的需求。例如,如果您需要像 2,3, 4 这样的少量回调,我建议您使用我已经展示过的相同方法。您只需要在构造函数类中将回调作为参数传递,将回调保存到类成员中并在需要时调用。
    • 感谢马科斯的回复。你有机会用代码告诉我吗?我正在尝试这样做,但我不知道该怎么做。我没有尝试任何工作。
    • 是的,我可以做后者。只需详细说明您的需求。
    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2019-03-29
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多