From e0f3001fde05e57f0ee73ab106f9b3510aea38db Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Fri, 21 Jul 2017 18:42:12 -0400 Subject: [PATCH] Fix physics with NestedScrollView (#11326) * Fix physics with NestedScrollView * Review feedback --- .../lib/src/widgets/nested_scroll_view.dart | 4 +- .../test/widgets/nested_scroll_view_test.dart | 40 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/nested_scroll_view.dart b/packages/flutter/lib/src/widgets/nested_scroll_view.dart index bd6a146dea..2661db7cce 100644 --- a/packages/flutter/lib/src/widgets/nested_scroll_view.dart +++ b/packages/flutter/lib/src/widgets/nested_scroll_view.dart @@ -135,7 +135,9 @@ class _NestedScrollViewState extends State { return new CustomScrollView( scrollDirection: widget.scrollDirection, reverse: widget.reverse, - physics: new ClampingScrollPhysics(parent: widget.physics), + physics: widget.physics != null + ? widget.physics.applyTo(const ClampingScrollPhysics()) + : const ClampingScrollPhysics(), controller: _coordinator._outerController, slivers: widget._buildSlivers(context, _coordinator._innerController, _coordinator.hasScrolledBody), ); diff --git a/packages/flutter/test/widgets/nested_scroll_view_test.dart b/packages/flutter/test/widgets/nested_scroll_view_test.dart index 3483a9c32b..a46965489b 100644 --- a/packages/flutter/test/widgets/nested_scroll_view_test.dart +++ b/packages/flutter/test/widgets/nested_scroll_view_test.dart @@ -6,7 +6,21 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -Widget buildTest({ ScrollController controller, String title: 'TTTTTTTT' }) { +class _CustomPhysics extends ClampingScrollPhysics { + const _CustomPhysics({ ScrollPhysics parent }) : super(parent: parent); + + @override + _CustomPhysics applyTo(ScrollPhysics ancestor) { + return new _CustomPhysics(parent: buildParent(ancestor)); + } + + @override + Simulation createBallisticSimulation(ScrollMetrics position, double dragVelocity) { + return new ScrollSpringSimulation(spring, 1000.0, 1000.0, 1000.0); + } +} + +Widget buildTest({ ScrollController controller, String title:'TTTTTTTT' }) { return new MediaQuery( data: const MediaQueryData(), child: new Scaffold( @@ -288,4 +302,28 @@ void main() { expect(tester.renderObject(find.byType(AppBar)).size.height, 200.0); }); + testWidgets('NestedScrollViews with custom physics', (WidgetTester tester) async { + await tester.pumpWidget(new MediaQuery( + data: const MediaQueryData(), + child: new NestedScrollView( + physics: const _CustomPhysics(), + headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { + return [ + const SliverAppBar( + floating: true, + title: const Text('AA'), + ), + ]; + }, + body: new Container(), + ))); + expect(find.text('AA'), findsOneWidget); + await tester.pump(const Duration(milliseconds: 500)); + final Offset point1 = tester.getCenter(find.text('AA')); + await tester.dragFrom(point1, const Offset(0.0, 200.0)); + await tester.pump(const Duration(milliseconds: 20)); + final Offset point2 = tester.getCenter(find.text('AA')); + expect(point1.dy, greaterThan(point2.dy)); + }); + }