Skip to content

Commit

Permalink
[Slider]The initial value of discrete slider should respect the discr…
Browse files Browse the repository at this point in the history
…ete values (#103966)

Fixes #103965
  • Loading branch information
xu-baolin authored May 25, 2022
1 parent 3fa355c commit 5a91967
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/flutter/lib/src/material/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
40 changes: 40 additions & 0 deletions packages/flutter/test/material/slider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5a91967

Please sign in to comment.