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

ChoiceChip get unselected strangely #13

Open
evanse9 opened this issue Apr 14, 2024 · 3 comments
Open

ChoiceChip get unselected strangely #13

evanse9 opened this issue Apr 14, 2024 · 3 comments

Comments

@evanse9
Copy link

evanse9 commented Apr 14, 2024

Hi davigmacode, thank you for this sweet package😊. I've been utilizing the package you provided recently and noticed an interesting observation. In one of our projects, we incorporated ChoiceChip widgets both with and without the choice package. We found that when a ChoiceChip implemented without the choice package was selected, it inadvertently deselected other ChoiceChips implemented with the choice package. This behavior piqued my curiosity, prompting me to reach out to you for clarification.

@davigmacode
Copy link
Owner

Hi evanse9, Much appreciated! Would you mind sharing your code so I can learn from it?

@evanse9
Copy link
Author

evanse9 commented Apr 23, 2024

simple_list
choice_orginal
choice_FIXED

Hi, we were able to resolve the problem. I uploaded 3 images were you can see the List we created, the original Choice.inline widget code and the modified Choice.inline code. In the original code, we used the prewritten methods provided by the Choice package at the level of the 'selected' and 'onselected' properties which is
"selected: selection.selected(_c_k[i]),
onSelected: selection.onSelected( _c_k[i], ),"

but in the modified(fixed) version we created a variable (Selected_car_K) which can hold a certain string and compare it's value to the name(text written on the choice chip) of the choice chip, and if the names are the same, the state of that choice chip is changed and it get selected. This is the code
" selected: Selected_car_K == _c_k[i].K_name ,
onSelected: (value) {
setState(() {
Selected_car_K = value ? _c_k[i].K_name.toString() : 'all' ;
});
},"
So i think the problem was at the level of the state management or the prewritten select method called at the level of the properties 'selected' and 'onselected' or may be i implemented the widget wrongly.

@davigmacode
Copy link
Owner

I see! To manage the selected value in your choice widget, you have two approaches:

  1. Updating from outside the widget:

    Use the onChanged parameter to define a callback function that receives the newly selected value. This function can then update your selectedValue variable accordingly.

  2. Setting the initial value inside the widget:

    Set the value parameter directly to your selectedValue variable. This will automatically populate the widget with the current selection.

Here's a code example that demonstrates both approaches:

InlineChoice<String>.single(
  clearable: true,
  value: selectedValue, // Set initial value
  onChanged: (newValue) { // Handle value changes from outside
    setState(() {
      selectedValue = newValue;
    });
  },
  itemCount: choices.length,
  itemBuilder: (state, i) {
    return ChoiceChip(
      selected: state.selected(choices[i]),
      onSelected: state.onSelected(choices[i]),
      label: Text(choices[i]),
    );
  },
),

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