Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions sheet/lib/src/sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,53 @@ class Sheet extends StatelessWidget {
},
);
}

/// Expanding [Sheet] using [SheetController] might not always work, e.g.
/// you are trying to animate the sheet and expand controller at the same
/// time (see: https://github.com/jamesblasco/modal_bottom_sheet/issues/412)
/// You can use [addPostLayoutCallback] in order to defer your call
/// to the moment [RenderSheetViewport] has finished [RenderSheetViewport.performLayout]
/// Additionally you can provide [timeout] after which this callback should
/// be called anyway incase [RenderSheetViewport.performLayout] doesn't start.
/// Usually (this has not been super benchmarked!) layout starts under 50ms,
/// putting 100ms to be on the safe side - YMMV
static void addPostLayoutCallback(VoidCallback callback,
{Duration timeout = const Duration(milliseconds: 100)}) {
__willClearLayoutCallbacks = false;
_postLayoutCallbacks.add(callback);

Future.delayed(timeout).then((_) {
if (_postLayoutCallbacks.isEmpty) {
return;
}

if (__willClearLayoutCallbacks) {
return;
}

_clearLayoutCallbacks();
});
}

static var __willClearLayoutCallbacks = false;

static void _willClearLayoutCallbacks() {
__willClearLayoutCallbacks = true;
}

static void _clearLayoutCallbacks() {
// post layout callbacks
final List<VoidCallback> localPostLayoutCallbacks =
List<VoidCallback>.of(Sheet._postLayoutCallbacks);

_postLayoutCallbacks.clear();
__willClearLayoutCallbacks = false;
for (final callback in localPostLayoutCallbacks) {
callback();
}
}

static final List<VoidCallback> _postLayoutCallbacks = <VoidCallback>[];
}

class _DefaultSheetScrollController extends StatelessWidget {
Expand Down Expand Up @@ -662,6 +709,12 @@ class RenderSheetViewport extends RenderBox
markNeedsSemanticsUpdate();
}

@override
void markNeedsLayout() {
super.markNeedsLayout();
Sheet._willClearLayoutCallbacks();
}

@override
void setupParentData(RenderObject child) {
// We don't actually use the offset argument in BoxParentData, so let's
Expand Down Expand Up @@ -805,6 +858,8 @@ class RenderSheetViewport extends RenderBox

offset.applyViewportDimension(_viewportExtent);
offset.applyContentDimensions(_minScrollExtent, _maxScrollExtent);

Sheet._clearLayoutCallbacks();
}

Offset get _paintOffset => _paintOffsetForPosition(offset.pixels);
Expand Down