Skip to content

Commit c956121

Browse files
authored
Revert "Remove single-view assumption from ScrollPhysics (#117503)" (#117647)
This reverts commit 999356b.
1 parent 894ea20 commit c956121

16 files changed

+41
-102
lines changed

dev/integration_tests/flutter_gallery/lib/demo/animation/home.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,14 @@ class _SnappingScrollPhysics extends ClampingScrollPhysics {
371371
return _SnappingScrollPhysics(parent: buildParent(ancestor), midScrollOffset: midScrollOffset);
372372
}
373373

374-
Simulation _toMidScrollOffsetSimulation(double offset, double dragVelocity, ScrollMetrics metrics) {
374+
Simulation _toMidScrollOffsetSimulation(double offset, double dragVelocity) {
375375
final double velocity = math.max(dragVelocity, minFlingVelocity);
376-
return ScrollSpringSimulation(spring, offset, midScrollOffset, velocity, tolerance: toleranceFor(metrics));
376+
return ScrollSpringSimulation(spring, offset, midScrollOffset, velocity, tolerance: tolerance);
377377
}
378378

379-
Simulation _toZeroScrollOffsetSimulation(double offset, double dragVelocity, ScrollMetrics metrics) {
379+
Simulation _toZeroScrollOffsetSimulation(double offset, double dragVelocity) {
380380
final double velocity = math.max(dragVelocity, minFlingVelocity);
381-
return ScrollSpringSimulation(spring, offset, 0.0, velocity, tolerance: toleranceFor(metrics));
381+
return ScrollSpringSimulation(spring, offset, 0.0, velocity, tolerance: tolerance);
382382
}
383383

384384
@override
@@ -396,10 +396,10 @@ class _SnappingScrollPhysics extends ClampingScrollPhysics {
396396
return simulation;
397397
}
398398
if (dragVelocity > 0.0) {
399-
return _toMidScrollOffsetSimulation(offset, dragVelocity, position);
399+
return _toMidScrollOffsetSimulation(offset, dragVelocity);
400400
}
401401
if (dragVelocity < 0.0) {
402-
return _toZeroScrollOffsetSimulation(offset, dragVelocity, position);
402+
return _toZeroScrollOffsetSimulation(offset, dragVelocity);
403403
}
404404
} else {
405405
// The user ended the drag with little or no velocity. If they
@@ -408,10 +408,10 @@ class _SnappingScrollPhysics extends ClampingScrollPhysics {
408408
// otherwise snap to zero.
409409
final double snapThreshold = midScrollOffset / 2.0;
410410
if (offset >= snapThreshold && offset < midScrollOffset) {
411-
return _toMidScrollOffsetSimulation(offset, dragVelocity, position);
411+
return _toMidScrollOffsetSimulation(offset, dragVelocity);
412412
}
413413
if (offset > 0.0 && offset < snapThreshold) {
414-
return _toZeroScrollOffsetSimulation(offset, dragVelocity, position);
414+
return _toZeroScrollOffsetSimulation(offset, dragVelocity);
415415
}
416416
}
417417
return simulation;

packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ class _DraggableScrollableSheetScrollPosition extends ScrollPositionWithSingleCo
902902
bool get _isAtSnapSize {
903903
return extent.snapSizes.any(
904904
(double snapSize) {
905-
return (extent.currentSize - snapSize).abs() <= extent.pixelsToSize(physics.toleranceFor(this).distance);
905+
return (extent.currentSize - snapSize).abs() <= extent.pixelsToSize(physics.tolerance.distance);
906906
},
907907
);
908908
}
@@ -937,7 +937,7 @@ class _DraggableScrollableSheetScrollPosition extends ScrollPositionWithSingleCo
937937
initialVelocity: velocity,
938938
pixelSnapSize: extent.pixelSnapSizes,
939939
snapAnimationDuration: extent.snapAnimationDuration,
940-
tolerance: physics.toleranceFor(this),
940+
tolerance: physics.tolerance,
941941
);
942942
} else {
943943
// The iOS bouncing simulation just isn't right here - once we delegate
@@ -946,7 +946,7 @@ class _DraggableScrollableSheetScrollPosition extends ScrollPositionWithSingleCo
946946
// Run the simulation in terms of pixels, not extent.
947947
position: extent.currentPixels,
948948
velocity: velocity,
949-
tolerance: physics.toleranceFor(this),
949+
tolerance: physics.tolerance,
950950
);
951951
}
952952

