首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从小工具列表中删除小工具[Flutter]

从小工具列表中删除小工具[Flutter]
EN

Stack Overflow用户
提问于 2020-10-21 23:56:06
回答 1查看 79关注 0票数 0

在我正在构建的flutter应用程序中(基本上是一个待办事项列表),我希望通过一个按钮删除所有已完成的任务(活动的复选框)。到目前为止,只有当最后一个复选框处于活动状态,并且所有其他活动复选框(如果有)同时位于它的正上方时,它才能正常工作。

如果在一个活动复选框之后有一个非活动复选框,应用程序就会出现故障。

下面是创建任务小部件的类的代码:

代码语言:javascript
复制
class addnote extends StatefulWidget {

  final String task_txt;
  final int index;// receives the value

  addnote({ Key key, this.task_txt, this.index }): super(key: key);

  @override
  _addnoteState createState() => _addnoteState();
}

class _addnoteState extends State<addnote> {

  bool value=false;
  
  Widget txt_strike(String to_strike, bool str_value){
    return str_value ?
    AutoSizeText(to_strike,style: TextStyle(
      decoration: TextDecoration.lineThrough,
      fontFamily: "Quicksand",
      fontSize: 20.0,
      color: Colors.grey,
    ),
    maxLines: 2,
      overflow: TextOverflow.ellipsis,
    )
        :
    AutoSizeText(to_strike,style: TextStyle(
      //decoration: TextDecoration.lineThrough,
      fontFamily: "Quicksand",
      fontSize: 20.0,
      color: Colors.black,
    ),
    maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
        borderRadius:BorderRadius.circular(4.0),
        boxShadow: [BoxShadow(
        color:Colors.grey,
        offset: Offset(-6.0,4.0),
        blurRadius: 4.0,
        )],
            color: Colors.white
        ),
         margin: EdgeInsets.symmetric(vertical: 5.0,horizontal: 3.0),
      child:InkWell(

        onTap: (){setState(() {
          value=!value;
          w_checks[widget.index]=!w_checks[widget.index];
        });},
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Expanded(flex:1,
              child: CircularCheckBox(
                  disabledColor: Colors.white,
                  activeColor: Colors.green,
                  value: value,
                  onChanged:(bool _changed)
                  {
                    // print(w_checks[Index]);
                    setState(() {
                      value=!value;
                      w_checks[widget.index]=!w_checks[widget.index];
                    });
                  }),
            ),
            Expanded(flex:5,
                child: txt_strike(widget.task_txt, value)),
           ],
        ),
      )
    );
  }
}

下面是删除任务小部件的函数的代码:

代码语言:javascript
复制
void del_task(){
   int count=0;
   for(int i=0; i<w_checks.length; i++)
   {
     if(w_checks[i]==true)
       count++;
   }
   while(count>=0)
   {
     for(int j=0; j<w_checks.length; j++)
     {
       if(w_checks[j]==true)
       {
         setState(() {
           { w_tasks.removeAt(j);  //w_tasks is the list of all the tasks
           w_checks.removeAt(j);   //w_checks is the list that stores bool values corresponding to each checkbox
           }
         });
         for(int k=j; k<w_checks.length; k++)
           {
             w_tasks[k]=w_tasks[k+1];
             w_checks[k]=w_checks[k+1];

           }
         break;
       }
     }
     count--;
   }
 }

希望我能描述我的问题,请多多指教。

EN

回答 1

Stack Overflow用户

发布于 2020-10-22 01:43:09

我不太清楚你在del_task()中做了什么,但我认为你的问题是由for循环引起的,在这个循环中,你将所有后来的对象都向下移动了一个位置。

这个操作已经由“index(list.removeAt)”方法执行了,您可以从文档中看到:https://api.dart.dev/stable/2.10.2/dart-core/List/removeAt.html

我会像这样修改代码:

代码语言:javascript
复制
void del_task(){
   for(int i=0; i<w_checks.length; i++)
   {
       if(w_checks[i]==true)
       {
         setState(() {
           w_tasks.removeAt(i);  
           w_checks.removeAt(i);   
         });
         i--;
       }
   }
 }

我不明白为什么需要count变量。

我发布的代码应该遍历w_checks。当它找到一个“真”值时,它将调用removeAt(i),然后将索引减1。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64467418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档