flutter-form-builder-ecosystem / form_builder_extra_fields

Additional ready-made form input fields for flutter_form_builder package
https://pub.dev/packages/form_builder_extra_fields
BSD 3-Clause "New" or "Revised" License
28 stars 48 forks source link

[FormBuilderTypeAhead]: initialValue doesn't work well doesn't appear #91

Closed zoelounge closed 5 months ago

zoelounge commented 11 months ago

Is there an existing issue for this?

Package/Plugin version

^10.1.0

Platforms

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.13.1, on macOS 14.1.1 23B81 darwin-arm64, locale it-IT) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2022.2) [✓] VS Code (version 1.84.2) [✓] Connected device (2 available) [✓] Network resources

Minimal code example

const allCountries = [
  'Afghanistan',
  'Albania',
  'Algeria',
  'American Samoa',
  'Andorra',
  'Angola',
  'Anguilla',
  'Antarctica',
  'Antigua and Barbuda',
  'Argentina',
  'Armenia',
  'Aruba',
  'Australia',
  'Austria',
  'Azerbaijan',
  'Bahamas',
  'Bahrain',
  'Bangladesh',
  'Barbados',
  'Belarus',
  'Belgium',
  'Belize',
  'Benin',
  'Bermuda',
  'Bhutan',
  'Bolivia',
  'Bosnia and Herzegowina',
  'Botswana',
  'Bouvet Island',
  'Brazil',
];

FormBuilderTypeAhead<String>(
                  decoration: const InputDecoration(
                    labelText: 'TypeAhead (Autocomplete TextField)',
                    hintText: 'Start typing country name',
                  ),
                  name: 'country',
                  onChanged: _onChanged,
                  itemBuilder: (context, country) {
                    return ListTile(title: Text(country));
                  },
                  controller: TextEditingController(text: ''),
                  initialValue: 'Uganda',
                  suggestionsCallback: (query) {
                    if (query.isNotEmpty) {
                      var lowercaseQuery = query.toLowerCase();
                      return allCountries.where((country) {
                        return country.toLowerCase().contains(lowercaseQuery);
                      }).toList(growable: false)
                        ..sort((a, b) => a
                            .toLowerCase()
                            .indexOf(lowercaseQuery)
                            .compareTo(
                                b.toLowerCase().indexOf(lowercaseQuery)));
                    } else {
                      return allCountries;
                    }
                  },
                ),

Current Behavior

InitialValue doesn't work well doesn't appear

Expected Behavior

initialValue should appear

Steps To Reproduce

Please see your code example in https://pub.dev/packages/form_builder_extra_fields/example

Aditional information

Thanks for your effort

LeonardoALARCON commented 8 months ago

The controller should have the initial value:

                  controller: TextEditingController(text: 'Uganda'),
                  initialValue: 'Uganda',

or you should not add the controller to the instatation of the FormBuilderTypeAhead, this way the controller will be created with the initialValue value

zoelounge commented 5 months ago

OK thx I resolved with : controller: TextEditingController(text: 'Uganda'), initialValue: 'Uganda',

zoelounge commented 5 months ago

OK thx I resolved with : controller: TextEditingController(text: 'Uganda'), initialValue: 'Uganda',