reactive_forms 2.0.8

Flutter Android iOS web

This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular Reactive Forms.

Reactive Forms #

This is a model-driven approach to handling Forms inputs and validations, heavily inspired in Angular's Reactive Forms.

Pub Version GitHub GitHub top language flutter tests Codacy Badge codecov

Getting Started #

For help getting started with Flutter, view the online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Minimum Requirements #

  • Dart SDK: >=2.7.0 <3.0.0
  • Flutter: >= 1.17.0

Installation and Usage #

Once you're familiar with Flutter you may install this package adding reactive_forms to the dependencies list of the pubspec.yaml file as follow:

dependencies:
  flutter:
    sdk: flutter

  reactive_forms: ^2.0.8

Then run the command flutter packages get on the console.

Creating a form #

A form is composed by multiple fields or controls.

To declare a form with the fields name and email is as simple as:

final form = FormGroup({
  'name': FormControl(defaultValue: 'John Doe'),
  'email': FormControl(),
});

Default Values #

Notice in the example above that in the case of the name we have also set a default value, in the case of the email the default value is null.

How to get Form data #

You can get the value of a single FormControl as simple as:

String get name() => this.form.control('name').value;

But you can also get the complete Form data as follows:

final form = FormGroup({
  'name': FormControl(defaultValue: 'John Doe'),
  'email': FormControl(defaultValue: 'johndoe@email.com'),
});

print(form.value);

The previous code prints the following output:

{
  "name": "John Doe",
  "email": "johndoe@email.com"
}

FormGroup.value returns an instance of Map<String, dynamic> with each field and its value.

What about Validators? #

You can add validators to a FormControl as follows:

final form = FormGroup({
  'name': FormControl(validators: [Validators.required]),
  'email': FormControl(validators: [
    Validators.required,
    Validators.email,
  ]),
});

If at least one FormControl is invalid then the FormGroup is invalid

There are common predefined validators, but you can implement custom validators too.

Predefined validators #

  • Validators.required
  • Validators.email
  • Validators.number
  • Validators.minLength
  • Validators.maxLength
  • Validators.pattern
  • Validators.creditCard

Custom Validators #

Lets implement a custom validator that validates empty white spaces value:

final form = FormGroup({
  'name': FormControl(validators: [
    Validators.required, 
    _emptyWhiteSpaces, // custom validator
  ]),
});
/// Validates if control has empty-white-spaces value
Map<String, dynamic> _emptyWhiteSpaces(AbstractControl control) {
  final error = {'required': true};

  if (control.value == null) {
    return error;
  } else if (control.value is String) {
    return control.value.trim().isEmpty ? error : null;
  }

  return null;
}

A custom FormControl validator is a function that receives the control to validate and returns a Map. If the the value of the control is correct the function must returns null otherwise returns a Map with a key and custom information, in the previous example we just set true as custom information.

You can see the implementation of predefined validators to see more examples. In fact the previous example is the current implementation of the required validator, but we have just change the names for demonstration purpose.

Pattern Validator #

Validator.pattern is a validator that comes with Reactive Forms. Validation using regular expressions have been always a very useful tool to solve validation requirements. Lets see how we can validate American Express card numbers:

American Express card numbers start with 34 or 37 and have 15 digits.

const AmericanExpressPattern = r'^3[47][0-9]{13}$';

final cardNumber = FormControl(
  validators: [Validators.pattern(AmericanExpressPattern)],
);

cardNumber.value = '395465465421'; // not a valid number

expect(cardNumber.valid, false);
expect(cardNumber.errors.containsKey('pattern'), true);

The above code is a Unit Test extracted from Reactive Forms tests.

If we print the value of FormControl.errors:

print(cardNumber.errors);

We will get a Map like this:

{
  "pattern": {
    "requiredPattern": "^3[47][0-9]{13}$", 
    "actualValue": 395465465421
  }
}

FormGroup validators #

There are special validators that can be attached to FormGroup. In the next section we will see an example of that.

What about Password and Password Confirmation? #

