Skip to content

fix: prefer topmost route when resolving tap/enter_text by text/key#53

Open
ReniDelonzek wants to merge 1 commit into
ai-dashboad:mainfrom
ReniDelonzek:fix/prefer-topmost-route-on-text-key-match
Open

fix: prefer topmost route when resolving tap/enter_text by text/key#53
ReniDelonzek wants to merge 1 commit into
ai-dashboad:mainfrom
ReniDelonzek:fix/prefer-topmost-route-on-text-key-match

Conversation

@ReniDelonzek

Copy link
Copy Markdown

Problem

When a screen is pushed over another that stays mounted (a very common Navigator/Overlay pattern), _findElementByText and _findElementByKey in flutter_skill.dart walk the entire element tree from rootElement and return the first match. In pre-order traversal the background route (mounted first) is visited before the foreground one, so tap(text:) / tap(key:) can hit an element that is invisible, behind the current screen.

Concrete case: a foreground screen has a tab labeled "X" while the background screen (still mounted underneath) also renders "X" as a list subtitle. tap(text: "X") lands on the background occurrence instead of the visible tab.

This is tracked in #52 (Problem 2).

Fix

Collect all matches and return the last one. In Overlay/Navigator order the foreground route is mounted last and therefore visited last, so the last match targets the screen the user actually sees. Minimal, dependency-free, and strictly better than first-match for stacked routes (single-match behavior is unchanged).

Applied to both the canonical lib/flutter_skill.dart and the bundled packaging/npm/dart/lib/flutter_skill.dart.

static Element? _preferTopmostMatch(List<Element> matches) {
  if (matches.isEmpty) return null;
  return matches.last;
}

Follow-ups (not in this PR, see #52)

Notes

  • Did not run dart format on the touched files: the formatter reflows ~900 lines (repo style differs from default dart format), which would bury the change. The edit follows the surrounding style. Happy to adjust if the project has a specific format step.

When a screen is pushed over another that stays mounted, _findElementByText
and _findElementByKey returned the first match in a pre-order tree walk — which
is the background route (mounted first). So tap(text:)/tap(key:) could hit an
element hidden behind the current screen (e.g. a tab label on the visible screen
that also exists as a list subtitle on the screen behind it).

Collect all matches and return the last one. In Overlay/Navigator order the
foreground route is mounted last and therefore visited last, so the last match
targets the screen the user actually sees. A center hit-test could refine this
further and is noted as a follow-up.

Applied to both the canonical lib/ source and the bundled npm copy.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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