The RangeSlider widget in Flutter is similar to the Slider widget but allows users to select a range of values instead of just a single value. It's commonly used for selecting a range of values, such as specifying a range of dates, prices, or any other numerical intervals.
Attributes:
values (RangeValues):
- The current values of the range slider. It consists of two double values representing the start and end values of the selected range.
onChanged (ValueChanged<RangeValues>):
- A callback function that is called whenever the selected range changes.
min (double):
- The minimum value of the range slider's range.
max (double):
- The maximum value of the range slider's range.
divisions (int):
- The number of discrete divisions or intervals within the range slider's range.
labels (RangeLabels):
- Text labels displayed above the thumbs of the range slider. It consists of two strings representing the start and end labels of the selected range.
activeColor (Color):
- The color of the track and thumbs when the range slider is active (being dragged).
inactiveColor (Color):
- The color of the track when the range slider is inactive (not being dragged).
Example:
import 'package:flutter/material.dart';
class RangeSliderExample extends StatefulWidget {
@override
_RangeSliderExampleState createState() => _RangeSliderExampleState();
}
class _RangeSliderExampleState extends State<RangeSliderExample> {
RangeValues _currentRangeValues = RangeValues(25, 75);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('RangeSlider Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Range: ${_currentRangeValues.start} - ${_currentRangeValues.end}'),
SizedBox(height: 16),
RangeSlider(
values: _currentRangeValues,
min: 0,
max: 100,
divisions: 10,
labels: RangeLabels(
'${_currentRangeValues.start.round()}',
'${_currentRangeValues.end.round()}',
),
onChanged: (RangeValues values) {
setState(() {
_currentRangeValues = values;
});
},
activeColor: Colors.blue,
inactiveColor: Colors.grey,
),
],
),
),
);
}
}
void main() {
runApp(MaterialApp(
home: RangeSliderExample(),
));
}
In this example, a RangeSlider widget is used to allow users to select a range of values between 0 and 100. The current range values are displayed as text below the slider. When the user drags the thumbs, the selected range is updated accordingly. The activeColor is set to blue, and the inactiveColor is set to grey. This demonstrates how you can use the RangeSlider widget to create interactive and customizable range sliders in your Flutter app.