There are some cases where we want to implement a Form where a validation of a field depends on the value of another field. For example a sign-up form with email and emailConfirmation or password and passwordConfirmation.

For that cases we must implement a custom validator and attach it to the FormGroup, lets see an example:

final form = FormGroup({
  'name': FormControl(validators: [Validators.required]),
  'email': FormControl(validators: [Validators.required, Validators.email]),
  'password': FormControl(validators: [
    Validators.required,
    Validators.minLength(8),
  ]),
  'passwordConfirmation': FormControl(),
}, validators: [
  _mustMatch('password', 'passwordConfirmation')
]);

Notice the use of Validators.minLength(8)

In the previous code we have added two more fields to the form: password and passwordConfirmation, both fields are required and the password must be at least 8 characters length.

However the most important thing here is that we have attached a validator to the FormGroup. This validator is a custom validator and the implementation follows as:

Map<String, dynamic> _mustMatch(String controlName, String matchingControlName) {
  return (AbstractControl control) {
    final form = control as FormGroup;

    final formControl = form.control(controlName);
    final matchingFormControl = form.control(matchingControlName);

    if (formControl.value != matchingFormControl.value) {
      matchingFormControl.addError({'mustMatch': true});

      // force messages to show up as soon as possible
      matchingFormControl.touch(); 
    } else {
      matchingFormControl.removeError('mustMatch');
    }

    return null;
  };
}

Fortunately you don't have to implement a custom must match validator because we have already included it into the code of the reactive_forms package so you could reuse it. The previous form definition example will become into:

final form = FormGroup({
  'name': FormControl(validators: [Validators.required]),
  'email': FormControl(validators: [Validators.required, Validators.email]),
  'emailConfirmation': FormControl(),
  'password': FormControl(validators: [
    Validators.required,
    Validators.minLength(8),
  ]),
  'passwordConfirmation': FormControl(),
}, validators: [
  FormGroupValidators.mustMatch('email', 'emailConfirmation'),
  FormGroupValidators.mustMatch('password', 'passwordConfirmation'),
]);

Asynchronous Validators 😎 #

Some times you want to perform a validation against a remote server, this operations are more time consuming and need to be done asynchronously.

For example you want to validate that the email the user is currently typing in a registration form is unique and is not already used in your application. Asynchronous Validators are just another tool so use it wisely.

Asynchronous Validators are very similar to their synchronous counterparts, with the following difference:

  • The validator function returns a Future

Asynchronous validation executes after the synchronous validation, and is performed only if the synchronous validation is successful. This check allows forms to avoid potentially expensive async validation processes (such as an HTTP request) if the more basic validation methods have already found invalid input.

After asynchronous validation begins, the form control enters a pending state. You can inspect the control's pending property and use it to give visual feedback about the ongoing validation operation.

Code speaks more than a thousand words :) so let's see an example.

Let's implement the previous mentioned example: the user is typing the email in a registration Form and you want to validate that the email is unique in your System. We will implement a custom async validator for that purpose.

final form = FormGroup({
  'email': FormControl<String>(
    validators: [
      Validators.required, // traditional required and email validators
      Validators.email,
    ],
    asyncValidators: [_uniqueEmail], // custom asynchronous validator :)
  ),
});

We have declared a simple Form with an email field that is required and must have a valid email value, and we have include a custom async validator that will validate if the email is unique. Let's see the implementation of our new async validator:

/// just a simple array to simulate a database of emails in a server
const inUseEmails = ['johndoe@email.com', 'john@email.com'];

/// Async validator example that simulates a request to a server
/// and validates if the email of the user is unique.
Future<Map<String, dynamic>> _uniqueEmail(AbstractControl control) async {
  final error = {'unique': false};

  final emailAlreadyUsed = await Future.delayed(
    Duration(seconds: 5), // a delay to simulate a time consuming operation
    () => inUseEmails.contains(control.value),
  );

  if (emailAlreadyUsed) {
    control.touch();
    return error;
  }

  return null;
}

