title: Flutter conditional rendering of 2 forms - duplicates the form values to the other form
body: <p>I'm trying to do conditional rendering of two forms, using flutter <a href="https://pub.dev/packages/reactive_forms" rel="nofollow noreferrer">reactive_forms</a>. But when I type into one form, then click a button which renders the other form in the exact same place, the same values remain in the other form as well. The only thing that changes in the form control labels. The forms just become a duplicate of each other. I've tried so many things to get the values not to duplicate, to no avail. One of the worst programming experiences I've had for a long time.</p>
  <p>Here is my <code>FormGroup</code> which I was initially using for both forms because I do want most of the fields to duplicate across, just not all of them, such as &quot;groceryStore&quot; and &quot;restaurant&quot;, are not shared between the two forms. I have even tried using a separate <code>FormGroup</code> for each with all completely unique field names... which I assumed would be the cause of the duplication initially but when changing to two separate <code>FormGroup</code>'s with all different names it did not help. It's like it is in the same place on the page as the other conditional form field, so it will just keep its value. When I leave the page and return to it the form that I view first has the correct, different values. My code:</p>
  <pre><code>  FormGroup form = FormGroup({
      VeganItemFieldNames.name:
          FormControl&lt;String&gt;(validators: [Validators.required]),
      VeganItemFieldNames.brand:
          FormControl&lt;String&gt;(validators: [Validators.required]),
      VeganItemFieldNames.description:
          FormControl&lt;String&gt;(validators: [Validators.required]),
      VeganItemFieldNames.price:
          FormControl&lt;double&gt;(validators: [Validators.required]),
      VeganItemFieldNames.groceryStore:
          FormControl&lt;String&gt;(validators: [Validators.required]),
      VeganItemFieldNames.restaurant:
          FormControl&lt;String&gt;(validators: [Validators.required]),
      // VeganItemFieldNames.groceryItemCategories: FormControl&lt;String&gt;(),
      // VeganItemFieldNames.menuItemCategories: FormControl&lt;String&gt;(),
      VeganItemFieldNames.image:
          FormControl&lt;String&gt;(validators: [Validators.required]),
    });
  </code></pre>
  <p>My view:</p>
  <pre><code>child: ReactiveForm(
                        formGroup: viewModelState.form,
                        child: Column(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                  flex: 7,
                                  child: KeyboardAvoider(
                                      autoScroll: true,
                                      child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          mainAxisAlignment:
                                              MainAxisAlignment.start,
                                          children: [
                                            if (viewModel.addVeganItemStrategy !=
                                                null)
                                              ...viewModel.buildFields(context)
                                          ]))),
                              Container(
                                  padding: const EdgeInsets.fromLTRB(
                                      PAD_0, PAD_1, PAD_0, PAD_3),
                                  child: Center(
                                      child: VpSubmitButton(
                                          text: 'Submit', onPressed: () {})))
                            ]))
  </code></pre>
  <p>The ViewModel:</p>
  <pre><code>  List&lt;Widget&gt; buildFields(BuildContext context1) {
      return addVeganItemStrategy.buildFields(
          context: context, form: viewModelState.form);
    }
  </code></pre>
  <p>one strategy buildFields()</p>
  <pre><code>class AddGroceryItemStrategy implements AddVeganItemStrategy {
    List&lt;Widget&gt; buildFields({BuildContext context, FormGroup form}) {
      return [
        nameTextField(context: context, form: form),
        brandTextField(context: context),
        groceryStoreTextField(context: context),
        descriptionTextField(context: context),
        priceTextField(context: context),
        //   groceryItemCategoriesTextField(context: context, form: form),
        imageTextField(context: context),
      ];
    }
  
    Widget nameTextField(
        {BuildContext context, FormGroup form, String nextField}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_4, PAD_3_HALF, PAD_4),
          child: VpTextField(
              context: context,
              labelText: 'Name',
              maxLength: 40,
              formControlName: GroceryItemFieldNames.name,
              hintText: 'Name...',
              form: form,
              textInputAction: TextInputAction.next,
              nextField: nextField,
              validationMessages: (control) =&gt; {
                    'required': 'Please enter the Product Name',
                  }));
    }
  
    Widget brandTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            labelText: 'Brand',
            maxLength: 30,
            formControlName: GroceryItemFieldNames.brand,
            hintText: 'Brand...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Product Brand'},
          ));
    }
  
    Widget groceryStoreTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            labelText: 'Grocery Store',
            formControlName: GroceryItemFieldNames.groceryStore,
            hintText: 'Grocery Store...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Grocery Store'},
          ));
    }
  
    Widget descriptionTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            maxLength: 150,
            labelText: 'Description',
            formControlName: GroceryItemFieldNames.description,
            hintText: 'Description...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Product Description'},
          ));
    }
  
    Widget priceTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            textInputType: TextInputType.number,
            context: context,
            labelText: 'Price',
            formControlName: GroceryItemFieldNames.price,
            hintText: 'Price...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Product Price'},
          ));
    }
  
    Widget groceryItemCategoriesTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            labelText: 'Categories',
            formControlName: GroceryItemFieldNames.groceryItemCategories,
            hintText: 'Categories...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Product Categories'},
          ));
    }
  
    Widget imageTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            labelText: 'Image',
            formControlName: GroceryItemFieldNames.image,
            hintText: 'Image...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Product Image'},
          ));
    }
  }
  </code></pre>
  <p>Another strategy buildFields:</p>
  <pre><code>class AddMenuItemStrategy implements AddVeganItemStrategy {
    List&lt;Widget&gt; buildFields({BuildContext context, FormGroup form}) {
      return [
        nameTextField(context: context, form: form),
        restaurantTextField(context: context),
        descriptionTextField(context: context),
        priceTextField(context: context),
        //   menuItemCategoriesTextField(context: context, form: form),
        imageTextField(context: context),
      ];
    }
  
    Widget nameTextField(
        {BuildContext context, FormGroup form, String nextField}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_4, PAD_3_HALF, PAD_4),
          child: VpTextField(
              context: context,
              labelText: 'Name',
              maxLength: 40,
              formControlName: MenuItemFieldNames.name,
              hintText: 'Name...',
              form: form,
              textInputAction: TextInputAction.next,
              nextField: nextField,
              validationMessages: (control) =&gt; {
                    'required': 'Please enter the Product Name',
                  }));
    }
  
    Widget restaurantTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            labelText: 'Restaurant',
            formControlName: MenuItemFieldNames.restaurant,
            hintText: 'Restaurant...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Restaurant'},
          ));
    }
  
    Widget descriptionTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            maxLength: 150,
            labelText: 'Description',
            formControlName: MenuItemFieldNames.description,
            hintText: 'Description...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Menu Item Description'},
          ));
    }
  
    Widget priceTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            textInputType: TextInputType.number,
            context: context,
            labelText: 'Price',
            formControlName: MenuItemFieldNames.price,
            hintText: 'Price...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Menu Item Price'},
          ));
    }
  
    Widget menuItemCategoriesTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            labelText: 'Categories',
            formControlName: MenuItemFieldNames.menuItemCategories,
            hintText: 'Categories...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Menu Item Categories'},
          ));
    }
  
    Widget imageTextField({BuildContext context}) {
      return Container(
          padding:
              const EdgeInsets.fromLTRB(PAD_3_HALF, PAD_0, PAD_3_HALF, PAD_4),
          child: VpTextField(
            context: context,
            labelText: 'Image',
            formControlName: MenuItemFieldNames.image,
            hintText: 'Image...',
            validationMessages: (control) =&gt;
                {'required': 'Please enter the Menu Item Image'},
          ));
    }
  </code></pre>