从Flutter中的下拉搜索中删除选定项目的方法

huangapple go评论90阅读模式
英文:

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 [];
  }
)
这是我的代码,我正在使用下拉搜索依赖项
英文:

从Flutter中的下拉搜索中删除选定项目的方法

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&lt;Fs&gt; createState() =&gt; _FsState();
}

class _FsState extends State&lt;Fs&gt; {
  String? _selectedItem;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _selectedItem = null;
          });
        },
      ),
      body: Column(
        children: [
          DropdownSearch&lt;String&gt;(
            popupProps: PopupProps.menu(
              showSelectedItems: true,
              disabledItemFn: (String s) =&gt; s.startsWith(&#39;I&#39;),
            ),
            items: [&quot;Brazil&quot;, &quot;Italia (Disabled)&quot;, &quot;Tunisia&quot;, &#39;Canada&#39;],
            dropdownDecoratorProps: DropDownDecoratorProps(
              dropdownSearchDecoration: InputDecoration(
                labelText: &quot;Menu mode&quot;,
                hintText: &quot;country in menu mode&quot;,
              ),
            ),
            onChanged: (String? data) {
              setState(() {
                _selectedItem = data;
              });
            },
            selectedItem: _selectedItem,
          )
        ],
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年4月13日 18:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定