From 473d75a6e326c4b12329fa15b1aae11572d18290 Mon Sep 17 00:00:00 2001 From: Jeff McGlynn Date: Tue, 14 Nov 2017 14:34:43 -0800 Subject: [PATCH] PageView ballistics overshoot the page on some devices (#12884) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * PageView ballistics overshoot the page on some devices On some devices, such as Cupertino “Plus”-sized devices, scrolling left on the first page of a PageView will overshoot the first page and land on the second page. The issue is that applyContentDimensions incorrectly detects a content size change due to a floating point comparison on certain screen sizes (18257.400000000005 vs 18257.4) To fix this, perform a nearEqual comparison in applyContentDimensions. * Apply style changes to nearEqual for code review feedback. --- packages/flutter/lib/src/physics/utils.dart | 7 ++- .../lib/src/widgets/scroll_position.dart | 5 +- .../flutter/test/physics/near_equal_test.dart | 6 +++ .../flutter/test/widgets/page_view_test.dart | 54 +++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/physics/utils.dart b/packages/flutter/lib/src/physics/utils.dart index 26d8148603..998f85dfff 100644 --- a/packages/flutter/lib/src/physics/utils.dart +++ b/packages/flutter/lib/src/physics/utils.dart @@ -4,9 +4,14 @@ /// Whether two doubles are within a given distance of each other. /// -/// The epsilon argument must be positive. +/// The `epsilon` argument must be positive and not null. +/// The `a` and `b` arguments may be null. A null value is only considered +/// near-equal to another null value. bool nearEqual(double a, double b, double epsilon) { + assert(epsilon != null); assert(epsilon >= 0.0); + if (a == null || b == null) + return a == b; return (a > (b - epsilon)) && (a < (b + epsilon)); } diff --git a/packages/flutter/lib/src/widgets/scroll_position.dart b/packages/flutter/lib/src/widgets/scroll_position.dart index 4c80074bdc..c3aec5ca22 100644 --- a/packages/flutter/lib/src/widgets/scroll_position.dart +++ b/packages/flutter/lib/src/widgets/scroll_position.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; +import 'package:flutter/physics.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/scheduler.dart'; @@ -412,8 +413,8 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics { @override bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) { - if (_minScrollExtent != minScrollExtent || - _maxScrollExtent != maxScrollExtent || + if (!nearEqual(_minScrollExtent, minScrollExtent, Tolerance.defaultTolerance.distance) || + !nearEqual(_maxScrollExtent, maxScrollExtent, Tolerance.defaultTolerance.distance) || _didChangeViewportDimension) { _minScrollExtent = minScrollExtent; _maxScrollExtent = maxScrollExtent; diff --git a/packages/flutter/test/physics/near_equal_test.dart b/packages/flutter/test/physics/near_equal_test.dart index 4dce1c48a0..0a10efdc0e 100644 --- a/packages/flutter/test/physics/near_equal_test.dart +++ b/packages/flutter/test/physics/near_equal_test.dart @@ -13,4 +13,10 @@ void main() { expect(nearEqual(5.0, 6.0, 0.5), isFalse); expect(nearEqual(6.0, 5.0, 0.5), isFalse); }); + + test('test_null', () { + expect(nearEqual(5.0, null, 2.0), isFalse); + expect(nearEqual(null, 5.0, 2.0), isFalse); + expect(nearEqual(null, null, 2.0), isTrue); + }); } diff --git a/packages/flutter/test/widgets/page_view_test.dart b/packages/flutter/test/widgets/page_view_test.dart index 48f3dbc239..19399de2b0 100644 --- a/packages/flutter/test/widgets/page_view_test.dart +++ b/packages/flutter/test/widgets/page_view_test.dart @@ -325,6 +325,60 @@ void main() { expect(find.text('Alaska'), findsOneWidget); }); + testWidgets('Bouncing scroll physics ballistics does not overshoot', (WidgetTester tester) async { + final List log = []; + final PageController controller = new PageController(viewportFraction: 0.9); + + Widget build(PageController controller, {Size size}) { + final Widget pageView = new Directionality( + textDirection: TextDirection.ltr, + child: new PageView( + controller: controller, + onPageChanged: log.add, + physics: const BouncingScrollPhysics(), + children: kStates.map((String state) => new Text(state)).toList(), + ), + ); + + if (size != null) { + return new OverflowBox( + child: pageView, + minWidth: size.width, + minHeight: size.height, + maxWidth: size.width, + maxHeight: size.height, + ); + } else { + return pageView; + } + } + + await tester.pumpWidget(build(controller)); + expect(log, isEmpty); + + // Fling right to move to a non-existent page at the beginning of the + // PageView, and confirm that the PageView settles back on the first page. + await tester.fling(find.byType(PageView), const Offset(100.0, 0.0), 800.0); + await tester.pumpAndSettle(); + expect(log, isEmpty); + + expect(find.text('Alabama'), findsOneWidget); + expect(find.text('Alaska'), findsOneWidget); + expect(find.text('Arizona'), findsNothing); + + // Try again with a Cupertino "Plus" device size. + await tester.pumpWidget(build(controller, size: const Size(414.0, 736.0))); + expect(log, isEmpty); + + await tester.fling(find.byType(PageView), const Offset(100.0, 0.0), 800.0); + await tester.pumpAndSettle(); + expect(log, isEmpty); + + expect(find.text('Alabama'), findsOneWidget); + expect(find.text('Alaska'), findsOneWidget); + expect(find.text('Arizona'), findsNothing); + }); + testWidgets('PageView viewportFraction', (WidgetTester tester) async { PageController controller = new PageController(viewportFraction: 7/8);