diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index f8001d0c9f..0ccae36798 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -71,6 +71,14 @@ class DraggableScrollableController extends ChangeNotifier { return _attachedController!.extent.sizeToPixels(size); } + /// Returns Whether any [DraggableScrollableController] objects have attached themselves to the + /// [DraggableScrollableSheet]. + /// + /// If this is false, then members that interact with the [ScrollPosition], + /// such as [sizeToPixels], [size], [animateTo], and [jumpTo], must not be + /// called. + bool get isAttached => _attachedController != null && _attachedController!.hasClients; + /// Convert a sheet's pixel height to size (fractional value of parent container height). double pixelsToSize(double pixels) { _assertAttached(); @@ -157,7 +165,7 @@ class DraggableScrollableController extends ChangeNotifier { void _assertAttached() { assert( - _attachedController != null, + isAttached, 'DraggableScrollableController is not attached to a sheet. A DraggableScrollableController ' 'must be used in a DraggableScrollableSheet before any of its methods are called.', ); diff --git a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart index 489d7cbaed..bfd65190b9 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -1195,4 +1195,18 @@ void main() { // Can't use animateTo with a zero duration. expect(() => controller.animateTo(.5, duration: Duration.zero, curve: Curves.linear), throwsAssertionError); }); + + testWidgets('DraggableScrollableController must be attached before using any of its paramters', (WidgetTester tester) async { + final DraggableScrollableController controller = DraggableScrollableController(); + expect(controller.isAttached, false); + expect(()=>controller.size, throwsAssertionError); + final Widget boilerplate = _boilerplate( + null, + minChildSize: 0.4, + controller: controller, + ); + await tester.pumpWidget(boilerplate); + expect(controller.isAttached, true); + expect(controller.size, isNotNull); + }); }