Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting initial value #18

Closed
JesusHdez960717 opened this issue Mar 7, 2023 · 2 comments
Closed

Setting initial value #18

JesusHdez960717 opened this issue Mar 7, 2023 · 2 comments

Comments

@JesusHdez960717
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants