From 16dce765775da51416f119a21fc88a8de9ee5fd3 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Fri, 6 Nov 2020 12:09:02 -0800 Subject: [PATCH] Fix excessive rebuilds of DSS (#69724) --- .../widgets/draggable_scrollable_sheet.dart | 39 +++-- .../draggable_scrollable_sheet_test.dart | 145 ++++++++++++++++++ 2 files changed, 175 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index dac3c02fd2..9303c98c78 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -288,17 +288,17 @@ class _DraggableSheetExtent { class _DraggableScrollableSheetState extends State { late _DraggableScrollableSheetScrollController _scrollController; late _DraggableSheetExtent _extent; + // The child only gets rebuilt when dependencies or the widget change. + // Otherwise, excessive rebuilds of the child are triggered every time the + // scroll extent changes, which is very expensive and does not provide any + // helpful information to the child. If the child needs to rebuild whenever + // the scroll position changes, they can always subscribe to it. + Widget? _child; @override void initState() { super.initState(); - _extent = _DraggableSheetExtent( - minExtent: widget.minChildSize, - maxExtent: widget.maxChildSize, - initialExtent: widget.initialChildSize, - listener: _setExtent, - ); - _scrollController = _DraggableScrollableSheetScrollController(extent: _extent); + _updateExtent(); } @override @@ -317,13 +317,33 @@ class _DraggableScrollableSheetState extends State { } _extent._currentExtent.value = _extent.initialExtent; } + _child = widget.builder(context, _scrollController); + } + + @override + void didUpdateWidget(DraggableScrollableSheet oldWidget) { + super.didUpdateWidget(oldWidget); + _updateExtent(); + // Call this unconditionally - the closure may not change even though it + // refers to things outside of its identity, e.g. a tearoff from state that + // has an `if (stateVariable)`. + _child = widget.builder(context, _scrollController); + } + + void _updateExtent() { + _extent = _DraggableSheetExtent( + minExtent: widget.minChildSize, + maxExtent: widget.maxChildSize, + initialExtent: widget.initialChildSize, + listener: _setExtent, + ); + _scrollController = _DraggableScrollableSheetScrollController(extent: _extent); } void _setExtent() { setState(() { // _extent has been updated when this is called. }); - } @override @@ -333,7 +353,7 @@ class _DraggableScrollableSheetState extends State { _extent.availablePixels = widget.maxChildSize * constraints.biggest.height; final Widget sheet = FractionallySizedBox( heightFactor: _extent.currentExtent, - child: widget.builder(context, _scrollController), + child: _child, alignment: Alignment.bottomCenter, ); return widget.expand ? SizedBox.expand(child: sheet) : sheet; @@ -344,6 +364,7 @@ class _DraggableScrollableSheetState extends State { @override void dispose() { _scrollController.dispose(); + _child = null; super.dispose(); } } diff --git a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart index f39d4e2111..9e1fcc46f4 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -309,4 +309,149 @@ void main() { expect(notificationTypes, types); }); } + + testWidgets('Builder is not called excessively', (WidgetTester tester) async { + int buildCount = 0; + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + DraggableScrollableSheet( + builder: (BuildContext context, ScrollController scrollController) { + buildCount += 1; + return Container( + color: const Color(0xFFABCDEF), + child: ListView.builder( + controller: scrollController, + itemExtent: 100, + itemCount: 100, + itemBuilder: (BuildContext context, int index) => Text('Item $index'), + ), + ); + }, + ), + ], + ), + )); + expect(buildCount, 1); + await tester.flingFrom(const Offset(0, 325), const Offset(0, -325), 200); + expect(buildCount, 1); + await tester.pumpAndSettle(); + expect(buildCount, 1); + }); + + testWidgets('Builder is called if widget updates', (WidgetTester tester) async { + int buildCount = 0; + final GlobalKey key = GlobalKey(); + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + DraggableScrollableSheet( + key: key, + builder: (BuildContext context, ScrollController scrollController) { + buildCount += 1; + return Container( + color: const Color(0xFFABCDEF), + child: ListView.builder( + controller: scrollController, + itemExtent: 100, + itemCount: 100, + itemBuilder: (BuildContext context, int index) => Text('Item $index'), + ), + ); + }, + ), + ], + ), + )); + expect(buildCount, 1); + expect(find.text('Item 1'), findsOneWidget); + + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + DraggableScrollableSheet( + key: key, + builder: (BuildContext context, ScrollController scrollController) { + buildCount += 1; + return Container( + color: const Color(0xFFFEDCBA), + child: ListView.builder( + controller: scrollController, + itemExtent: 50, + itemCount: 100, + itemBuilder: (BuildContext context, int index) => Text('New Item $index'), + ), + ); + }, + ), + ], + ), + )); + expect(buildCount, 2); + expect(find.text('Item 1'), findsNothing); + expect(find.text('New Item 1'), findsOneWidget); + }); + + testWidgets('Changes to min/max/initial child size are respected', (WidgetTester tester) async { + final GlobalKey key = GlobalKey(); + final Key childKey = UniqueKey(); + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + DraggableScrollableSheet( + key: key, + minChildSize: .25, + maxChildSize: 1.0, + initialChildSize: .5, + builder: (BuildContext context, ScrollController scrollController) { + return Container( + key: childKey, + color: const Color(0xFFABCDEF), + child: ListView.builder( + controller: scrollController, + itemExtent: 100, + itemCount: 100, + itemBuilder: (BuildContext context, int index) => Text('Item $index'), + ), + ); + }, + ), + ], + ), + )); + expect(find.text('Item 1'), findsOneWidget); + expect(tester.getRect(find.byKey(childKey)), const Rect.fromLTRB(0, 300, 800, 600)); + + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + DraggableScrollableSheet( + key: key, + minChildSize: .5, + maxChildSize: .75, + initialChildSize: .6, + builder: (BuildContext context, ScrollController scrollController) { + return Container( + key: childKey, + color: const Color(0xFFFEDCBA), + child: ListView.builder( + controller: scrollController, + itemExtent: 50, + itemCount: 100, + itemBuilder: (BuildContext context, int index) => Text('New Item $index'), + ), + ); + }, + ), + ], + ), + )); + expect(find.text('New Item 1'), findsOneWidget); + expect(tester.getRect(find.byKey(childKey)), const Rect.fromLTRB(0, 240, 800, 600)); + }); }