我想将复选框小部件的选中状态保存在共享首选项中。 这样,当我们再次启动该应用程序时,我们应该会看到选中的列表。
这是代码,但是由于在检查列表中刷新后变为未检查列表,因此无法正常工作。
我已使用SQLite数据库保存“待办事项”列表详细信息。
class _HomeState extends State<Home> {
TodoService _todoService;
var _selectedValue;
var _categories = List<DropdownMenuItem>();
List<Todo>_todoList=List<Todo>();
final GlobalKey<ScaffoldState> _globalKey=GlobalKey<ScaffoldState>();
@override
initState(){
super.initState();
getAllTodos();
_loadCategories();
getValues();
}
getAllTodos()async{
_todoService=TodoService();
_todoList=List<Todo>();
var todos= await _todoService.readTodo();
todos.forEach((todo){
setState(() {
var model=Todo();
model.id=todo['id'];
model.title=todo['title'];
model.dueDate=todo['dueDate'];
model.category=todo['category'];
model.isFinished=todo['isFinished'];
_todoList.add(model);
});
});
}
_save(String key,dynamic val)async{
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool(key,val);
}
getValues() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
bool value=prefs.getBool('Checked');
return value;
}
body: ListView.builder(itemCount: _todoList.length,itemBuilder: (context, index){
return Padding(
padding: EdgeInsets.only(top:8.0, left: 8.0, right: 8.0),
child: Card (
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0)
),
child: ListTile(
leading: Checkbox(
checkColor: Colors.indigo,
value: _todoList[index].isChecked,
onChanged:(bool value){
setState(() {
_todoList[index].isChecked=value;
});
_save('Checked',value);
},
),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(_todoList[index].title ?? 'No Title',
style: TextStyle(decoration: (_todoList[index].isChecked? TextDecoration.lineThrough: TextDecoration.none),
),
),
IconButton(icon: Icon(Icons.delete,color: Colors.red,
),
onPressed: (){
_deleteFormDialog(context,_todoList[index].id);
}
),
],
),
subtitle: Text(_todoList[index].dueDate ?? 'No Due Date'),
// trailing: Text(_todoList[index].dueDate ?? 'No Due Date'),
),
),
);
}),
floatingActionButton: FloatingActionButton(
onPressed: ()=>Navigator.of(context).push(MaterialPageRoute(builder:(context)=>TodoScreen())),
child: Icon(Icons.add),
),
);
}
}
答案 0 :(得分:1)
您不会对在let cell; // is of type Cell
let input; // is of type PlayerInput
if cell is Player or Metal, FALSE
if cell is Boulder(_) or Diamond(_) and input is Down, FALSE
if cell is Boulder(NotFalling) and input is Up, FALSE
if cell is Boulder(_) and input is Right or Left, and oneMoreCondition(cell,input) is true, FALSE
otherwise TRUE
中调用的getValues()
函数返回的值做任何事情。
initState()
我看到您的复选框的值是从列表中设置的,也许是将值存储在@override
initState(){
super.initState();
getAllTodos();
_loadCategories();
getValues(); // This is returning the saved value, either true, false, or null. You must set your checkBox initially to this value.
}
函数的列表中。
getValues()
getValues() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
bool val = prefs.getBool('Checked');
setState(() {
_todoList[0].isChecked = val?? false;
});
}
是一个空值运算符,如果保存的??
是_todoList[0].isChecked
,它将false
设置为val
,如果您不这样做,则会发生这种情况。还没有保存任何东西。