Skip to content

fix: close only the dialog's own route when open turns false - #37

Merged
AdamMusa merged 2 commits into
codex/engine-initializationfrom
fix/dialog-early-close-race
Oct 29, 2026
Merged

AdamMusa merged 2 commits into
codex/engine-initializationfrom
fix/dialog-early-close-race

Conversation

@AdamMusa

Copy link
Copy Markdown
Owner

Engine-only fix with no Ruby change. Follow-up found during Package D (D1 #17, D3 #29).

Bug

The Material BottomSheet and both AlertDialog renderers (Material and Cupertino) closed themselves with a blind Navigator.of(context).pop() called synchronously inside build.

  • The alert dialogs guarded it with canPop(); the sheet had no guard at all.
  • The alert dialogs' post-frame push also returned early when open was already false, but left _open: true behind.

Consequences, all reproduced by the new test with the dialog on the second page of a nested navigator:

  • open: false before the route is pushed (same frame as open: true): the alert dialogs pop the user's page, and _open stays true, so the control can never reopen. No dismiss is fired, so Ruby keeps tracking the dialog.
  • open: false while another route covers the dialog (all three renderers): the route above is popped instead of the dialog.
  • Every case: popping during build throws setState() or markNeedsBuild() called during build.

This is a regression of the owner's fix 0a4511eb ("Fix bottom sheet route closing"). It exists only in AdamMusa/ruflet, in the old ruflet_client/flet_packages/flet, and was the final state of that file before the client was deleted (68ba6952). The engine was extracted from an earlier copy, so the fix was lost. It is not in ruflet-engine or ruflet-template history. That commit made the sheet stateful: it tracked the sheet's own ModalRoute, deferred the pop to a post-frame callback, handled a close requested while still opening, and fired dismiss once the route completed.

Fix: the same route-ownership approach, shared

New neutral helper lib/src/widgets/control_route.dart (widgets layer only, so it imports neither Material nor Cupertino):

  • presentControlRoute(context, control, createRoute) sets _open and pushes the route onto the owning navigator after the frame. It remembers the route per control in an Expando. If open was turned off again first, or the control left the tree, nothing is pushed and the lifecycle completes: _open/open are reset and dismiss fires, so Ruby untracks the dialog and it can be reopened.
  • closeControlRoute(control) runs after the frame and closes only that control's route: pop() when it is on top, removeRoute() when something covers it. If the route is not presented yet, the pending presentation handles the close. Repeated rebuilds while closing do nothing.
  • Every presentation fires exactly one dismiss, through the route's popped future, as before.

Per renderer:

  • alert_dialog.dart / cupertino_alert_dialog.dart: same routes (ControlMaterialDialogRoute / ControlCupertinoDialogRoute, so the 1f21fca live-barrier behaviour is kept). Present and close now go through the helper.
  • bottom_sheet.dart: pushes the same ModalBottomSheetRoute that showModalBottomSheet builds, with identical arguments (capturedThemes, scrim label/tap hint, theme barrier-color fallback). This lets the control own the route object. The parity is proven by a guard test that passes on both the old showModalBottomSheet code and the new code.

The iOS BottomSheet (cupertino_bottom_sheet.dart) has the same bug on this base. It is fixed by D1 (#17) with the same ownership pattern and covered by its tests (close while animating in, reopen, nested-navigator rebuilds), so it is left untouched here to avoid conflicts. Once both PRs merge, D1/D3 can be folded onto this helper in a small refactor.

Tests

test/widgets/dialog_early_close_test.dart covers the Cupertino AlertDialog (iOS), the Material AlertDialog and the Material BottomSheet (Android). Each case mounts the dialog on page 2 of a nested navigator:

  1. open: false lands before the route is pushed (a post-frame callback ahead of the control's own). Expected: no route popped (page 2 survives, canPop true), no exception, open == false, exactly one dismiss, and reopen works.
  2. open: false while the route is still animating in. Expected: only the dialog closes, one dismiss, reopen works.
  3. open: false while another route is pushed above the dialog, plus an extra rebuild. Expected: the route above survives, one dismiss, back to page 2 with no dialog, reopen works.

A guard test checks that the Material sheet keeps the showModalBottomSheet presentation: a ModalBottomSheetRoute in the owning navigator, Scrim label, dismissible, drag, safe area, bgcolor, handle and barrier color, and a barrier tap that fires one dismiss.

before (base c9b9d76) after
dialog_early_close_test 5 of 9 race cases fail (wrong page/route popped + setState-during-build) 10/10 pass
modal_presentation_test (1f21fca) pass pass

verify_pr.sh (Flutter 3.41.2; Ruby tree = clean origin/main b34072be because Ruby is unchanged):

PASS ruby:ruflet_core                   145/145 files, 618 runs
PASS engine:ruflet flutter test         +705: All tests passed!   (baseline 695 + 10 new)
PASS engine:ruflet analyze              No issues found!
INFO wire-key audit                     NEW vs baseline: 0, fixed: 0

Ruby usage (unchanged API, now reliable)

page.show_dialog(dialog)     # open: true
page.close_dialog(dialog)    # open: false: closes only this dialog, even mid-animation or under another route
page.show_bottom_sheet(sheet)
page.close_bottom_sheet      # same for sheets; on_dismiss fires once per opening

The Material BottomSheet and both AlertDialog renderers closed with a
blind Navigator.pop() inside build. An open: false that arrived before
the route was pushed popped the user's page and left _open stuck, so the
control could never reopen; when another route covered the dialog, that
route was popped instead; and popping during build threw.

This restores the route ownership of 0a4511eb (lost when the engine was
extracted) in a shared widgets-layer helper: the route is pushed after
the frame and remembered per control, a close before the push completes
the lifecycle instead, and closing pops or removes only that route. Every
presentation fires exactly one dismiss and can be reopened. The sheet
now pushes the same ModalBottomSheetRoute showModalBottomSheet builds so
it owns the route.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant