英文:
How To Remove Selected Item From Drop Down Search In Flutter
问题
以下是您要翻译的内容:
[![图像 1][1]][1]
[1]: https://i.stack.imgur.com/jbMKi.png
当按下 Enter 键时,如何删除所选项目。这是我的代码
DropdownSearch<UserModel>(
selectedItem: name ?? null,
dropdownBuilder: (context, selectedItem) {
nama = selectedItem;
return Text(
"${nama ?? "${itemController.text ?? ''}"}",
);
},
asyncItems: (filter) async {
if (ItemList != null) {
print(ItemList);
return UserModel.fromJsonList(ItemList.where((user) => user['item_name'].toLowerCase().contains(filter.toLowerCase())).toList());
}
return [];
}
)
这是我的代码,我正在使用下拉搜索依赖项
英文:
How Can I Remove Selected Item When Enter Is Press . This Is My Code
DropdownSearch<UserModel>(
selectedItem: name ?? null,
dropdownBuilder: (context, selectedItem) {
nama = selectedItem;
return Text(
"${nama ?? "${itemController.text ?? ''}"}",
// style: TextStyle(
// // fontSize: 20,
// color: Colors.black,
// ),
);
},
asyncItems:(filter) async {
if (ItemList != null) {
// print(UserModel.fromJsonList(serviceDataList));
print(ItemList);
return UserModel.fromJsonList(ItemList.where((user) => user['item_name'].toLowerCase().contains(filter.toLowerCase())).toList());
}
return [];
}),
This is my code and i am using dropdown search dependency
答案1
得分: 1
你可以将 selectedItem
的值设置为 null,这将从下拉搜索中移除所选的值。
示例代码:
class Fs extends StatefulWidget {
const Fs({super.key});
@override
State<Fs> createState() => _FsState();
}
class _FsState extends State<Fs> {
String? _selectedItem;
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_selectedItem = null;
});
},
),
body: Column(
children: [
DropdownSearch<String>(
popupProps: PopupProps.menu(
showSelectedItems: true,
disabledItemFn: (String s) => s.startsWith('I'),
),
items: ["Brazil", "Italia (Disabled)", "Tunisia", 'Canada'],
dropdownDecoratorProps: DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
labelText: "Menu mode",
hintText: "country in menu mode",
),
),
onChanged: (String? data) {
setState(() {
_selectedItem = data;
});
},
selectedItem: _selectedItem,
)
],
),
);
}
}
这是你提供的代码的翻译部分。
英文:
You can set null value selectedItem
, it will remove the selected value from drondownSearch.
Example code
class Fs extends StatefulWidget {
const Fs({super.key});
@override
State<Fs> createState() => _FsState();
}
class _FsState extends State<Fs> {
String? _selectedItem;
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_selectedItem = null;
});
},
),
body: Column(
children: [
DropdownSearch<String>(
popupProps: PopupProps.menu(
showSelectedItems: true,
disabledItemFn: (String s) => s.startsWith('I'),
),
items: ["Brazil", "Italia (Disabled)", "Tunisia", 'Canada'],
dropdownDecoratorProps: DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
labelText: "Menu mode",
hintText: "country in menu mode",
),
),
onChanged: (String? data) {
setState(() {
_selectedItem = data;
});
},
selectedItem: _selectedItem,
)
],
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论