Skip to content

Commit e9bb791

Browse files
committed
Add missing removeStatusListener for bottom sheets (flutter#167899)
1 parent f454856 commit e9bb791

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎packages/flutter/lib/src/material/scaffold.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,6 +3273,7 @@ class _StandardBottomSheetState extends State<_StandardBottomSheet> {
32733273

32743274
@override
32753275
void dispose() {
3276+
widget.animationController.removeStatusListener(_handleStatusChange);
32763277
widget.onDispose?.call();
32773278
super.dispose();
32783279
}

‎packages/flutter/test/material/bottom_sheet_test.dart‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:flutter/foundation.dart';
88
import 'package:flutter/material.dart';
99
import 'package:flutter/semantics.dart';
1010
import 'package:flutter_test/flutter_test.dart';
11+
1112
import '../widgets/semantics_tester.dart';
1213

1314
void main() {
@@ -1799,6 +1800,40 @@ void main() {
17991800
},
18001801
);
18011802

1803+
testWidgets(
1804+
'The framework removes all animation listeners from foreign controllers when disposing.',
1805+
(WidgetTester tester) async {
1806+
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
1807+
final _StatusTestAnimationController controller = _StatusTestAnimationController(
1808+
vsync: const TestVSync(),
1809+
duration: const Duration(seconds: 2),
1810+
reverseDuration: const Duration(seconds: 2),
1811+
);
1812+
addTearDown(controller.dispose);
1813+
1814+
await tester.pumpWidget(
1815+
MaterialApp(home: Scaffold(key: scaffoldKey, body: const Center(child: Text('body')))),
1816+
);
1817+
1818+
await tester.pump();
1819+
expect(controller.isListening, isFalse);
1820+
1821+
scaffoldKey.currentState!.showBottomSheet((BuildContext context) {
1822+
return const SizedBox(height: 200.0, child: Text('BottomSheet'));
1823+
}, transitionAnimationController: controller);
1824+
1825+
await tester.pumpAndSettle();
1826+
expect(controller.isListening, isTrue);
1827+
expect(find.text('BottomSheet'), findsOneWidget);
1828+
1829+
// Swipe the bottom sheet to dismiss it.
1830+
await tester.drag(find.text('BottomSheet'), const Offset(0.0, 150.0));
1831+
await tester.pumpAndSettle(); // Bottom sheet dismiss animation.
1832+
expect(controller.isListening, isFalse);
1833+
expect(find.text('BottomSheet'), findsNothing);
1834+
},
1835+
);
1836+
18021837
testWidgets(
18031838
'Calling PersistentBottomSheetController.close does not crash when it is not the current bottom sheet',
18041839
(WidgetTester tester) async {
@@ -2808,3 +2843,13 @@ class _TestPage extends StatelessWidget {
28082843
);
28092844
}
28102845
}
2846+
2847+
class _StatusTestAnimationController extends AnimationController with AnimationLazyListenerMixin {
2848+
_StatusTestAnimationController({super.duration, super.reverseDuration, required super.vsync});
2849+
2850+
@override
2851+
void didStartListening() {}
2852+
2853+
@override
2854+
void didStopListening() {}
2855+
}

0 commit comments

Comments
 (0)