Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable the Flutter slider class to prevent a user from moving it?

I'm hoping to render a flutter slider that is unmovable, meaning that I hope to pass the slider an initial value that the user will not be able to change purely for UI visualization?

Has anyone had any experience locking this slider in place?

Thanks for the help.

like image 280
PJQuakJag Avatar asked Oct 11 '18 15:10

PJQuakJag


People also ask

How do I disable widgets on Flutter?

In flutter, most widgets already come with an option to disable them for example in a RaisedButton we can set the onClicked function to null to disable, or we can use NeverScrollableScrollPhysics( ) to disable a ListView.

How do you customize slider in Flutter?

Customizing the slider color The basic Slider widget gives you access to three properties for setting its color: activeColor : Applies color to the active portion of the slider track. inactiveColor : Applies color to the inactive portion of the slider track. thumbColor : Applies color to the slider thumb.

How do you use sliders in Flutter?

A slider can be used to select from either a continuous or a discrete set of values. The default is to use a continuous range of values from min to max. To use discrete values, use a non-null value for divisions, which indicates the number of discrete intervals.

How do you always show label in slider Flutter?

Try set up divisions value to 9. You will see the label on each step of the values.


3 Answers

Give it a value and then set the onChanged: property to null.

onChanged: null

This disables the Slider.

Otherwise, you could give max: and min: the same value. In this case, though, the slider is not greyed out but it stays at zero.

like image 92
chemamolins Avatar answered Oct 11 '22 09:10

chemamolins


Just wrap an AbsorbPointer around your slider like so:

AbsorbPointer(
            child: Slider(
              onChanged: (value) => print(value),
              min: 0,
              max: 100,
              divisions: 10,
              value: 30,
            ),
          )
like image 32
Vishnu Nair Avatar answered Oct 11 '22 10:10

Vishnu Nair


In order to have your SliderTheme affect a disabled slider you have to overwrite the disabled attributes.

Here is a quick example:

SliderTheme(
  data: SliderTheme.of(context).copyWith(
    disabledActiveTrackColor: Colors.red[700],
    disabledInactiveTrackColor: Colors.red[100],
    disabledThumbColor: Colors.redAccent,
  ),
  child: Slider(
    value: 0.2,
    onChanged: null,
  ),
)
like image 28
Lulupointu Avatar answered Oct 11 '22 11:10

Lulupointu