@@ -965,7 +965,7 @@ class _DraggableScrollableSheetScrollPosition extends ScrollPositionWithSingleCo
965965
// Make sure we pass along enough velocity to keep scrolling - otherwise
966966
// we just "bounce" off the top making it look like the list doesn't
967967
// have more to scroll.
968-
velocity = ballisticController.velocity + (physics.toleranceFor(this).velocity * ballisticController.velocity.sign);
968+
velocity = ballisticController.velocity + (physics.tolerance.velocity * ballisticController.velocity.sign);
969969
super.goBallistic(velocity);
970970
ballisticController.stop();
971971
} else if (ballisticController.isCompleted) {

packages/flutter/lib/src/widgets/list_wheel_scroll_view.dart

+4-9
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ class FixedExtentMetrics extends FixedScrollMetrics {
316316
required super.viewportDimension,
317317
required super.axisDirection,
318318
required this.itemIndex,
319-
required super.devicePixelRatio,
320319
});
321320

322321
@override
@@ -327,7 +326,6 @@ class FixedExtentMetrics extends FixedScrollMetrics {
327326
double? viewportDimension,
328327
AxisDirection? axisDirection,
329328
int? itemIndex,
330-
double? devicePixelRatio,
331329
}) {
332330
return FixedExtentMetrics(
333331
minScrollExtent: minScrollExtent ?? (hasContentDimensions ? this.minScrollExtent : null),
@@ -336,7 +334,6 @@ class FixedExtentMetrics extends FixedScrollMetrics {
336334
viewportDimension: viewportDimension ?? (hasViewportDimension ? this.viewportDimension : null),
337335
axisDirection: axisDirection ?? this.axisDirection,
338336
itemIndex: itemIndex ?? this.itemIndex,
339-
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
340337
);
341338
}
342339

@@ -402,7 +399,6 @@ class _FixedExtentScrollPosition extends ScrollPositionWithSingleContext impleme
402399
double? viewportDimension,
403400
AxisDirection? axisDirection,
404401
int? itemIndex,
405-
double? devicePixelRatio,
406402
}) {
407403
return FixedExtentMetrics(
408404
minScrollExtent: minScrollExtent ?? (hasContentDimensions ? this.minScrollExtent : null),
@@ -411,7 +407,6 @@ class _FixedExtentScrollPosition extends ScrollPositionWithSingleContext impleme
411407
viewportDimension: viewportDimension ?? (hasViewportDimension ? this.viewportDimension : null),
412408
axisDirection: axisDirection ?? this.axisDirection,
413409
itemIndex: itemIndex ?? this.itemIndex,
414-
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
415410
);
416411
}
417412
}
@@ -510,8 +505,8 @@ class FixedExtentScrollPhysics extends ScrollPhysics {
510505
// Scenario 3:
511506
// If there's no velocity and we're already at where we intend to land,
512507
// do nothing.
513-
if (velocity.abs() < toleranceFor(position).velocity
514-
&& (settlingPixels - metrics.pixels).abs() < toleranceFor(position).distance) {
508+
if (velocity.abs() < tolerance.velocity
509+
&& (settlingPixels - metrics.pixels).abs() < tolerance.distance) {
515510
return null;
516511
}
517512

@@ -524,7 +519,7 @@ class FixedExtentScrollPhysics extends ScrollPhysics {
524519
metrics.pixels,
525520
settlingPixels,
526521
velocity,
527-
tolerance: toleranceFor(position),
522+
tolerance: tolerance,
528523
);
529524
}
530525

@@ -535,7 +530,7 @@ class FixedExtentScrollPhysics extends ScrollPhysics {
535530
metrics.pixels,
536531
settlingPixels,
537532
velocity,
538-
toleranceFor(position).velocity * velocity.sign,
533+
tolerance.velocity * velocity.sign,
539534
);
540535
}
541536
}

