avoid AnimatedBuilder rebuilding widget (#55832)
This commit is contained in:
committed by
GitHub
parent
afdc440840
commit
e7b6b52102
@@ -369,6 +369,22 @@ class _ModalBottomSheetState<T> extends State<_ModalBottomSheet<T>> {
|
||||
|
||||
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<T> extends State<_ModalBottomSheet<T>> {
|
||||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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<void>(
|
||||
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<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
|
||||
bool showBottomSheetThenCalled = false;
|
||||
|
||||
Reference in New Issue
Block a user