diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 89a2df9433..9f0cf6659a 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -215,14 +215,22 @@ class _BottomSheetState extends State { } void _handleDragUpdate(DragUpdateDetails details) { - assert(widget.enableDrag); + assert( + widget.enableDrag && widget.animationController != null, + "'BottomSheet.animationController' can not be null when 'BottomSheet.enableDrag' is true. " + "Use 'BottomSheet.createAnimationController' to create one, or provide another AnimationController.", + ); if (_dismissUnderway) return; widget.animationController!.value -= details.primaryDelta! / _childHeight; } void _handleDragEnd(DragEndDetails details) { - assert(widget.enableDrag); + assert( + widget.enableDrag && widget.animationController != null, + "'BottomSheet.animationController' can not be null when 'BottomSheet.enableDrag' is true. " + "Use 'BottomSheet.createAnimationController' to create one, or provide another AnimationController.", + ); if (_dismissUnderway) return; bool isClosing = false; diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index c5efacf608..e5f9398790 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -23,6 +23,26 @@ void main() { expect(dyDelta1, isNot(moreOrLessEquals(dyDelta2, epsilon: 0.1))); } + testWidgets('Throw if enable drag without an animation controller', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/89168 + await tester.pumpWidget( + MaterialApp( + home: BottomSheet( + onClosing: () {}, + builder: (_) => Container( + height: 200, + color: Colors.red, + child: const Text('BottomSheet'), + ), + ), + ), + ); + + await tester.drag(find.text('BottomSheet'), const Offset(0.0, 150.0)); + + expect(tester.takeException(), isNotNull); + }); + testWidgets('Tapping on a modal BottomSheet should not dismiss it', (WidgetTester tester) async { late BuildContext savedContext;