I spent the last week building a Kanban board engine for Flutter — lanes, cards, drag to reorder, drag across lanes, batch moves, filters, undo. No packages, about 5,000 lines of Dart, 289 tests.
The layout was the easy part. Almost everything that bit me was about order: how a card's position is stored, what index a drop actually refers to, and what happens when the list the user sees is not the list the model has. Three of those bugs are worth writing down, because they are the kind you only find by dragging something on a real phone.
1. A card's position is a string, not an integer
The obvious model is position: int and renumber 0..n on every drop. It works until a lane has 200 cards and the user drags one card from the top to the bottom: you rewrite 200 rows to move one.
The usual fix is a fractional index: a key that sorts lexicographically and can always be split, so a card dropped between two others gets a value strictly between theirs and nothing else changes.
Mine is a base-62 string over the alphabet 0-9A-Za-z, which is exactly ASCII order, so comparison is plain String.compareTo with no collation surprises:
RankKey.between(null, null); // '0V' first card in an empty lane RankKey.between('0V', null); // '0W' append RankKey.between('0V', '0W'); // '0VV' dropped between RankKey.between(null, '0V'); // '0F' prepend The midpoint digit is V (31 of 62). Appending bumps the last digit that has headroom; inserting between two keys walks their shared prefix until it finds a digit with a gap of at least 2 and splits it.
One rule makes the maths hold: a generated key never ends in 0. A trailing zero carries no value, so nothing can ever sort below it — a hand-written '0V0' would be a key you can append after but never insert before. between validates its bounds and throws rather than returning something that sorts on the wrong side.
Two things I got wrong in the doc comment, which I only noticed when I measured:
- I claimed a lane takes "31 appends on one character, then 62 on two, and so on — a thousand cards still sort on three characters." Measured: 30 appends keep two characters (
0W..0z), then the key shrinks to one character (1,2, …z) for 61 more, three characters start at append #92, and a thousand appends land at 11 characters. Still fine — 11 bytes per card — but the comment was optimistic fiction. - Prepending is the expensive direction. Starting at
0Vand pushing 60 cards onto the top of the lane gives a 14-character key — thirteen zeros followed byV. That is the price of puttingfirstKeynear the bottom of the alphabet, which is the right trade because every new card is appended, not prepended. For the rare lane that has been edited to death there isrebalance(), which rewrites the lane assequence(n)and keeps the order exactly.
The property that matters for the UI is not key length, it is that a drop touches one card:
final String? previous = insertion > 0 ? remaining[insertion - 1].rank : null; final String? next = insertion < remaining.length ? remaining[insertion].rank : null; for (final BoardCard card in moving) { final String rank = RankKey.between(previous, next); previous = rank; updated.add(card.copyWith(laneId: toLaneId, rank: rank)); } That loop is also why a batch move of five cards can be one undo step and one notify.
2. The drop index is a view index, and the engine reads it as a model index
This is my favourite bug because the code is correct on both sides and still wrong together.
Every gap between two cards is a DragTarget. On drop it reports the slot the user aimed at:
// lane_column.dart — index is a position in the list this lane renders _CardDropSlot(index: i, child: BoardCardTile(...)) i runs over controller.cardsInLane(lane.id), which is after the filter. So with a search or a label chip active, index 2 means "third visible card".
The engine's contract says the same thing:
indexis the slot in the destination lane as it is displayed now, counted from0tolaneLength.
But it computes against board.cardsInLane(toLaneId) the model, unfiltered. As long as no filter is hiding anything those two lists are the same. The moment one is hidden, they are not, and the card lands somewhere the drop line never showed.
A real run, from a throwaway test I wrote to prove it to myself. Lane todo holds a, b, c, d; only b and c carry the bug label; filter on bug shows [b, c]; drag x in from another lane and drop it at the bottom of the visible lane (index 2):
before: todo: a b c d visible: [b, c] drop x at visible index 2 after: todo: a b x c d visible: [b, x, c] // x is between b and c, not at the bottom The user aimed below c and the card appeared between b and c.
Same lane is worse, because the "did anything change?" guard hides it:
visible: [b, c] drag b below c -> index 2 engine: insertion = 2 - 1 card above = 1 remaining = [a, c, d] -> a b c d == the list before the move result: MoveFailure.unchanged ("The card is already there") The card snaps back with a snackbar that says it was already there, while the drop indicator was two slots away. Nothing crashed, nothing threw, and both halves would pass their own review.
There are two honest fixes and they are not equivalent:
- Map the visible index to a model index in the controller before calling the engine. Cheap, but it only fixes filtering — it does nothing for a lane that is sorted by due date or priority, where "displayed index 2" is also not a model index.
- Change the drop payload from an integer to the neighbours: each gap knows the id of the card above and below it, and the engine places the moved card between those two cards' ranks. The engine stops caring how the lane is filtered or sorted, because the anchor is a card, not a position.
The second is the design I want, and it is not in 1.0: the drop target still passes an int. If you take one thing from this post, take that — an index into a visible list is not a position in your data, and the bug will be silent.
3. Batch moves need three separate decisions, not one
Dragging five selected cards onto a lane looks like the single-card case, and it is not. The engine has to answer three questions the UI never asks out loud:
Which slot? The index is counted against the lane including the cards being moved, because that is what the indicator showed. For cards coming from the same lane, subtract the ones that sat above the drop point — and only those:
if (fromLaneId == toLaneId) { for (final BoardCard card in moving) { if (card.laneId != toLaneId) continue; // a card from another lane has final int at = board.indexInLane(card.id); // an index in *its* lane if (at >= 0 && at < index) above++; } insertion -= above; } That continue is a regression fix. The first version adjusted the slot for every moving card's indexInLane, so a batch whose first card was already in the destination counted a card from another lane and landed one slot early.
Whose order? The ids arrive in the order the user selected them, which is not always the order they are looking at. A selection-based move sorts them by board order first, so the batch does not scramble lanes; an explicit drag keeps the order given, because that is what the pointer shows.
Does the WIP limit count once or per card? Once. Two cards into a lane limited to 2 is one drop of two, refused as a whole:
if (enforceWip && limit != null && incoming > 0 && occupancy + incoming > limit) { return MoveResult.rejected(MoveFailure.wipLimit, '${lane.title} is limited to $limit cards'); } Reordering inside the lane never counts as incoming, even when the lane is already over its limit — otherwise lowering a limit on a full lane would make its own cards undraggable. That is also why the lane header has a fourth state, overLimit: existing cards stay, new drops are refused, and the header turns red instead of lying about the count.
Undo is a snapshot of the previous board, so all of this — five ranks, two lanes, one selection prune — undoes in one step, and redo is cleared the moment the user does something new.
4. Filtering and highlighting disagreed on İstanbul
Search is cheap: lower-case the query once, lower-case the card's searchable text, contains per token. Highlighting is where the naive version breaks, so the highlighter deliberately does not use the lowered copy — it runs a case-insensitive RegExp over the original title, because indices from a lowered string can point one place off:
final RegExp pattern = RegExp(RegExp.escape(needle), caseSensitive: false); for (final RegExpMatch match in pattern.allMatches(title)) { ... } That avoids slicing past the end of the title. It does not avoid the real problem, which I found by testing a Turkish title instead of trusting the comment:
card title: 'İstanbul release' query: 'istanbul' filter match: true // 'İstanbul'.toLowerCase() == 'i' highlight: [] // the case-insensitive RegExp finds no 'i' in 'İstanbul' Dart's toLowerCase uses simple case mapping, so İ becomes i — one code unit — and the lowered haystack contains istanbul exactly. But caseSensitive: false in Dart's RegExp canonicalises both sides by upper-casing, and İ upper-cases to İ, not to I. So the card is in the result list with nothing highlighted, which to the user looks like the search is broken.
ß fails the other way: 'Straße' does not match a query of strasse, because 'ß'.toLowerCase() is still 'ß' (no full case folding).
The fix is the same one I ended up using in a command palette: fold per rune, append the folded output, and record which original index every output code unit came from. Match in the folded space, then map the spans back and merge neighbours that come from the same original character.
There is a defensive clamp in the tile that slices the title:
final int start = span.start.clamp(0, card.title.length); final int end = span.end.clamp(start, card.title.length); It is there because a caller-supplied query could hand back a range from a different locale's folding. In practice it never fired in 289 tests; the disagreement above is the bug that actually happens.
Smaller things that still mattered
- Selection is allowed to contain hidden cards. "Select all, then filter, then move" must move everything that was selected, not the half that is still visible. Selection lives beside the board, not inside it, and moves sort it into board order before handing it to the engine.
- Auto-scroll is a pure function. Dragging a card to a lane that is off-screen is the one gesture a phone cannot do by itself. The velocity depends only on pointer position (64 px edge zone, up to 1100 px/s at the border), so it is tested without a
ScrollViewor a timer, and the widget just feeds it the pointer. - A drag is modelled, not stored in the widget. Picked up / over lane N at slot M / dropped are three controller methods. The drop reads exactly the values that painted the indicator, and the indicator does not flicker between two gaps because the controller is the source of truth while the target's candidate list is briefly empty.
What I'd do differently
Anchor-based drop payloads instead of integer indices, from the first commit. Every other bug in this list was local and visible; that one was two correct-looking halves whose contract nobody had written down as a test — "the index is a displayed index" was documented in the engine and violated by every filter.
The engine, ten screens, and the full test suite live here, if you want to read the rest of it:
