I'm trying to customize the color of the valueIndicator text of a flutter slider depending of a condition, like this:
SliderThemeData(
valueIndicatorColor: Colors.transparent,
valueIndicatorTextStyle: TextStyle(color: _sliderValue > 0 ? Colors.orange : Colors.grey),
)
Currently, I calculated the tick divisions by 20. However, if for example the user is located in the tick 20 and then goes to the tick 0, the valueIndicatorTextStyle condition will be FALSE and the value indicator will take the color gray, but I don't see this change until the user moves again the thumb to the tick 20 (like a lazy rebuild)
When the user is located in the tick 20
When the user is located in the tick 0 (Here the text have to be gray
Someone can help me?
Thanks!
UPDATES
The following is the full code of my custom slider widget:
import 'package:flutter/material.dart';
class CustomSlider extends StatefulWidget {
const CustomSlider({Key? key}) : super(key: key);
@override
State<CustomSlider> createState() => _CustomSliderState();
}
class _CustomSliderState extends State<CustomSlider> {
late double _sliderValue;
late final int _currentDivisor;
late int _currentDivisions;
late int _maxPoints;
// late Color valueIndicatorColor;
@override
void initState() {
_currentDivisor = 20;
// valueIndicatorColor = Colors.orange;
_currentDivisions = 5;
_maxPoints = 100;
_sliderValue = _currentDivisor.toDouble();
super.initState();
}
void _updateCurrentSliderValue(double points) {
setState(() {
_sliderValue = points;
// valueIndicatorColor = _sliderValue == 0 ? Colors.grey : Colors.orange;
// print(_sliderValue == 0 ? "Colors.grey" : "Colors.orange");
});
}
@override
Widget build(BuildContext context) {
print("rebuild");
print(_sliderValue);
return SliderTheme(
data: SliderThemeData(
valueIndicatorColor: Colors.transparent,
disabledThumbColor: Colors.black,
// valueIndicatorTextStyle: TextStyle(color: valueIndicatorColor, fontWeight: FontWeight.bold),
valueIndicatorTextStyle: TextStyle(color: _sliderValue == 0.0 ? Colors.grey : Colors.orange, fontWeight: FontWeight.bold)),
child: Slider(
activeColor: _sliderValue > 0.0 ? Colors.black : Colors.grey.withOpacity(0.8),
inactiveColor: _sliderValue > 0.0 ? Colors.black : Colors.grey.withOpacity(0.8),
min: 0.0,
max: _maxPoints.toDouble(),
label: "${_sliderValue.toInt()} points",
value: _sliderValue,
divisions: _currentDivisions,
onChanged: _updateCurrentSliderValue));
}
}