Note the use of control.touch() to force the validation message to show up as soon as possible.

The previous implementation was a simple function that receives the AbstractControl and returns a Future that completes 5 seconds after its call and performs a simple check: if the value of the control is contained in the server array of emails.

If you want to see Async Validators in action with a full example using widgets and animations to feedback the user we strong advice you to visit our Wiki. We have not included the full example in this README.md file just to simplify things here and to not anticipate things that we will see later in this doc.

Debounce time in async validators #

Asynchronous validators have a debounce time that is useful if you want to minimize requests to a remote API. The debounce time is set in milliseconds and the default value is 250 milliseconds.

You can set a different debounce time as an optionally argument in the FormControl constructor.

final control = FormControl(
  asyncValidators: [_uniqueEmail],
  asyncValidatorsDebounceTime: 1000, // sets 1 second of debounce time.
);

Composing Validators #

To explain what Composing Validators is, let's see an example:

We want to validate a text field of an authentication form. In this text field the user can write an email or a phone number and we want to make sure that the information is correctly formatted: that is, if it is an email, it is a valid email and if it is a phone it must have a valid format.


final phonePattern = '<some phone regex pattern>';

final form = FormGroup({
  'user': FormControl<String>(
    validators: Validators.compose([
      Validators.email,
      Validators.pattern(phonePattern),
    ]),
  ),
});

Note that Validators.compose receives a collection of validators as argument and returns a collection of validators, this is intentional just to simplify code readability.

With Validators.compose we are saying to FormControl that if at least one validator evaluate as VALID then the control is VALID it's not necesary that both validators evaluate to valid.

Another example could be to validate multiples Credit Card numbers. In that case you have several regular expression patterns for each of the different kinds of credit cards. So the user can introduce a card number and if the information match with at least one of the cards the System recognice then the information is considered as valid.

final form = FormGroup({
  'cardNumber': FormControl<String>(
    validators: Validators.compose([
      Validators.pattern(americanExpressCardPattern),
      Validators.pattern(masterCardPattern),
      Validators.pattern(visaCardPattern),
    ]),
  ),
});

Groups of Groups 😁 #

FormGroup is not restricted to contains only FormControl, it can nest others FormGroup so you can create more complex Forms.

Supose you have a Registration Wizzard with several screens. Each screen collect specific information and at the end you want to collect all that information as one piece of data:

final form = FormGroup({
  'personal': FormGroup({
    'name': FormControl<String>(validators: [Validators.required]),
    'email': FormControl<String>(validators: [Validators.required]),
  }),
  'phone': FormGroup({
    'phoneNumber': FormControl<String>(validators: [Validators.required]),
    'countryIso': FormControl<String>(validators: [Validators.required]),
  }),
  'address': FormGroup({
    'street': FormControl<String>(validators: [Validators.required]),
    'city': FormControl<String>(validators: [Validators.required]),
    'zip': FormControl<String>(validators: [Validators.required]),
  }),
});

Note how we set the data type to a FormControl, this is not mandatory when define Forms but we recommend this syntax.

You can collect all data using FormGroup.value:

void _printFormData(FormGroup form) {
  print(form.value);
}

The previous method outputs a Map as the following one:

{
  "personal": {
    "name": "...",
    "email": "..."
  },
  "phone": {
    "phoneNumber": "...",
    "countryIso": "..."
  },
  "address": {
    "street": "...",
    "city": "...",
    "zip": "..."
  }
}

And of course you can access to a nested FormGroup as following:

FormGroup personalForm = form.control('personal');

A simple way to create a wizzard is for example to wrap a PageView within a ReactiveForm and each Page inside the PageView can contains a ReactiveForm to collect specific data.

Dynamic forms with FormArray #

FormArray is an alternative to FormGroup for managing any number of unnamed controls. As with FormGroup instances, you can dynamically insert and remove controls from FormArray instances, and the form array instance value and validation status is calculated from its child controls.

You don't need to define a key for each control by name, so this is a great option if you don't know the number of child values in advance.

