【发布时间】:2020-10-21 15:56:06
【问题描述】:
在我正在构建的颤振应用程序(基本上是一个待办事项列表)中,我希望使用一个按钮删除所有已完成的任务(活动复选框)。 到目前为止,只有当最后一个复选框处于活动状态并且所有其他活动复选框(如果有)同时位于其正上方时,它才会按预期运行。
如果在一个激活的复选框之后还有一个非激活的复选框,应用就会出现故障。
这是创建任务小部件的类的代码:
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)),
],
),
)
);
}
}
这是删除任务小部件的函数的代码:
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--;
}
}
希望我能够描述我的问题,请提供帮助。
【问题讨论】:
-
类似这样的东西:
List<String> numbers = ['one', 'two', 'three', 'four']; numbers.removeWhere((item) => item.length == 3); print(numbers); // [three, four'] -
@pskink 是的,我知道这种方法,但我还没有尝试在这里使用它。我会尽力让你知道。