【问题标题】:How to send parameters to setstate in Flutter如何在 Flutter 中向 setstate 发送参数
【发布时间】:2023-03-31 16:59:02
【问题描述】:

我无法设置 fn changedDetail 的值

当我点击 ontap 时,我想在 fn changedDetail 中设置值!= null icon Function(bool) changedDetail ;

在有状态的第二个小部件中

Function(bool) changedDetail ; 

处于状态第二个小部件

Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  return this.widget.changedDetail(false);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thLarge,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),
          Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                setState(() {
                  return this.widget.changedDetail(true);
                });
              },
              child: Container(
                  padding: EdgeInsets.only(top: 12.0),
                  child: Icon(
                    FontAwesomeIcons.thList,
                    color: KwanjaiColors.grey3,
                  )),
            ),
          ),

在父母中

  bool clicktap = false;
  bool choiceTap = false;

  tapFn(bool choice) {
    setState(() {
      choice == true ? choiceTap = true : choiceTap = false;
    });
  }

在小部件父级中

 body: Column(children: <Widget>[
          Container(
            child: new ProjectIndexTab(changedDetail: tapFn(clicktap)),
          ),
          Expanded(
            child: Container(
              color: KwanjaiColors.bg,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
                child: Column(children: <Widget>[
                  Container(child: ProjectIndexWorkDaily()),
                  Expanded(
                    // cuz has gridview must have expanded !!!!
                    child: Column(children: <Widget>[
                      Expanded(
                          child: choiceTap == true
                              ? ProjectViewList()
                              : ProjectGridList())
                    ]),
                  ),
                ]),
              ),
            ),
          ),
        ])

日志

I/flutter (12900):引发了另一个异常:NoSuchMethodError:方法“调用”在 null 上被调用。

