From a2aa2ed02d9573faf1d759a97a1621073d359db2 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Wed, 2 Dec 2020 11:42:38 -0800 Subject: [PATCH] Revert "Fix excessive rebuilds of DSS (#69724)" (#71459) This reverts commit 16dce765775da51416f119a21fc88a8de9ee5fd3. --- .../widgets/draggable_scrollable_sheet.dart | 39 ++--- .../draggable_scrollable_sheet_test.dart | 145 ------------------ 2 files changed, 9 insertions(+), 175 deletions(-) diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index 9303c98c78..dac3c02fd2 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(); - _updateExtent(); + _extent = _DraggableSheetExtent( + minExtent: widget.minChildSize, + maxExtent: widget.maxChildSize, + initialExtent: widget.initialChildSize, + listener: _setExtent, + ); + _scrollController = _DraggableScrollableSheetScrollController(extent: _extent); } @override @@ -317,33 +317,13 @@ 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 @@ -353,7 +333,7 @@ class _DraggableScrollableSheetState extends State { _extent.availablePixels = widget.maxChildSize * constraints.biggest.height; final Widget sheet = FractionallySizedBox( heightFactor: _extent.currentExtent, - child: _child, + child: widget.builder(context, _scrollController), alignment: Alignment.bottomCenter, ); return widget.expand ? SizedBox.expand(child: sheet) : sheet; @@ -364,7 +344,6 @@ 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 9e1fcc46f4..f39d4e2111 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -309,149 +309,4 @@ 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)); - }); }