Flutter Slider Divisions
To set the number of divisions for a Slider widget in Flutter, set divisions
property of this Slider with required integer value. The property creates the specified number of discrete devisions on the slider track.
Sample Code
Slider(
divisions: 10,
),
Example
Flutter Application with a Slider widget.
divisions
attribute of this Slider is set to 10
.
main.dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// main application widget
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Application';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyStatefulWidget(),
),
);
}
}
/// stateful widget that the main application instantiates
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// private State class that goes with MyStatefulWidget
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
double _currentSliderValue = 20;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
Slider(
activeColor: Colors.red,
divisions: 10,
min: 0,
max: 100,
value: _currentSliderValue,
label: _currentSliderValue.round().toString(),
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
});
},
),
],
),
);
}
}
Screenshot