Let's see a simple example:

final form = FormGroup({
  'emails': FormArray<String>([]), // an empty array of emails
});

We have defined just an empty array. Let's define another array with two controls:

final form = FormGroup({
  'emails': FormArray<String>([
    FormControl<String>(defaultValue: 'john@email.com'),
    FormControl<String>(defaultValue: 'susan@email.com'),
  ]),
});

Note that you don't have to specify the name of the controls inside of the array.

If we output the value of the previous form group we will get something like this:

void printFormValue(FormGroup form) {
  print(form.value);
}
{
  "emails": ["john@email.com", "susan@email.com"]
}

Lets dynamically add another control:

final array = form.control('emails') as FormArray<String>;

// adding another email
array.add(
  FormControl<String>(defaultValue: 'caroline@email.com'),
);

printFormValue(form);
{
  "emails": ["john@email.com", "susan@email.com", "caroline@email.com"]
}

Another way of add controls to an array:

// Given: an empty array of strings
final array = FormArray<String>([]);

// When: set a value to array
array.value = ["john@email.com", "susan@email.com", "caroline@email.com"];

// Then: the array is no longer empty
expect(array.controls.length, 3);

// And: array has a control for each inserted value
expect(array.controls('0').value, "john@email.com");
expect(array.controls('1').value, "susan@email.com");
expect(array.controls('2').value, "caroline@email.com");

To get a control from the array you must pass the index position as a String. This is because FormGroup and FormArray inherited from the same parent class and FormControl gets the controls by name (String).

A more advanced example:

// an array of contacts
final contacts = ['john@email.com', 'susan@email.com', 'caroline@email.com'];

// a form with a list of selected emails
final form = FormGroup({
  'selectedEmails': FormArray<bool>([], // an empty array of controls 
    validators: [emptyAddressee], // validates that at least one email is selected
  ), 
});

// get the array of controls
final formArray = form.control('selectedEmails') as FormArray<bool>;

// populates the array of controls.
// for each contact add a boolean form control to the array.
formArray.addAll(
  contacts.map((email) => FormControl<bool>(defaultValue: true)).toList(),
);
// validates that at least one email is selected
Map<String, dynamic> emptyAddressee(AbstractControl control) {
  final emails = (control as FormArray<bool>).value;
  return emails.any((isSelected) => isSelected)
      ? null
      : {'emptyAddressee': true};
}

FormBuilder #

The FormBuilder provides syntactic sugar that shortens creating instances of a FormGroup (for now). It reduces the amount of boilerplate needed to build complex forms.

final form = fb.group({
  'name': 'John Doe',
  'email': ['', Validators.required, Validators.email],
  'password': Validators.required,
});

The previous code is equivalent to the following one:

final form = FormGroup({
  'name': FormControl<String>(defaultValue: 'John Doe'),
  'email': FormControl<String>(defaultValue: '', validators: [Validators.required, Validators.email]),
  'password': FormControl(validators: [Validators.required]),
});

Reactive Form Widgets #

So far we have only defined our model-driven form, but how do we bind the form definition with our Flutter widgets? Reactive Forms Widgets is the answer ;)

Lets see an example:

@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveTextField(
          formControlName: 'name',
        ),
        ReactiveTextField(
          formControlName: 'email',
        ),
        ReactiveTextField(
          formControlName: 'password',
          obscureText: true,
        ),
      ],
    ),
  );
}

The example above ignores the emailConfirmation and passwordConfirmation fields previously seen for simplicity.

How to customize error messages? #

@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveTextField(
          formControlName: 'name',
          validationMessages: {
            'required': 'The name must not be empty'
          },
        ),
        ReactiveTextField(
          formControlName: 'email',
          validationMessages: {
            'required': 'The email must not be empty',
            'email': 'The email value must be a valid email'
          },
        ),
        ReactiveTextField(
          formControlName: 'password',
          obscureText: true,
          validationMessages: {
            'required': 'The password must not be empty',
            'minLenght': 'The password must have at least 8 characters'
          },
        ),
      ],
    ),
  );
}

