我是颤振新手。
我需要在一个页面中有多个自动完成字段,以接收用户输入(重新输入或从附加到每个自动完成字段的下拉列表中进行选择)。
当用户点击页面底部的按钮时,所有自动完成字段中的此类输入都需要传递给变量,并最终提交到数据库 (ObjectBox)。
我的要求是这样的;我不需要为每个自动完成字段编写大约 25 行,而是需要一个简单的语句,例如;
return Column(
children: [
MyAutocomplete (someList, someLabel, someHint);
SizedBox(width: 10.00,),
MyAutocomplete (anotherList, anotherLabel, anotherHint);
SizedBox(width: 10.00,),
//several more MyAutocomplete fields
// the ElevatedButton comes here
])
Run Code Online (Sandbox Code Playgroud)
并且,将用户输入收集到每个字段的专用变量中。
我的问题是;如何在一个地方构建上述自定义的 MyAutocomplete 小部件以及如何在 UI 代码中调用并向其传递参数(如上所示)?
非常感谢您在这方面的帮助。
下面附加了带有默认自动完成小部件详细版本的示例代码(只有两个字段!)。
预先感谢您,
Widget twoAutoCompleteBoxes() {
const List<String> _kOptions = <String>['aardvark', 'bobcat', 'chameleon'];
return Column(
children: [
Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text == '') {
return const Iterable<String>.empty();
}
return _kOptions.where((String option) {
return option.contains(textEditingValue.text.toLowerCase());
});
},
fieldViewBuilder: (BuildContext …Run Code Online (Sandbox Code Playgroud)