AbdullahChauhan / custom-dropdown

Custom dropdown widget allows to add highly customizable dropdown widget in your projects. Features includes Search on list data, Network search, Multi-selection and many more.
https://pub.dev/packages/animated_custom_dropdown
BSD 3-Clause "New" or "Revised" License
158 stars 73 forks source link

Setting initial value #18

Closed JesusHdez960717 closed 1 year ago

JesusHdez960717 commented 1 year ago

I have an implementation of this package as:

        CustomDropdown.searchRequest(
          items: [some items],
          futureRequest: futureRequest,
          hintText: 'Search something...',
          controller: _controllerForText,
          onChanged: (selected) {
            //some logic...
          },
        ),

It work perfect, but... How can I specify the selected item, for example, when editing some model, I start the view with every field fill with the current value, how do I put the value corresponding to this dropdown. Or if I have one dropdown that depend on other, B depend on A, when I select a item in A, B is updated with the info, and I update the view, but if I update the view A loose the selected item.

In defaults dropdown, there is a property 'value' with this function, what is the equivalent of this property in this package?

This is the one how worked for my:

DropdownButtonHideUnderline(
          child: DropdownButton<String>(
            hint: Text(
              'Select type',
              style: TextStyle(
                fontSize: 14,
                color: Theme.of(context).hintColor,
              ),
            ),
            items: items
                .map(
                  (item) => DropdownMenuItem<String>(
                    value: item,
                    child: Text(
                      item,
                      style: const TextStyle(
                        fontSize: 14,
                      ),
                    ),
                  ),
                )
                .toList(),
            value: initialValue, //this property is what I need
            onChanged: (value) {
              //some logic
            },
          ),
        )
AbdullahChauhan commented 1 year ago

Hey @JesusHdez960717 Thanks for asking.

You can specify your initial value or changing state value by using controller. final dataCtrl = TextEditingController(text: data)

Now, that data variable for controller text can hold your initial value or also the updated state[data] value. So in your case, when your model is ready for view, you can assign the value to controller so that dropdown knows something is passed as initial value.

I hope I answered your question, Thanks!

JesusHdez960717 commented 1 year ago

Thanks @AbdullahChauhan, your solution work perfectly. An amazing package, great work. 👍🏻