mirror of
https://github.com/flutter/flutter
synced 2024-08-25 02:46:31 +00:00
parent
ff9b1a5c22
commit
f733e3a441
|
@ -612,6 +612,11 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
|
|||
|
||||
bool _dragging = false;
|
||||
|
||||
// For discrete sliders, _handleChanged might receive the same value
|
||||
// multiple times. To avoid calling widget.onChanged repeatedly, the
|
||||
// value from _handleChanged is temporarily saved here.
|
||||
double? _currentChangedValue;
|
||||
|
||||
FocusNode? _focusNode;
|
||||
FocusNode get focusNode => widget.focusNode ?? _focusNode!;
|
||||
|
||||
|
@ -664,8 +669,11 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
|
|||
void _handleChanged(double value) {
|
||||
assert(widget.onChanged != null);
|
||||
final double lerpValue = _lerp(value);
|
||||
if (lerpValue != widget.value) {
|
||||
widget.onChanged!(lerpValue);
|
||||
if (_currentChangedValue != lerpValue) {
|
||||
_currentChangedValue = lerpValue;
|
||||
if (_currentChangedValue != widget.value) {
|
||||
widget.onChanged!(_currentChangedValue!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -676,6 +684,7 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
|
|||
|
||||
void _handleDragEnd(double value) {
|
||||
_dragging = false;
|
||||
_currentChangedValue = null;
|
||||
widget.onChangeEnd?.call(_lerp(value));
|
||||
}
|
||||
|
||||
|
|
|
@ -4253,4 +4253,32 @@ void main() {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
// This is a regression test for https://github.com/flutter/flutter/issues/143524.
|
||||
testWidgets('Discrete Slider.onChanged is called only once', (WidgetTester tester) async {
|
||||
int onChangeCallbackCount = 0;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: Slider(
|
||||
max: 5,
|
||||
divisions: 5,
|
||||
value: 0,
|
||||
onChanged: (double newValue) {
|
||||
onChangeCallbackCount++;
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final TestGesture gesture = await tester.startGesture(tester.getTopLeft(find.byType(Slider)));
|
||||
await tester.pump(kLongPressTimeout);
|
||||
await gesture.moveBy(const Offset(160.0, 0.0));
|
||||
await gesture.moveBy(const Offset(1.0, 0.0));
|
||||
await gesture.moveBy(const Offset(1.0, 0.0));
|
||||
expect(onChangeCallbackCount, 1);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue