Reputation: 7106
I want to change the color of the circular part of my widget. This color should be different from the activeColor
and inactiveColor
colors. This is my current code:
Slider(
value: sliderValue.toDouble(),
max: 100.0,
min: 0.0,
activeColor: Colors.red,
inactiveColor: Colors.grey,
onChanged: (double newValue) {
setState(() {
sliderValue = newValue.round();
});
},
),
Below is the resulting slider:
This is my goal:
Help please.
Upvotes: 2
Views: 14690
Reputation: 7106
I got the idea from all your answers. Thanks! Here is my resulting solution:
SliderTheme(
data: SliderThemeData(
thumbColor: Colors.red,
activeTrackColor: Colors.white,
inactiveTrackColor: Colors.grey,
),
child: Slider(
value: sliderValue.toDouble(),
max: 100.0,
min: 0.0,
onChanged: (double newValue) {
setState(() {
sliderValue = newValue.round();
});
},
),
),
Upvotes: 1
Reputation: 355
wrap with SliderTheme
SliderTheme(
data: SliderThemeData(
thumbColor: Colors.white,
child: Slider(
value: sliderValue.toDouble(),
max: 100.0,
min: 0.0,
activeColor: Colors.red,
inactiveColor: Colors.grey,
onChanged: (double newValue) {
setState(() {
sliderValue = newValue.round();
});
},
),
);
Upvotes: 2
Reputation: 54427
You can copy paste run full code below
You can use SliderTheme
and set activeTrackColor
and inactiveTrackColor
code snippet
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.white,
inactiveTrackColor: Colors.grey,
trackShape: RectangularSliderTrackShape(),
trackHeight: 4.0,
thumbColor: Colors.redAccent,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 12.0),
overlayColor: Colors.grey,
overlayShape: RoundSliderOverlayShape(overlayRadius: 28.0),
),
child: Slider(
value: sliderValue.toDouble(),
working demo
full code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
int sliderValue = 50;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.black,
child: SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.white,
inactiveTrackColor: Colors.grey,
trackShape: RectangularSliderTrackShape(),
trackHeight: 4.0,
thumbColor: Colors.redAccent,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 12.0),
overlayColor: Colors.grey,
overlayShape: RoundSliderOverlayShape(overlayRadius: 28.0),
),
child: Slider(
value: sliderValue.toDouble(),
max: 100.0,
min: 0.0,
onChanged: (double newValue) {
setState(() {
sliderValue = newValue.round();
});
},
),
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Upvotes: 5
Reputation: 2714
the part you want to change is controlled with the SliderTheme
you can read the whole part in the docs: https://api.flutter.dev/flutter/material/SliderThemeData-class.html
just set the value to your fits.
SliderTheme(
data: SliderThemeData(
thumbColor: Colors.red,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 20)),
child: Slider(
...
},
),
),
And your active color to Colors.white
Upvotes: 7