【问题讨论】:

    标签: flutter dart flutter-dependencies


    【解决方案1】:

    试试这个:

    在父窗口小部件中,不带任何参数传递函数

    我假设您已经为第二个小部件设置了带有键的构造函数

    附带说明,您不必检查choiceTap == true,默认情况下,如果 bool 为空则设置为 false

    body: Column(children: <Widget>[
              Container(
                child: new ProjectIndexTab(changedDetail: tapFn),
              ),
              Expanded(
                child: Container(
                  color: KwanjaiColors.bg,
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
                    child: Column(children: <Widget>[
                      Container(child: ProjectIndexWorkDaily()),
                      Expanded(
                        // cuz has gridview must have expanded !!!!
                        child: Column(children: <Widget>[
                          Expanded(
                              child: choiceTap
                                  ? ProjectViewList()
                                  : ProjectGridList())
                        ]),
                      ),
                    ]),
                  ),
                ),
              ),
            ])
    

    在有状态的第二个小部件中

    final void Function(bool value) changedDetail;
    

    在您的第二个小部件中,您不必返回函数

    Expanded(
                flex: 1,
                child: InkWell(
                  onTap: () {
                    setState(() {
                      widget.changedDetail(false);
                    });
                  },
                  child: Container(
                      padding: EdgeInsets.only(top: 12.0),
                      child: Icon(
                        FontAwesomeIcons.thLarge,
                        color: KwanjaiColors.grey3,
                      )),
                ),
              ),
              Expanded(
                flex: 1,
                child: InkWell(
                  onTap: () {
                    setState(() {
                      widget.changedDetail(true);
                    });
                  },
                  child: Container(
                      padding: EdgeInsets.only(top: 12.0),
                      child: Icon(
                        FontAwesomeIcons.thList,
                        color: KwanjaiColors.grey3,
                      )),
                ),
              ),
    

    【讨论】:

    • 它不起作用,抛出了另一个异常:NoSuchMethodError:方法'call'在null上被调用。再次
    • @peary 如果我可以问,您可以发布您的有状态小部件的代码吗?您声明 changedDetail 方法的部分。也许是参数传递给它的方式。
    【解决方案2】:

    在父小部件中

    class ProjectIndexScreen extends StatefulWidget {
          ProjectIndexScreen({Key key, this.title}) : super(key: key);
          final String title;
          @override
          ProjectIndexScreenState createState() => ProjectIndexScreenState();
        }
    
        class ProjectIndexScreenState extends State<ProjectIndexScreen> {
          void _incrementCounter() {
            setState(() {
              // This call to setState tells the Flutter framework that something has
              // changed in this State, which causes it to rerun the build method below
              // so that the display can reflect the updated values. If we changed
              // _counter without calling setState(), then the build method would not be
            });
          }
    
          void initState() {
            super.initState();
          }
    
          bool clicktap = false;
          bool choiceTap = false;
    
          tapFn(bool choice) {
            setState(() {
              choice == true ? choiceTap = true : choiceTap = false;
            });
          }
    
          @override
          Widget build(BuildContext context) {
            return Scaffold(
                appBar: AppBar(
                  backgroundColor: KwanjaiColors.title,
                  title: Container(
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          flex: 7,
                          child: Container(
                            padding: EdgeInsets.only(right: 50.0),
                            child: Text(
                              "Choose Project",
                              style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.bold,
                                  color: KwanjaiColors.white),
                              textAlign: TextAlign.end,
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 1,
                          child: Icon(
                            Icons.notifications,
                            color: KwanjaiColors.white,
                          ),
                        ),
                        Expanded(
                          flex: 1,
                          child: Icon(
                            Icons.menu,
                            color: KwanjaiColors.white,
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                body: Column(children: <Widget>[
                  Container(
                    child: new ProjectIndexTab(changedDetail: tapFn(clicktap)),
                  ),
                  Expanded(
                    child: Container(
                      color: KwanjaiColors.bg,
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
                        child: Column(children: <Widget>[
                          Container(child: ProjectIndexWorkDaily()),
                          Expanded(
                            // cuz has gridview must have expanded !!!!
                            child: Column(children: <Widget>[
                              Expanded(
                                  child:
                                      choiceTap ? ProjectViewList() : ProjectGridList())
                            ]),
                          ),
                        ]),
                      ),
                    ),
                  ),
                ]));
          }
        }
    

    在第二个小部件中

    class ProjectIndexTab extends StatefulWidget {
      final void Function(bool value) changedDetail;
      ProjectIndexTab({Key key, this.changedDetail}) : super(key: key);
      @override
      ProjectIndexTabState createState() => new ProjectIndexTabState();
    }
    
    class ProjectIndexTabState extends State<ProjectIndexTab> {
      @override
      Widget build(BuildContext context) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
          color: KwanjaiColors.white,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Expanded(
                flex: 6,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 8.0),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(22.0))),
                  child: TextFormField(
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        contentPadding: const EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: 10.0),
                        hintText: "Search",
                        enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: KwanjaiColors.grey2),
                            borderRadius: BorderRadius.all(Radius.circular(4.0)),
                            gapPadding: 0.0),
                        fillColor: KwanjaiColors.grey2,
                        filled: true,
                        hintStyle: TextStyle(color: KwanjaiColors.grey3),
                        prefixIcon: Icon(
                          Icons.search,
                          color: KwanjaiColors.grey3,
                        )),
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: InkWell(
                  onTap: () {
                    setState(() {
                      widget.changedDetail(false);
                    });
                  },
                  child: Container(
                      padding: EdgeInsets.only(top: 12.0),
                      child: Icon(
                        FontAwesomeIcons.thLarge,
                        color: KwanjaiColors.grey3,
                      )),
                ),
              ),
              Expanded(
                flex: 1,
                child: InkWell(
                  onTap: () {
                    setState(() {
                      widget.changedDetail(true);
                    });
                  },
                  child: Container(
                      padding: EdgeInsets.only(top: 12.0),
                      child: Icon(
                        FontAwesomeIcons.thList,
                        color: KwanjaiColors.grey3,
                      )),
                ),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢!你的代码看起来不错。嗯,试试这个....在 onTap 中完全删除 setState()。 onTap: () => widget.changedDetail(false); Debug 在 tapFn 方法中放了一个断点,看它是否在那里。
    • 当我点击 ontap widget.changedDetail(false); , changedDetail 的值 = null ,它不会将值更改为 false
    猜你喜欢
    • 2018-12-23
    • 2021-03-05
    • 2012-12-01
    • 2021-05-05
    • 1970-01-01
    • 2018-12-30
    • 2013-06-18
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多