LForm constructor

const LForm(
  1. {Key key,
  2. @required Widget child,
  3. LFormManager manager,
  4. bool autovalidate = false,
  5. WillPopCallback onWillPop,
  6. VoidCallback onChanged,
  7. VoidCallback onSubmit}
)

An optional container for grouping together multiple form field widgets (e.g. TextField widgets).

Each individual form field should be wrapped in a LFormField widget, with the LForm widget as a common ancestor of all of those. Call methods on LFormState to save, reset, access form/field state, serialize or validate each LFormField that is a descendant of this LForm. To obtain the LFormState, you may use LForm.of with a context whose ancestor is the LForm, or pass a LFormManager to the LForm constructor and call LFormManager.formState.

This example shows a LForm with one TextFormField to enter an email address and a LRaisedButton to submit the form. A LFormManager is used here to identify the LForm, access LFormField, their state, validate input and get serialize data.

final _manager = LFormManager(); // An effecient replacement of GlobalKey<LFormState>

@override
Widget build(BuildContext context) {
  return LForm(
    manager: _manager,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        LTextFormField(
          name: "email", // for serialization
          decoration: const InputDecoration(
            hintText: 'Enter your email',
          ),
          initialValue: 'xyz@xyz.com',
          validators: [
            LRequiredValidator(),
            LEmailValidator(
                invalidMessage: "Please enter correct email address")
          ],
        ),
        LRaisedButton.text(
          text: "Submit",
          margin: const EdgeInsets.symmetric(vertical: 16.0),
          onPressed: () {
            // Validate will return true if the form is valid, or false if
            // the form is invalid.

            print(_manager.formState.isDirty); // print if form is dirty

            if (_manager.formState.validate()) {
              // Get Serialized Data
              final data = _manager.formState.serialize();
              print(data); // {'email': 'xyz@xyz.com'}

            }
          },
        ),
      ],
    ),
  );
}

{@end-tool}

See also:

Implementation

const LForm({
  Key key,
  @required this.child,
  this.manager,
  this.autovalidate = false,
  this.onWillPop,
  this.onChanged,
  this.onSubmit,
})  : assert(child != null),
      super(key: key);