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

update selected value when menu is closed. #234

Open
djoenee opened this issue Jan 16, 2024 · 0 comments
Open

update selected value when menu is closed. #234

djoenee opened this issue Jan 16, 2024 · 0 comments

Comments

@djoenee
Copy link

djoenee commented Jan 16, 2024

Hello, is it possible to update the value of "selectedValue" from outside of the class?
(like when a button is pressed then selectedValue ="abc")
I tried a lot but the the value displays in the box only changes when taped on.
(I don't know how to trigger the Build methode)

void updateSelectedValue(String? newValue) async {
    print("Newvalue: $newValue");
    departValue = selectedValue;
    if (newValue != null) {
      departValue = newValue;
      selectedValue = newValue;
    } else {
      print("newvar is null");
    }
  }

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // print(stringList);
    print("buildvalue: $value");
    return Container(
      child: DropdownButtonHideUnderline(
        child: DropdownButton2<String>(
          key: _dropdwnKey,
          isExpanded: true,
          hint: Text(
            'Station',
            style: TextStyle(
              fontSize: 14,
              color: Bcolors.textSecondary,
            ),
          ),
          items: DropdwnState.stringList
              .map((item1) => DropdownMenuItem(
                    value: item1,
                    child: Text(
                      item1,
                      style: const TextStyle(
                        fontSize: 14,
                        color: Bcolors.textSecondary,
                      ),
                    ),
                  ))
              .toList(),
          value: selectedValue,
          **onChanged**: (value) async {
            setState(() {
              print("setState");
              selectedValue = value;
              updateSelectedValue(departValue);
              print("onchanged:$departValue");
            });
          },

The onchanged can trigger a setState , but I have not found another way to trigger a setState, updateSelectedValue would be idealy to to have a setState in ,but then it trouws an error.

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

1 participant