【问题标题】:UI not refreshing用户界面不刷新
【发布时间】:2021-08-19 00:06:46
【问题描述】:

这是我更新后的代码,UI 仍未刷新。我究竟做错了什么? 我移动了 setState 函数,也删除了 initState

class IncludeObject extends StatefulWidget {


const IncludeObject({
Key key,
this.title,

}) `: super(key: key); 最终字符串标题;

@覆盖 _IncludeObject createState() => _IncludeObject();`

class _IncludeObject extends State<IncludeObject> {
Widget build(BuildContext context) {
return Padding(
  padding: const EdgeInsets.symmetric(horizontal: 2.5),
  child: Container(
    decoration: BoxDecoration(
        color: Color(0xffDEDEDE), borderRadius: BorderRadius.circular(4)),
    child: Expanded(
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.all(8),
            child: Text(
              widget.title.toString(),
              style:
                  TextStyle(fontFamily: "Montserrat Regular", fontSize: 12),
            ),
          ),
          IconButton(
              icon: Icon(
                Icons.close_outlined,
                size: 20,
              ),
              onPressed: () {
                setState(() {
                  for (int i = 0; i <= includedList.length; i++) {
                    if (includedList[i].title == widget.title) {
                      includedList.removeAt(i);
                      included.removeAt(i);
                      print("removed");
                    }
                  }
                });
              }),
        ],
      ),
    ),
  ),
);

}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    首先,在 initState 中包含 setState 并不是很好(阅读有关小部件生命周期的更多信息) 现在为什么会发生这个问题是因为您没有调用 setState 来使用新数据更新 UI,而只需在 setState 内的 onPressed 中添加代码,就像这样

          onPressed: () {
               setState(() {
                 for (int i = 0; i <= includedList.length; i++) {
                  if (includedList[i].title == widget.title) {
                    includedList.removeAt(i);
                    included.removeAt(i);
                    _isRemoved = true;
                    print("removed");
                    print(_isRemoved);
                  }
                }
                });
              }),
    

    【讨论】:

    • 我试过了,但我仍然无法删除要删除的标签。即 UI 不会被刷新,除非我热重载然后标签会去
    • 你能用新代码编辑问题吗?
    • 我已将其更新为新代码。你能看一下吗
    【解决方案2】:

    您需要在删除项目后致电setState

    setState() 告诉 Flutter 重新运行构建,从而反映更改。

    您可以在删除项目后调用它。

         onPressed: () {
                for (int i = 0; i <= includedList.length; i++) {
                  if (includedList[i].title == widget.title) {
                   
    
                    includedList.removeAt(i);
                    included.removeAt(i);
    
                    _isRemoved = true;
    
                    setState(() {});
    
                    print("removed");
                    print(_isRemoved);
                  }
                }
              }
    

    【讨论】:

    • 我试过了,但我仍然无法删除要删除的标签。即 UI 不会被刷新,除非我热重载然后标签会去
    猜你喜欢
    • 1970-01-01
    • 2011-01-08
    • 2016-05-16
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2011-02-02
    • 2012-09-05
    • 1970-01-01
    相关资源
    最近更新 更多