【问题标题】:How to set state via a method from another class如何通过另一个类的方法设置状态
【发布时间】:2019-08-11 12:13:00
【问题描述】:

使用 Flutter 并尝试将 Widget 移出到另一个类以保持其模块化。 问题是其中一个内部 Widget 中有一个 setState 。但是当我把它移到另一个类时,它是无效的,因为它不是有状态的。最初考虑传入一个 Function 并将其替换为 setState 函数本身,但这似乎不起作用。我该如何解决这个问题?

当前运行良好的方法。我计划将所有内容从第 6 行(子项:Center)移到另一个类,因为这将通过 PageView 重复。将把这一页滑到下一页,每一个看起来都一样,只有文字发生了变化。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.tealAccent,
      body: SafeArea(
        child: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Text(
                  "Did you hear about the claustrophobic astronaut?",
                  style: TextStyle(
                    fontSize: 20,
                  ),
                ),
                Text(
                  "He just needed a little space.",
                  style: TextStyle(
                    fontSize: 20,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    GestureDetector(
                      child: Icon(heartStatus),
                      onTap: () {
                        setState(() {
                          if (heartStatus == FontAwesomeIcons.heart) {
                            heartStatus = FontAwesomeIcons.solidHeart;
                          } else {
                            heartStatus = FontAwesomeIcons.heart;
                          }
                        });
                      },
                    ),
                    GestureDetector(
                      child: Icon(FontAwesomeIcons.shareAlt),
                      onTap: (){
                        print('shared');
                      },
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

这是在将它移到另一个类之后,因为设置状态在这里无效。在这里传入函数会抛出错误 -> 不必要的语句

Widget getPageData(Function function){
  IconData heartStatus = FontAwesomeIcons.heart;

  return Center(
    child: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            "Did you hear about the claustrophobic astronaut?",
            style: TextStyle(
              fontSize: 20,
            ),
          ),
          Text(
            "He just needed a little space.",
            style: TextStyle(
              fontSize: 20,
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              GestureDetector(
                child: Icon(heartStatus),
                onTap: () {
                  setState(() {
                    //tried passing in function here
                    if (heartStatus == FontAwesomeIcons.heart) {
                      heartStatus = FontAwesomeIcons.solidHeart;
                    } else {
                      heartStatus = FontAwesomeIcons.heart;
                    }
                  });
                },
              ),
              GestureDetector(
                child: Icon(FontAwesomeIcons.shareAlt),
                onTap: (){
                  print('shared');
                },
              ),
            ],
          ),
        ],
      ),
    ),
  );
}

【问题讨论】:

  • 看看InheritedWidgetScoped ModelBLoC Pattern或者一个回调函数。我对这些内容还不够熟悉,无法给出完整的答案,但我希望这能引导您朝着正确的方向前进。

标签: flutter dart


【解决方案1】:

你可以转你的function:

Widget getPageData(Function function){...}

Customised Widget

class CustomizeWidget extend StatefulWidget{

}
class _CustomizeWidgetState extend State<StatefulWidget>{
   /// in the build function, you can call setState
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2014-01-13
    • 1970-01-01
    相关资源
    最近更新 更多