class Form extends StatefulWidget { /// Creates a container for form fields. /// /// The [child] argument must not be null. const Form({ super.key, required this.child, this.onWillPop, this.onChanged, AutovalidateMode? autovalidateMode, }) : assert(child != null), autovalidateMode = autovalidateMode ?? AutovalidateMode.disabled; /// Returns the closest [FormState] which encloses the given context. /// /// Typical usage is as follows: /// /// ```dart /// FormState form = Form.of(context); /// form.save(); /// ``` static FormState? of(BuildContext context) { final _FormScope? scope = context.dependOnInheritedWidgetOfExactType<_FormScope>(); return scope?._formState; } /// The widget below this widget in the tree. /// /// This is the root of the widget hierarchy that contains this form. /// /// {@macro flutter.widgets.ProxyWidget.child} final Widget child; /// Enables the form to veto attempts by the user to dismiss the [ModalRoute] /// that contains the form. /// /// If the callback returns a Future that resolves to false, the form's route /// will not be popped. /// /// See also: /// /// * [WillPopScope], another widget that provides a way to intercept the /// back button. final WillPopCallback? onWillPop; /// Called when one of the form fields changes. /// /// In addition to this callback being invoked, all the form fields themselves /// will rebuild. final VoidCallback? onChanged; /// Used to enable/disable form fields auto validation and update their error /// text. /// /// {@macro flutter.widgets.FormField.autovalidateMode} final AutovalidateMode autovalidateMode; @override FormState createState() => FormState(); }
这个构造函数接受了以下参数:
key
:表示组件的唯一标识符。它继承自StatefulWidget
类,是可选的。child
:表示组件的内容。它是必填的。onWillPop
:当表单即将被弹出(例如用户点击了返回按钮)时调用的回调函数。它是可选的。onChanged
:当表单内容发生改变时调用的回调函数。它是可选的。autovalidateMode
:表示表单自动验证的模式。它是可选的,默认值为AutovalidateMode.disabled
。