[Slider]The initial value of discrete slider should respect the discrete values (#103966)
Fixes #103965
This commit is contained in:
@@ -523,7 +523,7 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
|
||||
vsync: this,
|
||||
);
|
||||
enableController.value = widget.onChanged != null ? 1.0 : 0.0;
|
||||
positionController.value = _unlerp(widget.value);
|
||||
positionController.value = _convert(widget.value);
|
||||
_actionMap = <Type, Action<Intent>>{
|
||||
_AdjustSliderIntent: CallbackAction<_AdjustSliderIntent>(
|
||||
onInvoke: _actionHandler,
|
||||
@@ -623,6 +623,22 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
|
||||
return value * (widget.max - widget.min) + widget.min;
|
||||
}
|
||||
|
||||
double _discretize(double value) {
|
||||
assert(widget.divisions != null);
|
||||
assert(value >= 0.0 && value <= 1.0);
|
||||
|
||||
final int divisions = widget.divisions!;
|
||||
return (value * divisions).round() / divisions;
|
||||
}
|
||||
|
||||
double _convert(double value) {
|
||||
double ret = _unlerp(value);
|
||||
if (widget.divisions != null) {
|
||||
ret = _discretize(ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Returns a number between 0.0 and 1.0, given a value between min and max.
|
||||
double _unlerp(double value) {
|
||||
assert(value <= widget.max);
|
||||
@@ -771,7 +787,7 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
|
||||
link: _layerLink,
|
||||
child: _SliderRenderObjectWidget(
|
||||
key: _renderObjectKey,
|
||||
value: _unlerp(widget.value),
|
||||
value: _convert(widget.value),
|
||||
divisions: widget.divisions,
|
||||
label: widget.label,
|
||||
sliderTheme: sliderTheme,
|
||||
|
||||
@@ -101,6 +101,46 @@ class _StateDependentMouseCursor extends MaterialStateMouseCursor {
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('The initial value should respect the discrete value', (WidgetTester tester) async {
|
||||
final Key sliderKey = UniqueKey();
|
||||
double value = 0.20;
|
||||
final List<Offset> log = <Offset>[];
|
||||
final LoggingThumbShape loggingThumb = LoggingThumbShape(log);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: StatefulBuilder(
|
||||
builder: (BuildContext context, StateSetter setState) {
|
||||
final SliderThemeData sliderTheme = SliderTheme.of(context).copyWith(thumbShape: loggingThumb);
|
||||
return Material(
|
||||
child: Center(
|
||||
child: SliderTheme(
|
||||
data: sliderTheme,
|
||||
child: Slider(
|
||||
key: sliderKey,
|
||||
value: value,
|
||||
divisions: 4,
|
||||
onChanged: (double newValue) {
|
||||
setState(() {
|
||||
value = newValue;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(value, equals(0.20));
|
||||
expect(log.length, 1);
|
||||
expect(log[0], const Offset(212.0, 300.0));
|
||||
});
|
||||
|
||||
testWidgets('Slider can move when tapped (LTR)', (WidgetTester tester) async {
|
||||
final Key sliderKey = UniqueKey();
|
||||
double value = 0.0;
|
||||
|
||||
Reference in New Issue
Block a user