我很想创建一个 statefull 类,该类创建 formFiled 。为此,我需要执行以下操作:
class SelectCategories extends StatefulWidget{
SelectCategories();
@override
_SelectCategoriesState createState() => new _SelectCategoriesState();
}
class _SelectCategoriesState extends State<SelectCategories> extends FormField<List<number>>{
}
所以我需要扩展2类。什么是正确的方法? 我想创建一个类别列表,并允许用户选择。它应该是表格的一部分。当用户单击类别时,我需要更改状态以指示它
答案 0 :(得分:0)
您可以引用此软件包的源代码
https://pub.dev/packages/flutter_form_builder
下拉菜单看起来像您所需要的
https://github.com/danvick/flutter_form_builder/blob/master/lib/src/fields/form_builder_dropdown.dart
以及您可以引用的验证器,此程序包提供了10多个builin验证器https://github.com/danvick/flutter_form_builder/blob/master/lib/src/form_builder_validators.dart
form_builder_dropdown.dart的代码段
它返回一个FormField,并在构建器中返回DropdownButton
@override
Widget build(BuildContext context) {
return FormField(
key: _fieldKey,
enabled: !_readonly,
initialValue: widget.initialValue,
validator: (val) {
for (int i = 0; i < widget.validators.length; i++) {
if (widget.validators[i](val) != null)
return widget.validators[i](val);
}
},
onSaved: (val) {
if (widget.valueTransformer != null) {
var transformed = widget.valueTransformer(val);
FormBuilder.of(context)
?.setAttributeValue(widget.attribute, transformed);
} else
_formState?.setAttributeValue(widget.attribute, val);
},
builder: (FormFieldState<dynamic> field) {
return InputDecorator(
decoration: widget.decoration.copyWith(
errorText: field.errorText,
border: InputBorder.none,
),
child: DropdownButton(
isExpanded: widget.isExpanded,
hint: widget.hint,
items: widget.items,
value: field.value,
style: widget.style,
isDense: widget.isDense,
disabledHint: widget.disabledHint,
elevation: widget.elevation,
iconSize: widget.iconSize,
onChanged: _readonly
? null