我是新手,过去几天一直在尝试制作待办事项列表应用程序。我一直在尝试实现一项功能,用户可以将一项任务标记为重要任务,然后在列表视图中突出显示该任务。但是,在我放置了“切换”按钮的页面中,切换后,即使后端功能正在工作并且任务被标记为重要,该按钮也不会更新。有人可以帮我吗?这是代码:-
class toDoList extends StatefulWidget
{
bool data = false;
@override
createState()
{
return new toDoListState();
}
}
class toDoListState extends State<toDoList>
{
List<String> tasks = [];
List<String> completedTasks = [];
List<String> descriptions = [];
List<bool> importance = [];
@override
Widget build(BuildContext context)
{
return Scaffold
(
body: buildToDoList(),
floatingActionButton: new FloatingActionButton
(
onPressed: addToDoItemScreen,
tooltip: 'Add Task',
child: new Icon(Icons.add),
),
);
}
Widget buildToDoList()
{
return new ListView.builder
(
itemBuilder: (context, index)
{
if(index < tasks.length)
{
return row(tasks[index], descriptions[index], index);
}
},
);
}
Widget row(String task, String description, int index)
{
return Dismissible(
key: UniqueKey(),
background: Container(color: Colors.red, child: Align(alignment: Alignment.center, child: Text('DELETE', textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 18),))),
direction: DismissDirection.horizontal,
onDismissed: (direction) {
setState(() {
tasks.removeAt(index);
if(completedTasks.contains(task))
{
completedTasks.removeAt(index);
}
descriptions.removeAt(index);
importance.removeAt(index);
});
Scaffold.of(context).showSnackBar(SnackBar(content: Text(task+" dismissed")));
},
child: CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
title: Text(task, style: (completedTasks.contains(task)) ? TextStyle(decoration: TextDecoration.lineThrough) : TextStyle(),),
subtitle: (importance[index]) ? Text('This is important') : Text('No it\'s not'),
value: completedTasks.contains(task),
onChanged: (bool value) {
setState(() {
if(!completedTasks.contains(task))
{
completedTasks.add(task);
}
else
{
completedTasks.remove(task);
}
});
},
));
}
void addToDoItemScreen()
{
int index = tasks.length;
while(importance.length > tasks.length)
{
importance.removeLast();
}
importance.add(false);
Navigator.of(context).push
(
new MaterialPageRoute(
builder: (context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Add a new task')
),
body: Form
(child: Column
(
children: <Widget>
[TextField
(
autofocus: true,
onSubmitted: (name) {
addToDoItem(name);
//Navigator.pop(context); // Close the add todo screen
},
decoration: new InputDecoration(
hintText: 'Enter something to do...',
contentPadding: const EdgeInsets.all(20.0),
border: OutlineInputBorder()
),
),
TextField
(
//autofocus: true,
onSubmitted: (val)
{
addDescription(val, index);
},
decoration: new InputDecoration(
hintText: 'Enter a task decription...',
contentPadding: const EdgeInsets.all(20.0),
border: OutlineInputBorder()
),
),
Center
(
child: Switch
(
value: widget.data,
onChanged: (val)
{
setState(() {
widget.data = val;
widget.data = !widget.data;
print(widget.data);
});
impTask(index);
},
),
)
],
),)
);
}));
}
void addToDoItem(String task)
{
setState(() {
tasks.add(task);
descriptions.add("No description");
});
}
void addDescription(String desc, int index)
{
setState(() {
descriptions[index] = desc;
});
}
void impTask(int index)
{
setState(() {
if(importance[index])
{
importance[index] = false;
}
else
{
importance[index] = true;
}
});
}
}
答案 0 :(得分:1)
将StateFulBuilder
添加到addToDoItemScreen()
。
void addToDoItemScreen() {
int index = tasks.length;
while (importance.length > tasks.length) {
importance.removeLast();
}
importance.add(false);
Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
return StatefulBuilder(builder: (context, setState) { // this is new
return new Scaffold(
appBar: new AppBar(title: new Text('Add a new task')),
body: Form(
child: Column(
children: <Widget>[
TextField(
autofocus: true,
onSubmitted: (name) {
addToDoItem(name);
//Navigator.pop(context); // Close the add todo screen
},
decoration: new InputDecoration(
hintText: 'Enter something to do...',
contentPadding: const EdgeInsets.all(20.0),
border: OutlineInputBorder()),
),
TextField(
//autofocus: true,
onSubmitted: (val) {
addDescription(val, index);
},
decoration: new InputDecoration(
hintText: 'Enter a task decription...',
contentPadding: const EdgeInsets.all(20.0),
border: OutlineInputBorder()),
),
Center(
child: Switch(
value: widget.data,
onChanged: (val) {
setState(() {
widget.data = val;
});
impTask(index);
},
),
)
],
),
));
});
}));
}