FormController<TValue> class
Context for using the form.
How to use
First, initialize FormController with the values to be used in the form. In the example, Map is used, but it is safer to use an immutable class.
FormController should be saved using some state management method.
We will give key to the key
of Form and place the FormXX
widget under it.
Pass the value of FormController in the initialValue
of each form widget, and describe the process of rewriting the value of FormController in the onSaved
.
If the Submit button is pressed, validate is executed.
Each form's validate
is executed. Only when true
is returned from validate, onSaved
of each form is executed and the process of rewriting value of FormController is executed.
After that, execute the process of writing data to the server based on the value of FormController, etc.
フォームを利用していくためのコンテキスト。
基本的な使い方
まず、FormControllerにフォーム中で利用するための値を与えながら初期化します。 例ではMapが利用されてますが、immutableなクラスを利用したほうが安全です。
FormControllerはなにかしらの状態管理手法で保存してください。
Formのkey
にkeyを与え、その下にFormXX
ウィジェットを配置していきます。
それぞれのフォームウィジェットのinitialValue
にFormControllerのvalueを渡し、onSaved
にFormControllerのvalueを書き換える処理を記載します。
Submitボタンが押された場合、validateを実行します。
それぞれのフォームのvalidate
が実行されます。validateからtrue
が返された場合のみそれぞれのフォームのonSaved
が実行され、FormControllerのvalueを書き換える処理が走ります。
その後、FormControllerのvalueを元にサーバーにデータを書き込む処理等を実行してください。
class FormPage extends StatefulWidget {
const FormPage();
State<StatefulWidget> createState() => _FormPageState();
}
class _FormPageState extends State<FormPage> {
final _context = FormContext(<String, dynamic>{});
@override
Widget build(BuildContext context) {
return Form(
key: _context.key,
child: Column(
children: [
FormTextField(
hintText: "Input name",
initialValue: _context.value["name"],
onSaved: (value) => {...form.value, "name": value},
),
FormTextField(
hintText: "Input description",
initialValue: _context.value["description"],
onSaved: (value) => {...form.value, "description": value},
),
FormButton(
"Submit",
onPressed: () {
if(!_context.validate()){
return;
}
// Form data storage process.
}
),
]
),
);
}
}
- Inheritance
-
- Object
- ChangeNotifier
- ValueNotifier<
TValue> - FormController
- Available Extensions
Constructors
- FormController(TValue value)
- Context for using the form.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
-
key
→ GlobalKey<
FormState> -
GlobalKey passed to Form.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value ↔ TValue
-
The current value stored in this notifier.
getter/setter pairinherited
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
dispose(
) → void -
Discards any resources used by the object. After this is called, the
object is not in a usable state and should be discarded (calls to
addListener will throw after the object is disposed).
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
register(
FormFieldState state) → void -
Register FormFieldState to execute
validator
andonSaved
when validate is executed. -
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
unregister(
FormFieldState state) → void - Unregister FormFieldState.
-
validate(
{bool autoUnfocus = true}) → TValue? -
Validate all forms placed under Form to which key is passed, and if there are no problems,
onSaved
is executed and the value is returned. -
validateAndSave(
{bool autoUnfocus = true}) → bool -
Validation is performed on all forms placed under Form to which key is passed, and if there are no problems,
onSaved
is executed.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited