我有一个扩展为StatefulWidget
的窗口小部件。我已经决定让该组件返回formKey.currentState.save()
(或.currentState.validate()
)的值,所以我决定再创建一个扩展FormField
的小部件。现在我有了onSaved
,validator
等,但是想法是FormField
中的组件应该首先准备数据。并且此准备工作需要在onSaved
之前甚至在validator
函数之前完成。您是否知道如何完成?
这是示例类:
class MDClassInputFormField extends FormField<Set<MDClass>> {
MDClassInputFormField({
FormFieldSetter<Set<MDClass>> onSaved,
FormFieldValidator<Set<MDClass>> validator,
Set<MDClass> initialValue,
bool autovalidate = false
}) : super(
onSaved: onSaved,
validator: validator,
initialValue: initialValue,
autovalidate: autovalidate,
builder: (FormFieldState<Set<MDClass>> state) {
MDClassInput MDClassInput = MDClassInput();
return MDClassInput;
}
);
}
class MDClassInput extends StatefulWidget {
final bool onlySaved;
MDClassInput({this.onlySaved = false});
@override
_MDClassInputState createState() => _MDClassInputState();
}
class _MDClassInputState extends State<MDClassInput> {
// some variables here
@override
initState() {
super.initState();
// some logic here
}
@override
Widget build(BuildContext context) {
// return the Widget
}
// also here I have some logic
// sometimes I need to use widget.onlySaved (for example)
}
因此,结论是,在调用onSaved或Validator之前,我需要从_MDClassInputState
运行一些方法
有任何想法吗?谢谢!