Skip to content

Commit 054d9d1

Browse files
[cupertino_ui] Fix CupertinoSheetRoute throwing when a drag ends during a pending relayout (flutter#13316)
Dragging a `CupertinoSheetRoute` converts drag deltas and the release velocity into fractions of the sheet's height. Both drag paths read that height from `context.size`: `_CupertinoDragGestureDetectorState.sheetHeight` for regular sheets, and `_dragUpdate` / `_handleDragEnd` in `_CupertinoDraggableScrollableSheetState` for sheets built with `scrollableBuilder`. `Element.size` asserts when the render object is dirty for layout, so a drag that ends while a relayout is pending throws `Cannot get size from a render object that has been marked dirty for layout.` The reporter saw it cascade into `_drag == null` asserts in `ScrollableState`, after which the sheet stopped responding. In release the assert is stripped and the read returns the last laid out size. The fix reads the height from the `RenderBox` directly. That's the size the sheet was last laid out at, which is the size the drag was made against, and it's what release builds were already using. The Material `BottomSheet` reads its height for drag updates and fling velocity the same way (`_childHeight`). ## Tests Two regression tests, one per drag path. Each starts a partial drag, marks the box the sheet measures as dirty, and releases before the next frame. Both fail on `main` with the assertion above, and both pass with the fix, with the sheet settling back to where it started. The rest of `sheet_test.dart` passes as well (44 tests in total). One limitation: the reporter hit this on a device when the keyboard was dismissed mid-drag. Changing the view insets mid-drag in a widget test doesn't reproduce it, because the relayout that follows lands before the pointer up. So the tests set up the condition from the reporter's stack trace directly, not the keyboard sequence. Fixes flutter/flutter#194681 ## Pre-Review Checklist I used AI tooling while working on this, and reviewed and tested all of it myself. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 7729836 commit 054d9d1

3 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎packages/cupertino_ui/lib/src/sheet.dart‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,13 @@ mixin _CupertinoSheetRouteTransitionMixin<T> on PageRoute<T> {
10141014
}
10151015
}
10161016

