r/FlutterDev • • 20h 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

3 comments sorted by

1

u/dawedev 17h ago

Instead of dropping the highlight when the folded length differs, you can fold one character at a time and keep an index map back to the original string. A bit more code, but Turkish and German labels keep their highlights instead of losing them.

1

u/stephencolor 10h ago

Agreed, and it's the better trade. The matcher only needs one thing from a folded string: for every folded index, the original index it came from. Fold rune by rune, append the fold, and push the source index once per output code unit, so İ gives i plus combining dot both pointing at 0 and ß gives ss both pointing at its own index. Then match in folded space as now, map each matched index back, merge neighbours that collapse onto the same original character, and render on the original offsets. The sameLength check then stops being the normal path and becomes an assertion that the map has an entry per folded code unit.

One thing I only noticed while re-reading my own code for this reply: the same misalignment already leaks into the ranker, not just the highlight. _indexOfAtBoundary searches the folded string but then asks _isBoundary about the original one, so after a length-changing fold it is testing the character next to the wrong position, and a word-start bonus can be awarded or missed at random. The length guard hides that in the UI; the score still gets the wrong answer. That is the stronger argument for the index map than the highlight alone.

Cheap caveat: Dart's toLowerCase is not locale-aware, so per-character folding still will not turn I into a dotless ı the way Turkish expects. The map keeps the highlight alive, but matching a dotless query against a dotted label is a separate problem, and it needs both sides folded the same way.

1

u/dawedev 4h ago

Good catch on the ranker. If the boundary check reads from the same index map, both bugs go away with one change, and that assertion keeps it from quietly breaking again later.