flutter-form-builder-ecosystem / flutter_form_builder

Simple form maker for Flutter Framework
https://pub.dev/packages/flutter_form_builder
MIT License
1.49k stars 542 forks source link

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

Open kris175 opened 5 months ago

kris175 commented 5 months ago

Is there an existing issue for this?

Package/Plugin version

9.3.0

Platforms

Flutter doctor

Flutter doctor ```bash 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 ```dart class MyForm extends StatefulWidget { const MyForm({super.key}); @override _MyFormState createState() => _MyFormState(); } class _MyFormState extends State { final _formKey = GlobalKey(); @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.all(16.0), child: FormBuilder( key: _formKey, child: Column( children: [ 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}
kris175 commented 5 months ago

Could be related to this issue - https://github.com/flutter-form-builder-ecosystem/flutter_form_builder/pull/1389

thandal commented 2 months ago

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)

thandal commented 2 months ago

... which is the same issue mentioned here: https://github.com/flutter-form-builder-ecosystem/flutter_form_builder/issues/1371