Flutter 如何使下拉菜单在颤振中重复使用
我想让这个下拉列表可以重复使用。当我想用的时候。我只需要调用下拉列表并传递值。希望你能理解这个问题:) 这是我试过的一些代码Flutter 如何使下拉菜单在颤振中重复使用,flutter,drop-down-menu,flutter-layout,reusability,flutter-dependencies,Flutter,Drop Down Menu,Flutter Layout,Reusability,Flutter Dependencies,我想让这个下拉列表可以重复使用。当我想用的时候。我只需要调用下拉列表并传递值。希望你能理解这个问题:) 这是我试过的一些代码 FormBuilder( key: _fbKey, autovalidate: true, initialValue: { 'country': 5, }, child: FormBuilderCustomField( attribute: "name", vali
FormBuilder(
key: _fbKey,
autovalidate: true,
initialValue: {
'country': 5,
},
child: FormBuilderCustomField(
attribute: "name",
validators: [
FormBuilderValidators.required(),
],
formField: FormField(
// key: _fieldKey,
enabled: true,
builder: (FormFieldState<dynamic> field) {
return InputDecorator(
decoration: InputDecoration(
labelText: "Select Country",
contentPadding: EdgeInsets.only(top: 10.0, bottom: 0.0),
border: InputBorder.none,
errorText: field.errorText,
),
child: DropdownButton(
isExpanded: true,
items: ["One", "Two"].map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),
value: field.value,
onChanged: (value) {
field.didChange(value);
},
),
);
},
),
),
);
FormBuilder(
键:fbKey,
自动验证:true,
初始值:{
"国家":5,,
},
子项:FormBuilderCustomField(
属性:“名称”,
验证器:[
FormBuilderValidators.required(),
],
formField:formField(
//键:_fieldKey,
启用:对,
生成器:(FormFieldState字段){
返回输入装饰器(
装饰:输入装饰(
标签文本:“选择国家”,
contentPadding:仅限边集(顶部:10.0,底部:0.0),
边框:InputBorder.none,
errorText:field.errorText,
),
孩子:下拉按钮(
是的,
项目:[“一”、“二”].map((可选){
返回下拉菜单项(
子项:文本(“$选项”),
价值:期权,
);
}).toList(),
value:field.value,
一旦更改:(值){
字段.didChange(值);
},
),
);
},
),
),
);
创建一个自定义类,如下所示
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
class DropDown extends StatelessWidget {
final GlobalKey fbKey;
final String attribute, labelText;
final List<String> itemsList;
DropDown({
Key key,
@required this.fbKey,
this.attribute,
this.labelText,
this.itemsList,
}) : super(key: key);
@override
Widget build(BuildContext context) {
// TODO: implement build
return FormBuilder(
key: fbKey,
autovalidate: true,
initialValue: {
'country': 5,
},
child: FormBuilderCustomField(
attribute: attribute,
validators: [
FormBuilderValidators.required(),
],
formField: FormField(
// key: _fieldKey,
enabled: true,
builder: (FormFieldState<dynamic> field) {
return InputDecorator(
decoration: InputDecoration(
labelText: labelText,
contentPadding: EdgeInsets.only(top: 10.0, bottom: 0.0),
border: InputBorder.none,
errorText: field.errorText,
),
child: DropdownButton(
isExpanded: true,
items: itemsList.map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),
value: field.value,
onChanged: (value) {
field.didChange(value);
},
),
);
},
),
),
);
}
}
如何获取选定值?
DropDown(
fbKey: _bfKey,
attribute: 'Name',
labelText: 'Select Country',
itemsList: ['One', 'Two'],
),