Flutter custom slider value with not equal period

Viewed 373

I can use slider with Slider class

  Slider(
    value: _currentLenValue,
    min: 1,
    max: 8,
    divisions: 8,
    label: _currentLenValue.round().toString(),
    onChanged: (double value) {
      setState(() {
        _currentLenValue = value;
      });
    },
  ),

This make the slider with value

[1,2,3,4,5,6,7,8]

However I want to set the custom value of slider, not equal period.

[0,4,8,16,24,32,128,512]

Is it possible or how can I make it?

1 Answers

You can either alter the function that gets the label of the slider:

  Slider(
    value: _currentLenValue,
    min: 1,
    max: 8,
    divisions: 8,
    label: [0,4,8,16,24,32,128,512][_currentLenValue.round()].toString(),
    onChanged: (double value) {
      setState(() {
        _currentLenValue = value;
      });
    },
  ),

Or alter the value in SetState:

  Slider(
    value: _currentLenValue,
    min: 1,
    max: 8,
    divisions: 8,
    label: _currentLenValue.toString(),
    onChanged: (double value) {
      setState(() {
        _currentLenValue = [0,4,8,16,24,32,128,512][value.round()];
      });
    },
  ),

Of course, indexing only goes well if the divisions are of size unity, so that rounding always yields the right index.

Related