Reactive Forms have an utility class called ValidationMessage that brings access to common validation messages: required, email, pattern and so on. So instead of write 'required' you could use ValidationMessage.required as the key of validation messages:

return ReactiveTextField(
   formControlName: 'email',
   validationMessages: {
     ValidationMessage.required: 'The email must not be empty',
     ValidationMessage.email: 'The email value must be a valid email',
   },
),

nice isn't it? ;)

When does Validation Messages begin to show up? #

Even when the FormControl is invalid, validation messages will begin to show up when the FormControl is touched. That means when the user taps on the ReactiveTextField widget and then remove focus or completes the text edition.

You can initialize a FormControl as touched to force the validation messages to show up at the very first time the widget builds.

final form = FormGroup({
  'name': FormControl(
    defaultValue: 'John Doe',
    validators: [Validators.required],
    touched: true,
  ),
});

Also when we set a value to a FormControl the validations messages begin to show up, even if the UI widget haven't been touched by the user. For example:

set name(String newName) {
  final formControl = this.form.control('name');
  formControl.value = newName; // if newName is invalid then messages will show up in UI
}

Enable/Disable Submit button #

For a better User Experience some times we want to enable/disable the Submit button based on the validity of the Form. Getting this behavior, even in such a great framework as Flutter, some times can be hard and can lead to have individual implementations for each Form of the same application plus boilerplate code.

We will show you two different approaches to accomplish this very easily:

  1. Separating Submit Button in a different Widget.
  2. Using ReactiveFormConsumer widget.

Separating Submit Button in a different Widget: #

Lets add a submit button to our Form:

@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveTextField(
          formControlName: 'email',
        ),
        ReactiveTextField(
          formControlName: 'password',
          obscureText: true,
        ),
        MySubmitButton(), 
      ],
    ),
  );
}

The above is a simple sign-in form with email, password, and a submit button.

Now lets see the implementation of the MySubmitButton widget:

class MySubmitButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final form = ReactiveForm.of(context);
    return RaisedButton(
      child: Text('Submit'),
      onPressed: form.valid ? _onPressed : null,
    );
  }

  void _onPressed() {
    print('Hello Reactive Forms!!!');
  }
}

Notice the use of ReactiveForm.of(context) to get access to the nearest FormGroup up the widget's tree.

In the previous example we have separated the implementation of the submit button in a different widget. The reasons behind this is that we want to re-build the submit button each time the validity of the FormGroup changes. We don't want to rebuild the entire Form, but just the button.

How is that possible? Well, the answer is in the expression:

final form = ReactiveForm.of(context);

The expression above have two important responsibilities:

  • Obtains the nearest FormGroup up the widget's tree.
  • Register the current context with the changes in the FormGroup so that if the validity of the FormGroup change then the current context is rebuilt.

The ReactiveForm widget has this behavior because is implemented using the InheritedNotifier.

Using ReactiveFormConsumer widget: #

ReactiveFormConsumer widget is a wrapped around the ReactiveForm.of(context) expression so that we can reimplement the previous example as follows:

@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveTextField(
          formControlName: 'email',
        ),
        ReactiveTextField(
          formControlName: 'password',
          obscureText: true,
        ),
        ReactiveFormConsumer(
          builder: (context, form, child) {
            return RaisedButton(
              child: Text('Submit'),
              onPressed: form.valid ? _onSubmit : null,
            );
          },
        ),
      ],
    ),
  );
}

void _onSubmit() {
  print('Hello Reactive Forms!!!');
}

It is entirely up to you to decide which of the above two approaches to use, but note that to access the FormGroup via ReactiveForm.of(context) the consumer widget must always be down in the tree of the ReactiveForm widget.

Focus/UnFocus a FormControl #

There are some cases where we want to add or remove focus on a UI TextField without the interaction of the user. For that particular cases you can use FormControl.focus() or FormControl.unfocus() methods.

