📚 Encyclopedia

Flutter & Mobile · updated 2026-10-26

Rendering markdown in Flutter (selection, code-block builders, tappable task lists)

flutterflutter_markdown_plusmarkdownselectionareatext-selectionclipboardchecklistsgfm

TLDR renders every post, AI answer, doc and share page through one widget that wraps flutter_markdown_plus (the maintained fork of flutter_markdown). Three features built on it in October 2026 each ran into package internals that are not documented: whole-document text selection, a copy button on code blocks, and checkboxes that can be ticked. The lessons apply to any app that renders markdown with this package. Versions: flutter_markdown_plus 1.0.7, Flutter 3.41.

One renderer, one hook

Route all markdown through a single wrapper widget (TLDR: FileAwareMarkdown). Then a builder or selection change lands everywhere at once: post bodies, responses, docs, public pages, the editor preview. Inline code, DMs rendered as plain linkified text, and similar surfaces are separate and need their own handling.

Selection: one SelectionArea, never per-block selectable text

MarkdownBody(selectable: true) renders one SelectableText per block. Each block is its own selection island, so a user can select one paragraph or one bullet, never the whole answer, and Select All stops at a block. This regressed twice in TLDR: once when a "make links clickable" change replaced the wrapper with selectable: true, again when a code-block widget used its own SelectableText.

The working setup: - Wrap the whole rendered body in one SelectionArea, and pass selectable: false to MarkdownBody and to every custom builder. - Links inside a SelectionArea are tappable on current Flutter (the old reason to avoid the wrapper no longer applies). Prove it with a test rather than trusting it. - Fix the copied text. Flutter's stock StaticSelectionContainerDelegate concatenates blocks with no separator, so two bullets copy as •first•second. Put a SelectionContainer with a custom delegate (subclass StaticSelectionContainerDelegate, override getSelectedContent) inside the area and join pieces by geometry: compute each selectable's global bounding rect (boundingBoxes + getTransformTo(null)); a piece starting below the previous piece's vertical midpoint gets a newline; on the same line, a space after a list marker (•, 1., -) and a tab otherwise, so table rows paste as columns. - Regression test: tester.tapOnText on a link (it opens), then select all + copy and compare the clipboard to an exact string. Run it on the VM and with flutter test --platform chrome, because selection is implemented differently on web.

Rule written into the project: never put SelectableText or MarkdownBody(selectable: true) inside the shared renderer.

Custom block builders: a copy button on code blocks

Register a MarkdownElementBuilder for tag pre (builders: {'pre': CodeBlockBuilder()}) with isBlockElement() => true. What the package does with it (read builder.dart): - It calls the builder's visitText; returning null means the default code text is not added, so the builder must render the code itself. Trim the trailing newline the way the package's formatText() does. - It still wraps the builder's widget in the stylesheet's codeblockDecoration container, so background and border stay the package's. Don't draw a second background. - The builder is not given the merged stylesheet. Pass it in yourself (MarkdownStyleSheet.fromTheme(theme).merge(overrides)) while MarkdownBody keeps getting the raw overrides, so rendering does not change.

The widget itself: a Stack with the code in a horizontal Scrollbar + SingleChildScrollView (code should scroll, not wrap) and a small positioned button top-right that swaps its icon to a check mark for 2 seconds after copying, with a tooltip and semantics label. Add right padding so a short line never sits under the button. Render the code with Text.rich(softWrap: false); selection comes from the outer SelectionArea.

Tappable task lists (checklists)

GFM task-list lines (- [ ] item, - [x] item) render as checkboxes via checkboxBuilder. Making them tickable means mapping a rendered box back to a line in the source text: - checkboxBuilder is called in post-order (after an li's children are built), not source order, so nested lists come out shuffled. Re-parse the markdown to compute build order to source order, and keep the counter modulo the box count because the tree can be rebuilt. - A loose task list (blank lines between items) renders no checkbox at all: the marker ends up inside a <p> and is dropped. If the number of rendered boxes differs from the number of task lines in the source, keep every box read-only rather than tick the wrong line. - Define the index once: "the Nth line matching the task-line regex, outside `` and ~~~ fences", allowing blockquote prefixes and numbered markers. If the server applies ticks too, mirror that function exactly in the server language and check parity on the same inputs (TLDR compared md5s of the Dart and PHP outputs). - Server side, toggle by index with the row locked (SELECT ... FOR UPDATE), return the new body, and don't write edit history for a tick (a normal edit would also re-run tag extraction). Reject a non-integer index (400), a post with no boxes (422), and non-owners (403). - Client: optimistic toggle with per-box rollback and a snackbar on failure. - When converting markdown to a rich-text editor's format, add only the task-list block syntaxes; enabling all of GFM changes round-tripping of other constructs such as tables. - Test gotcha: a code-block copy button is anInkWell, so "no InkWell in the tree" is not a valid read-only check. Look for anInkWell` ancestor of the box icons instead.

Android: selection-menu items from other apps

Flutter's text-selection toolbars on Android list other apps' PROCESS_TEXT text actions (if the app's manifest declares the <queries> entry for that intent). So "add a Search-in-my-browser item to the selection menu" can be done by adding a text action to the other app, with no change to the Flutter app; it then also appears in every other Android app. flutter_markdown_plus gives no hook to edit its selection menu, which makes this the cheaper route.

See also

Linked from

Sources: tldr memory (bug-selection-single-block, feature-code-block-copy, feature-checklists), posts #1788, #1831, #1813, tldr commits 737f64b, 82f0eed, d6e6338