packages/flutter/lib/src/widgets/nested_scroll_view.dart

-4
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ class _NestedScrollMetrics extends FixedScrollMetrics {
526526
required super.pixels,
527527
required super.viewportDimension,
528528
required super.axisDirection,
529-
required super.devicePixelRatio,
530529
required this.minRange,
531530
required this.maxRange,
532531
required this.correctionOffset,
@@ -539,7 +538,6 @@ class _NestedScrollMetrics extends FixedScrollMetrics {
539538
double? pixels,
540539
double? viewportDimension,
541540
AxisDirection? axisDirection,
542-
double? devicePixelRatio,
543541
double? minRange,
544542
double? maxRange,
545543
double? correctionOffset,
@@ -550,7 +548,6 @@ class _NestedScrollMetrics extends FixedScrollMetrics {
550548
pixels: pixels ?? (hasPixels ? this.pixels : null),
551549
viewportDimension: viewportDimension ?? (hasViewportDimension ? this.viewportDimension : null),
552550
axisDirection: axisDirection ?? this.axisDirection,
553-
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
554551
minRange: minRange ?? this.minRange,
555552
maxRange: maxRange ?? this.maxRange,
556553
correctionOffset: correctionOffset ?? this.correctionOffset,
@@ -818,7 +815,6 @@ class _NestedScrollCoordinator implements ScrollActivityDelegate, ScrollHoldCont
818815
minRange: minRange,
819816
maxRange: maxRange,
820817
correctionOffset: correctionOffset,
821-
devicePixelRatio: _outerPosition!.devicePixelRatio,
822818
);
823819
}
824820

packages/flutter/lib/src/widgets/page_view.dart

+1-6
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ class PageMetrics extends FixedScrollMetrics {
273273
required super.viewportDimension,
274274
required super.axisDirection,
275275
required this.viewportFraction,
276-
required super.devicePixelRatio,
277276
});
278277

279278
@override
@@ -284,7 +283,6 @@ class PageMetrics extends FixedScrollMetrics {
284283
double? viewportDimension,
285284
AxisDirection? axisDirection,
286285
double? viewportFraction,
287-
double? devicePixelRatio,
288286
}) {
289287
return PageMetrics(
290288
minScrollExtent: minScrollExtent ?? (hasContentDimensions ? this.minScrollExtent : null),
@@ -293,7 +291,6 @@ class PageMetrics extends FixedScrollMetrics {
293291
viewportDimension: viewportDimension ?? (hasViewportDimension ? this.viewportDimension : null),
294292
axisDirection: axisDirection ?? this.axisDirection,
295293
viewportFraction: viewportFraction ?? this.viewportFraction,
296-
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
297294
);
298295
}
299296

@@ -496,7 +493,6 @@ class _PagePosition extends ScrollPositionWithSingleContext implements PageMetri
496493
double? viewportDimension,
497494
AxisDirection? axisDirection,
498495
double? viewportFraction,
499-
double? devicePixelRatio,
500496
}) {
501497
return PageMetrics(
502498
minScrollExtent: minScrollExtent ?? (hasContentDimensions ? this.minScrollExtent : null),
@@ -505,7 +501,6 @@ class _PagePosition extends ScrollPositionWithSingleContext implements PageMetri
505501
viewportDimension: viewportDimension ?? (hasViewportDimension ? this.viewportDimension : null),
506502
axisDirection: axisDirection ?? this.axisDirection,
507503
viewportFraction: viewportFraction ?? this.viewportFraction,
508-
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
509504
);
510505
}
511506
}
@@ -578,7 +573,7 @@ class PageScrollPhysics extends ScrollPhysics {
578573
(velocity >= 0.0 && position.pixels >= position.maxScrollExtent)) {
579574
return super.createBallisticSimulation(position, velocity);
580575
}
581-
final Tolerance tolerance = toleranceFor(position);
576+
final Tolerance tolerance = this.tolerance;
582577
final double target = _getTargetPixels(position, tolerance, velocity);
583578
if (target != position.pixels) {
584579
return ScrollSpringSimulation(spring, position.pixels, target, velocity, tolerance: tolerance);

packages/flutter/lib/src/widgets/scroll_metrics.dart

-10
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ mixin ScrollMetrics {
4646
double? pixels,
4747
double? viewportDimension,
4848
AxisDirection? axisDirection,
49-
double? devicePixelRatio,
5049
}) {
5150
return FixedScrollMetrics(
5251
minScrollExtent: minScrollExtent ?? (hasContentDimensions ? this.minScrollExtent : null),
5352
maxScrollExtent: maxScrollExtent ?? (hasContentDimensions ? this.maxScrollExtent : null),
5453
pixels: pixels ?? (hasPixels ? this.pixels : null),
5554
viewportDimension: viewportDimension ?? (hasViewportDimension ? this.viewportDimension : null),
5655
axisDirection: axisDirection ?? this.axisDirection,
57-
devicePixelRatio: devicePixelRatio ?? this.devicePixelRatio,
5856
);
5957
}
6058