final form = FormGroup({
  'name': FormControl(defaultValue: 'John Doe'),
});

final formControl = form.control('name');

formControl.focus(); // UI text field get focus and the device keyboard pop up

formControl.unfocus(); // UI text field lose focus

Focus flow between Text Fields #

Another example is when you have a form with several text fields and each time the user completes edition in one field you wnat to request next focus field using the keyboard actions:

final form = FormGroup({
  'name': FormControl<String>(validators: [Validators.required]),
  'email': FormControl<String>(validators: [Validators.required, Validators.email]),
  'password': FormControl<String>(validators: [Validators.required]),
});
/// Gets the email
FormControl get email => this.form.control('email');

/// Gets the password
FormControl get password => this.form.control('password');
@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveTextField(
          formControlName: 'name',
          textInputAction: TextInputAction.next,
          onSubmitted: () => this.email.focus(), // email text field request focus
        ),
        ReactiveTextField(
          formControlName: 'email',
          textInputAction: TextInputAction.next,
          onSubmitted: () => this.password.focus(), // password text field request focus
        ),
        ReactiveTextField(
          formControlName: 'password',
          obscureText: true,
        ),
      ],
    ),
  );
}

How does ReactiveTextField differs from native TextFormField or TextField? #

ReactiveTextField has more in common with TextFormField that with TextField. As we all know TextFormField is a wrapper around the TextField widget that brings some extra capabilities such as Form validations with properties like autovalidate and validator. In the same way ReactiveTextField is a wrapper around TextField that handle the features of validations in a own different way.

ReactiveTextField has all the properties that you can find in a common TextField, it can be customizable as much as you want just as a simple TextField or a TextFormField. In fact must of the code was taken from the original TextFormField and ported to have a reactive behavior that binds itself to a FormControl in a two-way binding.

Below is an example of how to create some ReactiveTextField with some common properties:

@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveTextField(
          formControlName: 'name',
          decoration: InputDecoration(
            labelText: 'Name',
          ),
          textCapitalization: TextCapitalization.words,
          textAlign: TextAlign.center,
          style: TextStyle(backgroundColor: Colors.white),
        ),
        ReactiveTextField(
          formControlName: 'phoneNumber',
          decoration: InputDecoration(
            labelText: 'Phone number',
          ),
          keyboardType: TextInputType.number,
        ),
        ReactiveTextField(
          formControlName: 'password',
          obscureText: true,
          decoration: InputDecoration(
            labelText: 'Password',
          ),
        ),
      ],
    ),
  );
}

Because of the two-binding capability of the ReactiveTextField with a FormControl the widget don't include properties as controller, validator, autovalidate, onSaved, onChanged, onEditingComplete, onFieldSubmitted, the FormControl is responsible for handling validation as well as changes notifications.

Supported Reactive Form Field Widgets #

  • ReactiveTextField
  • ReactiveDropdownField
  • ReactiveSwitch
  • ReactiveCheckbox
  • ReactiveRadio
  • ReactiveSlider
  • ReactiveCheckboxListTile
  • ReactiveSwitchListTile
  • ReactiveRadioListTile

Bonus Field Widgets #

  • ReactiveDatePicker
  • ReactiveTimePicker

Other Reactive Forms Widgets #

  • ReactiveForm
  • ReactiveFormConsumer
  • ReactiveFormArray
  • ReactiveValueListenableBuilder
  • ReactiveStatusListenableBuilder

ReactiveTextField #

We have explain the common usage of a ReactiveTextField along this documentation.

ReactiveDropdownField #

ReactiveDropdownField as all the other reactive field widgets is almost the same as its native version DropdownButtonFormField but adding two-binding capabilities. The code is ported from the original native implementation. It have all the capability of styles and themes of the native version.

final form = FormGroup({
  'payment': FormControl(validators: [Validators.required]),
});

