flutter - 如何使下拉菜单在颤动中可重用
问题描述
我想让这个下拉列表可重复使用。当我想使用它时。我需要简单地调用该下拉列表并传递值。希望你理解这个问题:)
这是我尝试过的一些代码。
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);
},
),
);
},
),
),
);
解决方案
创建一个自定义类,如下所示
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'],
),
推荐阅读
- android - 将收到的通知存储在我的 Android 应用程序中?
- javascript - 调用javascript方法变量丢失'this'
- machine-learning - 在 Ubuntu 16.04 上卸载 Caffe
- angular - Angular - 将额外的参数传递给带有路由的下一个组件
- machine-learning - 利用强化学习中学到的策略的方法
- python - 我如何组织 SQLAlchemy 关系以便像 JSON 一样的 dict 访问?
- ios - 尝试制作一个相机应用程序,对 Swift 来说是新的
- solr - SolrJ addFilterQuery 被忽略
- python-3.x - 以列表格式获取 KMeans 之后的聚类点
- sql - 如何通过 sql 数据库中的自定义 id 获取特定产品和组的最大计数