r/FlutterDev • • 20m ago

Article Three bugs a Flutter command palette only shows you after the demo

I spent the last while building a Ctrl/Cmd+K palette with no third-party packages (pure-Dart matcher, controller, widgets), then ran it at 320 dp with 3.0x text and a provider that answers 800 ms late. The list itself was never the problem. These three were.

1. Case folding can change the string length, so highlight offsets lie.

I score a lowercase copy and render spans on the original label. Fine in ASCII. Then 'İ'.toLowerCase() is two code units (i + combining dot) and 'ß'.toLowerCase() is 'ss' — every span in that row shifts by one. Not a crash: a bold letter in the wrong place, which reads as a ranking bug and sends you hunting in the ranker. The guard is to return spans only when the folded string has the same length as the original, and to clamp each span to the rendered string so a translated label can't blow up substring(). The item still matches; it just doesn't claim to know where.

A related one that looked wrong but was right: my subsequence matcher picked the earliest matching character, so "cs" highlighted the c and s inside a word when a word-start pair existed two characters later. A 3-character boundary lookahead fixes the highlight — "cs" now hits the C and S of "Create snapshot".

2. "The older request will answer first" is false.

140 ms debounce, then one provider call. Type "inv", pause, type "oice": the first answer lands after the second and overwrites it, and the list shows results for a query that's no longer in the field. Debounce only makes the race rarer.

Generation token, bumped per keystroke and re-checked after every await:

final int generation = ++_generation;
final String snapshot = _query;
for (final provider in _providers) {
  final answer = await Future.value(provider(snapshot));
  if (_disposed || generation != _generation) return;
  merged.addAll(answer);
}

The part that cost me a session: abandoning has to abandon everything, including the isSearching flag. My first version cleared it on the way out, so a stale run ending switched off the progress bar under a search that was still running.

The test does what a network does — completes the older future last:

second.complete([fresh]);  // where the user is
first.complete([stale]);   // where they were
expect(results, contains('fresh')); expect(results, isNot(contains('stale')));

3. The keyboard has two owners and Ctrl/Cmd+K is destructive.

Arrow keys belong to the TextField, so the palette's bindings sit above the field (CallbackShortcuts wrapping it) and left/right are deliberately unbound so the caret still moves. But Ctrl+K on a text field is a platform editing shortcut — kill to end of line — so binding it naively opens the palette and deletes the rest of what you were typing. I use HardwareKeyboard.instance.addHandler, return true on key-down for Ctrl/Cmd+K, and drop KeyRepeatEvent so holding K doesn't open it repeatedly.

Two smaller rules that came out of the same area: disabled rows sort below every enabled row whatever their score (a greyed row that ranks first is a trap for the person pressing Enter), and the highlighted row is scrolled into view in a post-frame Scrollable.ensureVisible — with a last-highlighted guard, because holding an arrow key otherwise queues one scroll callback per row.

Bonus: at 320 dp and 3.0x the label was fine (one line, ellipsis, inside Expanded). What overflowed was the hint chip on each row (needs maxWidth) and the footer (↑↓ Navigate · Enter Run · Esc Close + "38 results" doesn't fit; below 400 dp I drop the words and keep the key caps, scroll the group, and give the count a Flexible).

I put the screen states, README and a demo GIF in a public repo if the layout helps anyone; no source in it: https://github.com/StephenColor/flutter-palette-kit

Genuinely curious how others handle the IME case — on Korean/Chinese composition onChanged fires with the composing text, so the provider sees half-formed queries, and I currently only hide that behind the debounce instead of checking value.composing.

0 Upvotes

0 comments sorted by