如何在 Flutter 中从下拉搜索中删除所选项目

问题描述 投票:0回答:2

按下 Enter 后如何删除所选项目。这是我的代码

 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 [];
                    }),

这是我的代码,我正在使用下拉搜索依赖项

flutter android-studio dart search dropdown
2个回答
1
投票

您可以设置空值

selectedItem
,它将从drondownSearch中删除所选值。

示例代码

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,
          )
        ],
      ),
    );
  }
}

0
投票

您可以设置:

clearButtonProps: const ClearButtonProps(isVisible:true)

到下拉搜索。

© www.soinside.com 2019 - 2024. All rights reserved.