【问题标题】:Flutter auto-suggest TextFieldFlutter 自动提示 TextField
【发布时间】:2020-11-19 07:40:53
【问题描述】:

我正在开发一个 Flutter 应用程序,它在 Form 内包含一个 TextFormField/TextFieldTextFields 之一将是自动建议/完成。

TextField 输入发生变化时显示数据列表的正确小部件是什么?

【问题讨论】:

  • 查看autocomplete_textfield 包。
  • 我有两个要求 *\1。文本更改事件建议将从服务器检索(键,值) 2. 键将显示在建议中。在选择建议列表时,键将添加到文本字段,值应存储在变量中
  • 好的,我认为flutter_typeahead 包更接近你想要的。
  • 是的,它运行良好,但如果建议列表更多,则没有滚动操作。我将添加限制或找到滚动建议的方法。感谢链接

标签: forms autocomplete flutter textfield


【解决方案1】:

Flutter 即将推出 AutoComplete 功能,您可以在此 thread 中跟踪其进度。除了在 cmets 中使用上述软件包外,您还可以在等待 AutoComplete 功能发布时尝试将 RawAutocomplete 用于您的 Text[Form]Field。不过,您需要在master channel 上进行尝试。

这是demo 之后的示例。

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _textFieldHints = List<String>();
  var _textFieldController;

  @override
  void dispose() {
    super.dispose();
    _textFieldController.dispose();
  }

  _addAutoFillText() {
    if (_textFieldController.text.isEmpty) {
      // Don't add input text if TextFormField is empty
      debugPrint('_textFieldController.text is empty');
    } else {
      debugPrint('${_textFieldController.text} added');
      setState(() {
        // Add TextFormField String to List
        _textFieldHints.add(_textFieldController.text);
        // Clear TextFormField after adding
        _textFieldController.clear();
      });
      debugPrint('_textFieldHints contains:');
      _textFieldHints.sort();
      _textFieldHints.forEach((text) {
        debugPrint('$text');
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          padding: EdgeInsets.all(16.0),
          child: RawAutocomplete<String>(
            optionsBuilder: (TextEditingValue textEditingValue) {
              if (textEditingValue.text == null ||
                  textEditingValue.text == '') {
                return const Iterable<String>.empty();
              }
              return _textFieldHints.where((String option) {
                return option
                    .toLowerCase()
                    .contains(textEditingValue.text.toLowerCase());
              });
            },
            onSelected: (String selection) {
              debugPrint('$selection selected');
            },
            fieldViewBuilder: (BuildContext context,
                TextEditingController textEditingController,
                FocusNode focusNode,
                VoidCallback onFieldSubmitted) {
              _textFieldController = textEditingController;
              return TextFormField(
                controller: _textFieldController,
                focusNode: focusNode,
                onFieldSubmitted: (String value) {
                  onFieldSubmitted();
                },
              );
            },
            optionsViewBuilder: (BuildContext context,
                AutocompleteOnSelected<String> onSelected,
                Iterable<String> options) {
              return Align(
                alignment: Alignment.topLeft,
                child: Material(
                  elevation: 4.0,
                  child: Container(
                    height: 200.0,
                    child: ListView.builder(
                      padding: EdgeInsets.all(8.0),
                      itemCount: options.length + 1,
                      itemBuilder: (BuildContext context, int index) {
                        if (index >= options.length) {
                          return TextButton(
                            child: const Text('clear'),
                            onPressed: () {
                              _textFieldController.clear();
                            },
                          );
                        }
                        final String option = options.elementAt(index);
                        return GestureDetector(
                          onTap: () {
                            onSelected(option);
                          },
                          child: ListTile(
                            title: Text(option),
                          ),
                        );
                      },
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _addAutoFillText(),
        child: Icon(Icons.add),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 2017-09-24
    • 2019-05-09
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 2019-01-25
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多