dart 文本表单字段建议或自动完成

lvmkulzt  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(157)

我想在textFormField中实现建议。如下所示

所以,我已经搜索了关于这个,但没有成功。到处都是我得到的建议内的列表。这是很容易做到的。如果你有任何建议,然后请添加您的宝贵答案和评论。
下面是我的代码

Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    TextFormField(
                      controller: controller,
                      onFieldSubmitted: (value) {},
                      onChanged: (value) {
                        displaySuggestionInList(value);
                      },
                    ),
                    const SizedBox(height: 30),
                    ConstrainedBox(
                      constraints: const BoxConstraints(
                        maxHeight: 100,
                        maxWidth: 200,
                        minWidth: 200,
                        minHeight: 100,
                      ),
                      child: ListView.builder(
                        shrinkWrap: true,
                        itemCount: dashboardLayouts!.length,
                        itemBuilder: (context, index) {
                          return Text((dashboardLayouts![index]['dashBoardData']
                                  as DashboardInfo)
                              .commonName
                              .toString());
                        },
                      ),
                    )
                  ],
                ),
noj0wjuj

noj0wjuj1#

你需要创建一个Type-Ahead Widget。要做到这一点,你首先要创建一个普通的List建议StatefulWidget。在filter函数中,你应该用建议列表中的第一个值来更新提示。这样你就可以调用提示值,并将其放置在屏幕上的任何地方。不幸的是,flutter不允许在输入时在输入字段中更新显示提示。
不过,我举了个例子让你明白。

class AutocompleteExample extends StatefulWidget {
  const AutocompleteExample({super.key});

  @override
  State<AutocompleteExample> createState() => _AutocompleteExampleState();
}

class _AutocompleteExampleState extends State<AutocompleteExample> {
  TextEditingController controller = TextEditingController();
  List suggestionList = [];
  String hint = "";

  List<String> nameList = <String>[
    'aardvark',
    'bobcat',
    'chameleon',
    'Nathaniel Bond',
    'Taylor Story',
    'Lamont Padilla',
    'Jamia Sun',
    'Nikki Reichert',
    'Tea Holguin',
    'Rafael Meade',
    'Mercedez Goad',
    'Aileen Foltz',
    'Bryant Burt',
  ];

  void typeAheadFilter(String value) {
    suggestionList.clear();

    if (value.isEmpty) {
      setState(() {});
      return;
    }

    for (String name in nameList) {
      if (name.toLowerCase().contains(value)) {
        suggestionList.add(name);
      }
    }

    if (suggestionList.isNotEmpty) {
      var firstSuggestion = suggestionList[0];

      setState(() => hint = firstSuggestion);
    }

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        TextFormField(
          controller: controller,
          onFieldSubmitted: (value) {},
          onChanged: (value) => typeAheadFilter(value),
          decoration: InputDecoration(
              hintText: hint,
              labelText: hint.isEmpty ? "Search" : hint,
              alignLabelWithHint: true,
              hintTextDirection: TextDirection.rtl),
        ),
        const SizedBox(height: 10),
        if (suggestionList.isNotEmpty || controller.text.isNotEmpty) ...[
          Expanded(
            child: ListView.separated(
              padding: const EdgeInsets.all(10),
              shrinkWrap: true,
              itemCount: suggestionList.length,
              separatorBuilder: (context, index) => const Divider(),
              itemBuilder: (context, index) {
                return Text((suggestionList[index]));
              },
            ),
          )
        ] else ...[
          Expanded(
            child: ListView.separated(
              padding: const EdgeInsets.all(10),
              shrinkWrap: true,
              itemCount: nameList.length,
              separatorBuilder: (context, index) => const Divider(),
              itemBuilder: (context, index) {
                return Text((nameList[index]));
              },
            ),
          )
        ]
      ],
    );
  }
}

相关问题