From e7b6b521022765027671a6791642ef34ede3613e Mon Sep 17 00:00:00 2001 From: Enol Casielles Martinez Date: Sat, 16 May 2020 23:52:08 +0200 Subject: [PATCH] avoid AnimatedBuilder rebuilding widget (#55832) --- .../lib/src/material/bottom_sheet.dart | 33 ++++++++++--------- .../test/material/bottom_sheet_test.dart | 32 ++++++++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 82bd26a564..d1b9aa3448 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -369,6 +369,22 @@ class _ModalBottomSheetState extends State<_ModalBottomSheet> { return AnimatedBuilder( animation: widget.route.animation, + child: BottomSheet( + animationController: widget.route._animationController, + onClosing: () { + if (widget.route.isCurrent) { + Navigator.pop(context); + } + }, + builder: widget.route.builder, + backgroundColor: widget.backgroundColor, + elevation: widget.elevation, + shape: widget.shape, + clipBehavior: widget.clipBehavior, + enableDrag: widget.enableDrag, + onDragStart: handleDragStart, + onDragEnd: handleDragEnd, + ), builder: (BuildContext context, Widget child) { // Disable the initial animation when accessible navigation is on so // that the semantics are added to the tree at the correct time. @@ -383,22 +399,7 @@ class _ModalBottomSheetState extends State<_ModalBottomSheet> { child: ClipRect( child: CustomSingleChildLayout( delegate: _ModalBottomSheetLayout(animationValue, widget.isScrollControlled), - child: BottomSheet( - animationController: widget.route._animationController, - onClosing: () { - if (widget.route.isCurrent) { - Navigator.pop(context); - } - }, - builder: widget.route.builder, - backgroundColor: widget.backgroundColor, - elevation: widget.elevation, - shape: widget.shape, - clipBehavior: widget.clipBehavior, - enableDrag: widget.enableDrag, - onDragStart: handleDragStart, - onDragEnd: handleDragEnd, - ), + child: child, ), ), ); diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index 8b33bf54e4..bddd329c1f 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -306,6 +306,38 @@ void main() { expect(find.text('BottomSheet'), findsNothing); }); + testWidgets('Modal BottomSheet builder should only be called once', (WidgetTester tester) async { + BuildContext savedContext; + + await tester.pumpWidget(MaterialApp( + home: Builder( + builder: (BuildContext context) { + savedContext = context; + return Container(); + }, + ), + )); + + int numBuilderCalls = 0; + showModalBottomSheet( + context: savedContext, + isDismissible: false, + enableDrag: true, + builder: (BuildContext context) { + numBuilderCalls++; + return const Text('BottomSheet'); + }, + ); + + await tester.pumpAndSettle(); + expect(numBuilderCalls, 1); + + // Swipe the bottom sheet to dismiss it. + await tester.drag(find.text('BottomSheet'), const Offset(0.0, 150.0)); + await tester.pumpAndSettle(); // Bottom sheet dismiss animation. + expect(numBuilderCalls, 1); + }); + testWidgets('Verify that a downwards fling dismisses a persistent BottomSheet', (WidgetTester tester) async { final GlobalKey scaffoldKey = GlobalKey(); bool showBottomSheetThenCalled = false;