gpt4 book ai didi

flutter - Flutter 中 DropDownMenu 按钮中的 OnTap 函数

转载 作者:行者123 更新时间:2023-12-05 01:35:19 29 4
gpt4 key购买 nike

我尝试用 SQLite 数据库中的数据填充下拉菜单按钮。然后在 onTap 函数上,我想导航到选定的类别。

当我点击类别时它不导航。

我在数据库中保存了每个类别的 id,该 id 用于标识所选项目。这是代码:

'''

class _HomeState extends State<Home> {

TodoService _todoService;
var _selectedValue;

var _categories = List<DropdownMenuItem>();

List<Todo>_todoList=List<Todo>();

@override
initState(){
super.initState();
_loadCategories();

}

_loadCategories() async {
var _categoryService = CategoryService();
var categories = await _categoryService.readCategory();
categories.forEach((category) {
setState(() {
_categories.add(DropdownMenuItem(
child: Text(category['name']),
value: category['name'],
onTap: ()=>Navigator.of(context).push(MaterialPageRoute(builder:(context)=>TodosByCategory(category: category['name'],))),
));
});
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
key: _globalKey,
appBar: AppBar(
actions: <Widget>[
DropdownButtonHideUnderline(
child: DropdownButton(
value: _selectedValue,
items: _categories,
dropdownColor: Colors.blue,
style: TextStyle(color: Colors.white,fontSize: 16.0),
iconDisabledColor: Colors.white,
iconEnabledColor: Colors.white,
onChanged: (value) {
setState(() {
_selectedValue = value;
});
},
),
),

'''

这是 todosByCategory():

'''

class _TodosByCategoryState extends State<TodosByCategory> {

List<Todo>_todoList=List<Todo>();

TodoService _todoService=TodoService();

@override
initState(){
super.initState();
getTodosByCategories();
}

getTodosByCategories()async{
var todos=await _todoService.readTodoByCategory(this.widget.category);
todos.forEach((todo){
setState(() {
var model= Todo();
model.title=todo['title'];
model.dueDate=todo['dueDate'];

_todoList.add(model);

});
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Todos By Category'),
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: _todoList.length,
itemBuilder: (context, index){
return Padding(
padding: EdgeInsets.only(top:8.0, left: 8.0, right: 8.0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0),
),
elevation: 8.0,
child: ListTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(_todoList[index].title)
],
),
subtitle: Text(_todoList[index].dueDate),
// trailing: Text(_todoList[index].dueDate),
),
),
);
},),
)
],
),
);
}
}

'''请帮帮我。

最佳答案

与其将导航代码写在 DropdownMenuItemonTap 中,不如将其写在 DropdownButtononChanged 中您还可以在其中获取类别名称字符串作为 value。它应该可以工作。

关于flutter - Flutter 中 DropDownMenu 按钮中的 OnTap 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63014780/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com