1017+
// Reads the height from the render box instead of context.size, which throws
1018+
// in debug mode when the box is dirty for layout. A drag can end while a
1019+
// relayout is pending, and the last laid out height is the one the drag was
1020+
// made against.
1021+
double _sheetHeightOf(BuildContext context) =>
1022+
(context.findRenderObject()! as RenderBox).size.height;
1023+
10171024
class _CupertinoDragGestureDetector<T> extends StatefulWidget {
10181025
const _CupertinoDragGestureDetector({
10191026
required this.enabledCallback,
@@ -1040,8 +1047,6 @@ class _CupertinoDragGestureDetectorState<T> extends State<_CupertinoDragGestureD
10401047
static VelocityTracker _cupertinoVelocityBuilder(PointerEvent event) =>
10411048
IOSScrollViewFlingVelocityTracker(event.kind);
10421049

1043-
double get sheetHeight => context.size!.height;
1044-
10451050
@override
10461051
void initState() {
10471052
super.initState();
@@ -1089,6 +1094,7 @@ class _CupertinoDragGestureDetectorState<T> extends State<_CupertinoDragGestureD
10891094
if (_stretchDragController == null) {
10901095
return;
10911096
}
1097+
final double sheetHeight = _sheetHeightOf(context);
10921098
final double delta = sheetHeight > 0 ? details.primaryDelta! / sheetHeight : 0.0;
10931099
_dragGestureController!.dragUpdate(
10941100
// Divide by size of the sheet.
@@ -1104,6 +1110,7 @@ class _CupertinoDragGestureDetectorState<T> extends State<_CupertinoDragGestureD
11041110
_dragGestureController = null;
11051111
return;
11061112
}
1113+
final double sheetHeight = _sheetHeightOf(context);
11071114
final double velocity = sheetHeight > 0
11081115
? details.velocity.pixelsPerSecond.dy / sheetHeight
11091116
: 0.0;
@@ -1450,8 +1457,9 @@ class _CupertinoDraggableScrollableSheetState<T>
14501457
void _dragUpdate(double delta) {
14511458
assert(mounted);
14521459
if (_dragGestureController != null) {
1460+
final double sheetHeight = _sheetHeightOf(context);
14531461
_dragGestureController!.dragUpdate(
1454-
delta / (context.size!.height - (context.size!.height * _kTopGapRatio)),
1462+
delta / (sheetHeight - (sheetHeight * _kTopGapRatio)),
14551463
null,
14561464
);
14571465
}
@@ -1460,7 +1468,7 @@ class _CupertinoDraggableScrollableSheetState<T>
14601468
void _handleDragEnd(double velocity) {
14611469
assert(mounted);
14621470
if (_dragGestureController != null) {
1463-
_dragGestureController!.dragEnd(-velocity / context.size!.height, null);
1471+
_dragGestureController!.dragEnd(-velocity / _sheetHeightOf(context), null);
14641472
_dragGestureController = null;
14651473
}
14661474
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `CupertinoSheetRoute` throwing when a drag ends while a relayout of the sheet is pending.
3+
version: patch

‎packages/cupertino_ui/test/sheet_test.dart‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,45 @@ void main() {
16251625
final double finalPosition = box.localToGlobal(Offset.zero).dy;
16261626
expect(finalPosition, initialPosition);
16271627
});
1628+
1629+
testWidgets('drag can end while a relayout of the sheet is pending', (
1630+
WidgetTester tester,
1631+
) async {
1632+
// Regression test for https://github.com/flutter/flutter/issues/194681.
1633+
final GlobalKey homeKey = GlobalKey();
1634+
final GlobalKey sheetKey = GlobalKey();
1635+
1636+
await tester.pumpWidget(dragGestureApp(homeKey, sheetKey));
1637+
1638+
await tester.tap(find.text('Push Page 2'));
1639+
await tester.pumpAndSettle();
1640+
1641+
var box = tester.renderObject(find.byKey(sheetKey)) as RenderBox;
1642+
final double initialPosition = box.localToGlobal(Offset.zero).dy;
1643+
1644+
final TestGesture gesture = await tester.startGesture(const Offset(100, 200));
1645+
await gesture.moveBy(const Offset(0, 100));
1646+
await tester.pump();
1647+
1648+
// The sheet measures the drag against the height of the box the drag
1649+
// gesture detector renders. Mark it dirty so the drag ends before the
1650+
// next layout.
1651+
final Finder dragGestureDetector = find.ancestor(
1652+
of: find.byKey(sheetKey),
1653+
matching: find.byWidgetPredicate(
1654+
(Widget widget) => '${widget.runtimeType}'.startsWith('_CupertinoDragGestureDetector'),
1655+
),
1656+
);
1657+
tester.renderObject(dragGestureDetector).markNeedsLayout();
1658+
1659+
await gesture.up();
1660+
await tester.pumpAndSettle();
1661+
1662+
expect(tester.takeException(), isNull);
1663+
expect(find.text('Page 2'), findsOneWidget);
1664+
box = tester.renderObject(find.byKey(sheetKey)) as RenderBox;
1665+
expect(box.localToGlobal(Offset.zero).dy, initialPosition);
1666+
});
16281667
});
16291668

16301669
group('draggable scrollable CupertinoSheetRoute', () {
@@ -1865,6 +1904,44 @@ void main() {
18651904
// Scrollable sheet should not be open.
18661905
expect(find.text('Scroll Item 3'), findsNothing);
18671906
});
1907+
1908+
testWidgets('drag can end while a relayout of the sheet content is pending', (
1909+
WidgetTester tester,
1910+
) async {
1911+
// Regression test for https://github.com/flutter/flutter/issues/194681.
1912+
final GlobalKey homeKey = GlobalKey();
1913+
final GlobalKey sheetKey = GlobalKey();
1914+
1915+
await tester.pumpWidget(draggableScrollableApp(homeKey, sheetKey));
1916+
1917+
await tester.tap(find.text('Push Page 2'));
1918+
await tester.pumpAndSettle();
1919+
1920+
var box = tester.renderObject(find.byKey(sheetKey)) as RenderBox;
1921+
final double initialPosition = box.localToGlobal(Offset.zero).dy;
1922+
1923+
// Drag the sheet down while its content is scrolled to the top.
1924+
final TestGesture gesture = await tester.startGesture(const Offset(100, 300));
1925+
// Do a small drag first to win the gesture arena.
1926+
await gesture.moveBy(const Offset(0, 30));
1927+
await gesture.moveBy(const Offset(0, 50));
1928+
await tester.pump();
1929+
1930+
box = tester.renderObject(find.byKey(sheetKey)) as RenderBox;
1931+
expect(box.localToGlobal(Offset.zero).dy, greaterThan(initialPosition));
1932+
1933+
// The sheet measures the drag against the height of its content. Mark
1934+
// the content dirty so the drag ends before the next layout.
1935+
box.markNeedsLayout();
1936+
1937+
await gesture.up();
1938+
await tester.pumpAndSettle();
1939+
1940+
expect(tester.takeException(), isNull);
1941+
expect(find.text('Scroll Item 3'), findsOneWidget);
1942+
box = tester.renderObject(find.byKey(sheetKey)) as RenderBox;
1943+
expect(box.localToGlobal(Offset.zero).dy, initialPosition);
1944+
});
18681945
});
18691946

18701947
testWidgets('CupertinoSheet causes SystemUiOverlayStyle changes', (WidgetTester tester) async {

0 commit comments

Comments
 (0)