@@ -126,10 +124,6 @@ mixin ScrollMetrics {
126124
/// The quantity of content conceptually "below" the viewport in the scrollable.
127125
/// This is the content below the content described by [extentInside].
128126
double get extentAfter => math.max(maxScrollExtent - pixels, 0.0);
129-
130-
/// The [FlutterView.devicePixelRatio] of the view that the [Scrollable]
131-
/// associated with this metrics object is drawn into.
132-
double get devicePixelRatio;
133127
}
134128

135129
/// An immutable snapshot of values associated with a [Scrollable] viewport.
@@ -143,7 +137,6 @@ class FixedScrollMetrics with ScrollMetrics {
143137
required double? pixels,
144138
required double? viewportDimension,
145139
required this.axisDirection,
146-
required this.devicePixelRatio,
147140
}) : _minScrollExtent = minScrollExtent,
148141
_maxScrollExtent = maxScrollExtent,
149142
_pixels = pixels,
@@ -177,9 +170,6 @@ class FixedScrollMetrics with ScrollMetrics {
177170
@override
178171
final AxisDirection axisDirection;
179172

180-
@override
181-
final double devicePixelRatio;
182-
183173
@override
184174
String toString() {
185175
return '${objectRuntimeType(this, 'FixedScrollMetrics')}(${extentBefore.toStringAsFixed(1)}..[${extentInside.toStringAsFixed(1)}]..${extentAfter.toStringAsFixed(1)})';

packages/flutter/lib/src/widgets/scroll_physics.dart

+10-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:math' as math;
66

77
import 'package:flutter/foundation.dart';
88
import 'package:flutter/gestures.dart';
9-
import 'package:flutter/painting.dart' show AxisDirection;
109
import 'package:flutter/physics.dart';
1110

1211
import 'binding.dart' show WidgetsBinding;
@@ -384,29 +383,16 @@ class ScrollPhysics {
384383
/// The spring to use for ballistic simulations.
385384
SpringDescription get spring => parent?.spring ?? _kDefaultSpring;
386385

387-
/// Deprecated. Call [toleranceFor] instead.
388-
@Deprecated(
389-
'Call toleranceFor instead. '
390-
'This feature was deprecated after v3.7.0-13.0.pre.',
391-
)
392-
Tolerance get tolerance {
393-
return toleranceFor(FixedScrollMetrics(
394-
minScrollExtent: null,
395-
maxScrollExtent: null,
396-
pixels: null,
397-
viewportDimension: null,
398-
axisDirection: AxisDirection.down,
399-
devicePixelRatio: WidgetsBinding.instance.window.devicePixelRatio,
400-
));
401-
}
386+
/// The default accuracy to which scrolling is computed.
387+
static final Tolerance _kDefaultTolerance = Tolerance(
388+
// TODO(ianh): Handle the case of the device pixel ratio changing.
389+
// TODO(ianh): Get this from the local MediaQuery not dart:ui's window object.
390+
velocity: 1.0 / (0.050 * WidgetsBinding.instance.window.devicePixelRatio), // logical pixels per second
391+
distance: 1.0 / WidgetsBinding.instance.window.devicePixelRatio, // logical pixels
392+
);
402393

403394
/// The tolerance to use for ballistic simulations.
404-
Tolerance toleranceFor(ScrollMetrics metrics) {
405-
return parent?.toleranceFor(metrics) ?? Tolerance(
406-
velocity: 1.0 / (0.050 * metrics.devicePixelRatio), // logical pixels per second
407-
distance: 1.0 / metrics.devicePixelRatio, // logical pixels
408-
);
409-
}
395+
Tolerance get tolerance => parent?.tolerance ?? _kDefaultTolerance;
410396

411397
/// The minimum distance an input pointer drag must have moved to
412398
/// to be considered a scroll fling gesture.
@@ -710,7 +696,7 @@ class BouncingScrollPhysics extends ScrollPhysics {
710696

711697
@override
712698
Simulation? createBallisticSimulation(ScrollMetrics position, double velocity) {
713-
final Tolerance tolerance = toleranceFor(position);
699+
final Tolerance tolerance = this.tolerance;
714700
if (velocity.abs() >= tolerance.velocity || position.outOfRange) {
715701
double constantDeceleration;
716702
switch (decelerationRate) {
@@ -854,7 +840,7 @@ class ClampingScrollPhysics extends ScrollPhysics {
854840

855841
@override
856842
Simulation? createBallisticSimulation(ScrollMetrics position, double velocity) {
857-
final Tolerance tolerance = toleranceFor(position);
843+
final Tolerance tolerance = this.tolerance;
858844
if (position.outOfRange) {
859845
double? end;
860846
if (position.pixels > position.maxScrollExtent) {

packages/flutter/lib/src/widgets/scroll_position.dart

-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ import 'package:flutter/scheduler.dart';
1212

1313
import 'basic.dart';
1414
import 'framework.dart';
15-
import 'media_query.dart';
1615
import 'notification_listener.dart';
1716
import 'page_storage.dart';
1817
import 'scroll_activity.dart';
1918
import 'scroll_context.dart';
2019
import 'scroll_metrics.dart';
2120
import 'scroll_notification.dart';
2221
import 'scroll_physics.dart';
23-
import 'view.dart';
2422

2523
export 'scroll_activity.dart' show ScrollHoldController;
2624

@@ -244,9 +242,6 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
244242
isScrollingNotifier.value = activity!.isScrolling;
245243
}
246244

247-
@override
248-
double get devicePixelRatio => MediaQuery.maybeDevicePixelRatioOf(context.storageContext) ?? View.of(context.storageContext).devicePixelRatio;
249-
250245
/// Update the scroll position ([pixels]) to a given pixel value.
251246
///
252247
/// This should only be called by the current [ScrollActivity], either during

packages/flutter/lib/src/widgets/scroll_position_with_single_context.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class ScrollPositionWithSingleContext extends ScrollPosition implements ScrollAc
176176
required Duration duration,
177177
required Curve curve,
178178
}) {
179-
if (nearEqual(to, pixels, physics.toleranceFor(this).distance)) {
179+
if (nearEqual(to, pixels, physics.tolerance.distance)) {
180180
// Skip the animation, go straight to the position as we are already close.
181181
jumpTo(to);
182182
return Future<void>.value();

packages/flutter/lib/src/widgets/scrollable.dart

-6
Original file line numberDiff line numberDiff line change
@@ -727,12 +727,6 @@ class ScrollableState extends State<Scrollable> with TickerProviderStateMixin, R
727727
}
728728

729729
void _handleDragCancel() {
730-
if (_gestureDetectorKey.currentContext == null) {
731-
// The cancel was caused by the GestureDetector getting disposed, which
732-
// means we will get disposed momentarily as well and shouldn't do
733-
// any work.
734-
return;
735-
}
736730
// _hold might be null if the drag started.
737731
// _drag might be null if the drag activity ended and called _disposeDrag.
738732
assert(_hold == null || _drag == null);

packages/flutter/test/material/scrollbar_test.dart

-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ void main() {
148148
pixels: 0.0,
149149
viewportDimension: 100.0,
150150
axisDirection: AxisDirection.down,
151-
devicePixelRatio: tester.binding.window.devicePixelRatio,
152151
);
153152
scrollPainter!.update(metrics, AxisDirection.down);
154153

0 commit comments

Comments
 (0)