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

Select color by code control #50

Closed
stoneLee81 opened this issue Jul 6, 2021 · 2 comments
Closed

Select color by code control #50

stoneLee81 opened this issue Jul 6, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@stoneLee81
Copy link

stoneLee81 commented Jul 6, 2021

I want to flexibly control the selected color block through the code. I made a small demo and hardcoded a color 【0xffFFB400】 that did not exist in _defaultColors. When I selected the color. The expectation is that no color block will be selected, but when I click, it is still selected. The color block I clicked.
in debug mode, I found that _currColor value is 0xffFFB400, but widget is still select the wrong color block .

...
const List<Color> _defaultColors = [Color(0xfff44336),Color(0xffe91e63),Color(0xff9c27b0),Color(0xff673ab7),Color(0xff3f51b5),Color(0xff2196f3),Color(0xff03a9f4),Color(0xff00bcd4),Color(0xff009688),Color(0xff4caf50),Color(0xffcddc39),Color(0xffff9800)];
....
void changeColor(Color color) => setState(() => _currColor = Color(0xffFFB400));
...
SingleChildScrollView(child:BlockPicker(availableColors: _defaultColors, pickerColor: _currColor, onColorChanged: changeColor
@tsinis
Copy link
Contributor

tsinis commented Sep 3, 2021

Hey @stoneLee81 !

I'm not sure it's an issue with a package. You can just update your code with a color check, something like:

 void changeColor(Color color) {
    if (_defaultColors.contains(color)) {
      setState(() => _currColor = color);
    } else {
      throw Exception('The selected color is not on the list!');
      // and handle it via try/catch on other side, or just show a snackbar, etc.
    }
  }

If it's enough for your needs — please consider closing the issue. Thanks!

@dowski
Copy link

dowski commented Nov 13, 2021

I think what the OP was asking for is the ability to change the selected color in the picker from the outside.

Currently, there's no way to do that once the widget has been constructed. The selected color can only be changed by interaction with the widget's UI. In the example shared above, _currColor can only be used for setting the initial value of the picker. If it changes (based on reloading backing data or something) the picker doesn't pick up that change.

@mchome mchome added the bug Something isn't working label Nov 14, 2021
@mchome mchome closed this as completed in 9389d23 Nov 14, 2021
mchome added a commit that referenced this issue Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants