flutter-form-builder-ecosystem/flutter_form_builder

[FormBuilderDropdown]: Not reverting to initial value upon form reset

kris175 opened this issue · 3 comments

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.3.0

Platforms

  • Android
  • iOS
  • Linux
  • MacOS
  • Web
  • Windows

Flutter doctor

Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.22.1, on macOS 14.5 23F79 darwin-arm64, locale en-AU)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] IntelliJ IDEA Community Edition (version 2023.2.2)
[✓] VS Code (version 1.89.1)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

Minimal code example

Code sample
class MyForm extends StatefulWidget {
  const MyForm({super.key});

  @override
  _MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: FormBuilder(
        key: _formKey,
        child: Column(
          children: <Widget>[
            FormBuilderDropdown(
              name: 'dropdown',
              decoration: const InputDecoration(
                labelText: 'Select Option',
              ),
              // hint: Text('Select Option'), // Optional
              items: const [
                DropdownMenuItem(
                  value: 'Option 1',
                  child: Text('Option 1'),
                ),
                DropdownMenuItem(
                  value: 'Option 2',
                  child: Text('Option 2'),
                ),
                DropdownMenuItem(
                  value: 'Option 3',
                  child: Text('Option 3'),
                ),
              ],
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                print("BEFORE RESET");
                print(_formKey.currentState?.value);
                _formKey.currentState?.reset();
                print("AFTER RESET");
                print(_formKey.currentState?.value);
              },
              child: const Text('Reset'),
            ),
          ],
        ),
      ),
    );
  }
}

Current Behavior

The dropdown field is not reverting to the initial value upon form reset. It instead retains the value that has been selected prior to reset.

Expected Behavior

Dropdown field should be reset to initial value when the form is reset

Steps To Reproduce

  1. Run app
  2. Dropdown field has initial value as null. Select an option
  3. Press reset button

Aditional information

flutter: BEFORE RESET
flutter: {dropdown: Option 2}
flutter: AFTER RESET
flutter: {dropdown: Option 2}

Could be related to this issue - #1389

I think this is just a bug. When reset() is called, onChange gets called twice for FormBuilderDropdown widgets: first with the correct (original) value, then with the incorrect (current value). Net result: the value doesn't get reset to the original value. (flutter_form_builder: ^9.4.1)

... which is the same issue mentioned here: #1371