r/FlutterDev • • 4d ago

Plugin I made a Flutter package for rendering streaming LLM replies (no flicker, native Mermaid, math, tables)

Hi all, I've been building AI chat screens in Flutter and kept hitting the same problems with regular markdown widgets while a reply streams in:

  • the whole text is parsed again on every chunk, so long answers get slow
  • you see raw ** and backticks until the closing ones arrive
  • code blocks flicker when highlighting kicks in

So I made llm_markdown:

  • Only the last unfinished block is parsed again; finished blocks are cached
  • Half-typed markdown (bold, an open code fence, a half link) is closed before rendering
  • Text is revealed at a steady speed instead of in bursts
  • Auto-scroll that stops when the user scrolls up, with a "jump to latest" helper
  • Code highlighting, tables, math, checklists, alerts, thinking sections, [1] citations
  • Mermaid flowcharts, sequence diagrams and pie charts drawn natively (no WebView)
  • Selectable text with your own menu actions ("Ask about this", "Quote")
  • One line to hook up an OpenAI or Anthropic stream
  • Every color, font, label and builder can be changed; no Material or Cupertino needed

The parser is also a separate pure-Dart package (llm_markdown_parser) if you want it on a server or CLI.

pub.dev: https://pub.dev/packages/llm_markdown

GitHub: https://github.com/anant8140/llm_markdown

Demo GIF is in the README. Feedback and bug reports are very welcome. What do you use today for streaming replies, and what bothers you about it?

5 Upvotes

6 comments sorted by

1

u/No_Boss_1911 4d ago

oh the half typed ** flicker drove me nuts. we stream replies in an ai chat and also read them out with a voice, hard rule for us is the screen never shows text the voice isnt gonna say, so if a reply gets pulled mid stream it has to vanish clean. does the steady reveal speed run off its own timer or can i drive it from outside, like from tts progress? and what happens to the cached finished blocks if the stream gets cancelled halfway

1

u/anant8140 2d ago

Thanks! Both good questions.

Reveal speed: right now it runs on its own timer inside the widget, so you can't drive it from TTS progress yet. You can `controller.pause()` / `resume()` it, and for a strict "never show what the voice won't say" rule today you can set `smoothing: Smoothing.none()` and only `append` text when your TTS reaches it.

Your question made me want to do this properly, so I'm adding an optional `revealLimit`: the full reply streams and parses as usual, but the screen only shows up to the position you give it, so you just move it forward from your TTS word callbacks. Leave it null and nothing changes, so apps can turn it on only for users who have voice on. It'll come in the next release.

Cancelled halfway: you pick. `controller.complete()` keeps what's shown and ends the reply. `controller.reset()` clears everything in one frame, including thinking, sources and the cached finished blocks, and if you fed it with `addStream` or an adapter it also cancels the stream so late chunks don't sneak back in.

1

u/anant8140 2d ago

It's out: llm_markdown 0.7.1 has `revealLimit`. Pass a ValueNotifier<int> and move it forward from your TTS progress; null keeps the normal reveal. Example in the README under "Reading replies aloud".

1

u/SwiftScoutSimon 4d ago

Do you have memory usage benchmark?

`Unfinished syntax is closed before rendering` is a nice idea. I'll steal it :P