Slider in Flutter

18-Sep-2024

Learn how to use the Slider widget to get input from users and control values dynamically

A slider is a widget that allows users to select a value from a range by dragging their thumb along a track.
Flutter provides  Slider widget for this purpose.

There Are Some Common Attributes for Slider

valueValue property represents the current state of the Slider
minMinimum value of slider
max
Maximum value of slider

Here's a simple example of using a Slider in Flutter:

File open lib->main.dart file −



import 'package:flutter/material.dart';

void main() {
runApp(
MyApp()
);
}
class MyApp extends StatefulWidget{

@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
double _sliderValue = 0.0;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('Slider Example',
style:TextStyle(fontSize: 20,color: Colors.white)),
),
body:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Slider(
value: _sliderValue,
min: 0.0,
max: 100.0,
onChanged: (value) {
setState(() {
_sliderValue = value;

});
},
),
Text("Slider Value $_sliderValue",
style: TextStyle(fontSize: 20,color: Colors.blue),)
],
),
));

}
}



Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:


Comments