Introduction
A slider lets users pick a value by moving a thumb along a line. It is easy and quick to choose numbers or settings.
A slider lets users pick a value by moving a thumb along a line. It is easy and quick to choose numbers or settings.
Slider(
value: currentValue,
min: minValue,
max: maxValue,
onChanged: (newValue) {
setState(() {
currentValue = newValue;
});
},
)Slider( value: 50, min: 0, max: 100, onChanged: (value) {}, )
Slider( value: volume, min: 0, max: 10, onChanged: (val) => setState(() => volume = val), )
This app shows a slider from 0 to 100 with steps of 5 (because divisions=20). The current value is shown below the slider and updates as you move the thumb.
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { double _sliderValue = 20; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Slider Example')), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Slider( value: _sliderValue, min: 0, max: 100, divisions: 20, label: _sliderValue.round().toString(), onChanged: (value) { setState(() { _sliderValue = value; }); }, ), Text('Value: ${_sliderValue.toStringAsFixed(1)}'), ], ), ), ), ); } }
Use divisions to make the slider snap to fixed steps.
The label shows a tooltip above the thumb while dragging.
Always update the slider value inside setState to refresh the UI.
Slider lets users pick a number by sliding a thumb along a line.
Set value, min, max, and onChanged to make it interactive.
Use divisions and label for better user experience.
Slider widget in Flutter?value, min, max, and onChanged properties exactly as in Slider(value: 0.5, min: 0, max: 1, onChanged: (v) => print(v)).onChanged callback. Slider(value: 0.5, min: 0, max: 1) misses it, causing an error.double sliderValue = 0.0;
Slider(
value: sliderValue,
min: 0,
max: 10,
divisions: 10,
label: (value) => value.round().toString(),
onChanged: (double value) {
print(value);
},
)Slider( value: 5, min: 0, max: 10, onChanged: null, )
onChanged callback to be interactive. Setting it to null disables the slider.Pass the test and this pattern moves unknown