如何解决dropdown_search 5.0.3多选下拉框选中值提取与打印问题?
问题解决方法
错误原因
你使用的是DropdownSearch<String>.multiSelection(多选下拉框),它的onChanged回调要求接收**List<String>类型**的参数,但你传入的是接收单个String的函数,类型不匹配导致报错。另外,当前你只传递了Food的title作为选项值,无法直接关联到对应的amount,所以更合理的做法是直接用Food作为下拉框的泛型类型。
修改步骤及完整代码
- 将下拉框泛型从
String改为Food,直接基于自定义对象构建选项 - 调整选中值变量为
List<Food>,适配多选场景 - 配置
popupProps的itemBuilder来渲染每个选项的title - 修改
onChanged回调参数类型为List<Food>,直接获取选中的Food对象,从而轻松拿到title和amount
修改后的完整代码:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:dropdown_search/dropdown_search.dart'; import 'dart:io'; void main() => runApp(const MyApp()); class Food { final String title; final int id; final int amount; final DateTime date; Food({ required this.title, required this.id, required this.amount, required this.date, }); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'My App', theme: ThemeData(), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { final _popupBuilderKey = GlobalKey<DropdownSearchState<Food>>(); List<Food> selectedFoods = []; // 改为存储选中的Food对象列表 final List<Food> foodListData = [ Food( title: 'Food 1', id: 1, amount: 3, date: DateTime.now(), ), Food( title: "Food 2", id: 2, amount: 1, date: DateTime.now(), ), Food( title: 'Food 3', id: 3, amount: 3, date: DateTime.now(), ), Food( title: 'Food 4', id: 4, amount: 1, date: DateTime.now(), ), ]; @override Widget build(BuildContext context) { final pageBody = SafeArea( child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[ Column( children: <Widget>[ const Text('My Foods'), ElevatedButton( child: const Text("Add Food"), onPressed: null, ), DropdownSearch<Food>.multiSelection( key: _popupBuilderKey, items: foodListData, // 直接传入Food列表 popupProps: PopupPropsMultiSelection.menu( showSelectedItems: true, showSearchBox: true, // 自定义选项渲染,显示Food的title itemBuilder: (context, item, isSelected) { return ListTile( title: Text(item.title), trailing: isSelected ? const Icon(Icons.check) : null, ); }, ), // 自定义选中项的显示 selectedItemBuilder: (context, selectedItems) { return selectedItems.map((food) => Text(food.title)).toList(); }, // 回调参数改为List<Food>,直接获取选中的对象 onChanged: (List<Food> selectedItems) { setState(() { selectedFoods = selectedItems; }); // 打印每个选中项的title和amount for (var food in selectedItems) { print("标题:${food.title},数量:${food.amount}"); } }, ), ], ), ], ), ), ); return Scaffold( appBar: const AppBar(title: Text('My App')), body: pageBody, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, floatingActionButton: Platform.isIOS ? Container() : FloatingActionButton( child: const Icon(Icons.add), onPressed: null, ), ); } }
关键改动说明
- 泛型改为
Food后,下拉框直接操作自定义对象,无需额外映射title,能直接获取所有字段 onChanged接收List<Food>参数,完美适配多选场景,同时可以直接访问每个选中项的title和amount- 通过
itemBuilder和selectedItemBuilder自定义UI显示,保持界面和之前一致
内容的提问来源于stack exchange,提问作者Anika Denwell
相关产品推荐
相关产品推荐

