adds isAttached getter to DraggableScrollableController (#100269)
This commit is contained in:
@@ -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.',
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user