Star Rating with Flutter Reactive Forms
Tags: flutter, starrating, reactive, form
Published: 2020-09-29 15:57:34

[Reactive Forms](https://pub.dev/packages/reactive_forms) is not limited just to common widgets in Forms like text inputs, dropdowns, sliders, and switches; you can easily create custom widgets that two-way binds to FormControls and create your own set of Reactive Widgets.

In this post, we will learn how to implement a Reactive Star Rating widget using **Reactive Forms**, and thus be able to create rich forms that can also collect information using a star rating bar.

To know more about **Reactive Forms** you can read my previous post [Why use Reactive Forms in Flutter?](https://dev.to/joanpablo/why-use-reactive-forms-in-flutter-ea1) or visit [github](https://github.com/joanpablo/reactive_forms) and [pub repo](https://pub.dev/packages/reactive_forms). You can also read the wiki page [Custom Reactive Widgets](https://github.com/joanpablo/reactive_forms/wiki/Custom-Reactive-Widgets).

This is what we are going to achieve at the end of this post:

![Reactive Star Rating](https://dev-to-uploads.s3.amazonaws.com/i/glwj17tfk5un3ww4xul1.gif)

We are not going to create a Star Rating Bar from zero, instead we are going to use an existing widget and convert it into a Reactive Widget. 

We will use the excellent plugin [flutter_rating_bar](https://pub.dev/packages/flutter_rating_bar) and give it the ability to two-way binds with a **FormControl**.

### Less talking and more coding ;)

```yaml
# import plugin in pubspec.yaml
dependencies:
  flutter_rating_bar: ^3.0.1+1
```
> flutter_rating_bar: ^3.0.1+1 was the last version at the time of the post.

Create our reactive widget, declare **ReactiveStarRating** and extends from **ReactiveFormField**:

```dart
/// A reactive star rating widget that two-way binds 
/// to a FormControl<double>.
class ReactiveStarRating extends ReactiveFormField<double> {
  //...

  @override
  ReactiveFormFieldState<double> createState() =>
      ReactiveFormFieldState<double>();
}
```

Then supply to the parent class the **formControlName** and the **builder** function:

```dart
/// A reactive star rating widget that two-way binds 
/// to a FormControl<double>.
class ReactiveStarRating extends ReactiveFormField<double> {
  /// Constructs an instance of [ReactiveStarRating].
  ///
  /// The argument [formControlName] must not be null.
  ReactiveStarRating({
    @required String formControlName,
  }) : super(
          formControlName: formControlName,
          builder: (ReactiveFormFieldState<double> field) {
            // RatingBar inner widget
            return RatingBar(
              // set UI value when control changes
              initialRating: field.value,
              // set control value when UI changes
              onRatingUpdate: field.didChange,
              itemBuilder: (context, _) => Icon(Icons.star),
            );
          },
        );

  @override
  ReactiveFormFieldState<double> createState() =>
      ReactiveFormFieldState<double>();
}
```

**Done**, say hello to our new reactive widget **ReactiveStarRating**.  

Wow but wait a minute, what just happened here, is that all? Yes, we are done with the reactive widget, but before we start using it, let me explain the above code a little bit.

### These are the steps:

1- Extend from **ReactiveFormField**. This is a StatefulWidget that maintains the state and is bound with the FormControl.
2- Provide a **builder** function in the parent's constructor (**super**). This function will return your inner widget, in this case, the **RatingBar** widget. The builder function will be called every time the **FormControl** changes its value.
3- Update the value of the FormControl with the method **didChange** method. Every time you want to update the value of the FormControl, call the **didChange** method of the ReactiveFormField passing the new value.

### How use it?
We are almost done, now let's consume our new ReactiveStarRating, we will also add some other reactive widgets that listen for changes in the FormControl just to visualize how they stay in sync with our new ReactiveStarRating:

```dart
class StarRatingSample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ReactiveFormBuilder(
        // define a form with a FormControl<double> field.
        form: () => fb.group({'rating': 0.0}),
        builder: (context, form, child) {
          return Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                // listen for changes in `rating` and show value
                ReactiveValueListenableBuilder<double>(
                  formControlName: 'rating',
                  builder: (context, control, child) {
                    return Text(
                      control.value.toStringAsFixed(1),
                      style: TextStyle(fontSize: 40.0),
                    );
                  },
                ),
                SizedBox(height: 20.0),
                // Bind our custom widget with `rating` control
                // just by provinding the control name.
                ReactiveStarRating(
                  formControlName: 'rating',
                ),
                SizedBox(height: 20.0),
                // Bind also a slider
                ReactiveSlider(
                  formControlName: 'rating',
                  divisions: 5,
                  min: 0,
                  max: 5,
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
```  

And that's it. This was a very simple example. I urge you to make the widget more configurable so that it allows you to change the background color of the stars and evaluate with a half star.

I hope you have enjoyed the post.

Thanks.