@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveDropdownField<int>(
          formControlName: 'payment',
          hint: Text('Select payment...'),
          items: [
            DropdownMenuItem(
              value: 0,
              child: Text('Free'),
            ),
            DropdownMenuItem(
              value: 1,
              child: Text('Visa'),
            ),
            DropdownMenuItem(
              value: 2,
              child: Text('Mastercard'),
            ),
            DropdownMenuItem(
              value: 3,
              child: Text('PayPal'),
            ),
          ],
        ),
      ],
    ),
  );
}

As you can see from the above example the usage of ReactiveDropdownField is almost the same as the usage of a common DropdownButtonFormField, except for the additional formControlName and validationMessages properties.

ReactiveValueListenableBuilder to listen when value changes in a FormControl #

If you want to rebuild a widget each time a FormControl value changes you could use the ReactiveValueListenableBuilder widget.

In the following example we are listening for changes in lightIntensity. We change that value with a ReactiveSlider and show all the time the value in a Text widget:

final form = FormGroup({
  'lightIntensity': FormControl<double>(defaultValue: 50.0),
});

@override
Widget build(BuildContext context) {
  return ReactiveForm(
    formGroup: this.form,
    child: Column(
      children: <Widget>[
        ReactiveValueListenableBuilder<double>(
          formControlName: 'lightIntensity',
          builder: (context, value, child) {
            return Text('lights at ${value.toStringAsFixed(2)}%');
          },
        ),
        ReactiveSlider(
          formControlName: 'lightIntensity',
          max: 100.0,
        ),
      ],
    )
  );
}

Reactive Forms + Provider plugin 💪 #

Although Reactive Forms can be used with any state management library or even without any one at all, Reactive Forms gets its maximum potential when is used in combination with a state management library like the Provider plugin.

This way you can separate UI logic from business logic and you can define the FormGroup inside a business logic class and then exposes that class to widgets with mechanism like the one Provider plugin brings.

How create a custom Reactive Widget? #

Reactive Forms is not limited just to common widgets in Forms like text, dropdowns, sliders switch fields and etc, you can easily create custom widgets that two-way binds to FormControls and create your own set of Reactive Widgets ;)

In our Wiki you can find a tutorial of how to create your custom Reactive Widget.

What is not Reactive Forms #

  • Reactive Forms is not a fancy widgets package. It is not a library that brings some new Widgets with new shapes, colors or animations. It lets you to decide the shapes, colors, and animations you want for your widgets, but frees you from the responsibility of gathering and validating the data. And keeps the data in sync between your model and your widgets.

  • Reactive Forms does not pretend to replace the native widgets that you commonly use in your Flutter projects like TextFormField, DropdownButtonFormField or CheckboxListTile. Instead of that it brings new two-way binding capabilities and much more features to those same widgets.

What is Reactive Forms #

  • Reactive Forms provides a model-driven approach to handling form inputs whose values change over time. It's heavily inspired in Angular Reactive Form.
  • It lets you focus on business logic and save you time from collect, validate and mantain synchronization between your models and widgets.
  • Remove boilerplate code and brings you the posibility to write clean code defining a separation between model and UI with minimal efforts.
  • And it integrates perfectly well with common state management libraries like Provider, Bloc and many others good libraries the community has created.

Migrate from 1.x to 2.x #

Events renamed:

  • In AbstractControl

    • onValueChanged to valueChanges
    • onStatusChanged to statusChanges
    • onTouched to touchChanges
  • In FormGroup and FormArray

    • onCollectionChanged to collectionChanges
  • In FormControl

    • onFocusChanged to focusChanges

All events are now Streams so previous codes like the following one:

final control = FormControl<String>();

// listen to control changes
control.onValueChanged.addListener((){
  // must access the control to get the value 
  print(control.value);
});

control.value = 'Hello Reactive Forms!';

converts now to:

final control = FormControl<String>();

// listen to control changes
control.valueChanges.listen((String value){
  // the value arrives from the arguments :)
  print(value);
});

control.value = 'Hello Reactive Forms!';
30
likes
110
pub points
64%
popularity

This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular Reactive Forms.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

johnpjm85@gmail.com

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on reactive_forms