streamdown 0.0.1
streamdown: ^0.0.1 copied to clipboard
Flicker-free streaming markdown renderer for Flutter AI apps. Drop-in replacement for flutter_markdown with no flash on partial code fences, tables, or LaTeX.
streamdown #
Flicker-free streaming markdown renderer for Flutter AI apps.
A drop-in replacement for flutter_markdown that handles partial code fences, half-finished tables, and mid-stream LaTeX without re-parsing the prefix on every chunk. Built for ChatGPT-style apps where every token counts.
[streamdown vs flutter_markdown β same stream, side by side]
π¦ Status: functional, pre-release. All π΄ features implemented. See
TRACKER.mdfor the live build log.
Why streamdown? #
flutter_markdown re-parses the entire string on every chunk. For a 2000-token GPT response, that's ~400 re-parses + full re-highlight of code blocks + LaTeX re-renders. Result: visible flicker, jumping cursor, code blocks that flash unstyled β styled.
streamdown keeps an append-only AST, renders incomplete blocks provisionally (e.g., a half-finished ```dart becomes a code block immediately), and uses stable widget keys so Flutter's diff doesn't tear down + rebuild on every token.
flutter_markdown |
streamdown |
|
|---|---|---|
| Re-parse on every chunk | β (slow) | β (incremental) |
| Provisional code fence rendering | β | β |
| Provisional table rows | β | β |
| Stable widget keys during stream | β | β |
| Per-line syntax highlighting cache | β | β |
| LaTeX (optional) | β | β |
| Bundle size | ~80KB | <50KB |
Headline benchmark β 5KB markdown, 4-char chunks (typical AI stream cadence): streamdown is ~188Γ faster than re-parsing from scratch on every chunk. See test/perf/benchmark_test.dart.
Install #
dependencies:
streamdown: ^0.0.1
Basic usage #
import 'package:streamdown/streamdown.dart';
// Streaming (typical AI chat use case)
Streamdown(stream: openai.responseStream)
// Static (non-stream) markdown
Streamdown.text(fullMarkdownString)
That's it. Theme, code highlighting, link tap, and selectable text Just Work out of the box.
Advanced usage #
Streamdown(
stream: openai.responseStream,
syntaxTheme: SyntaxTheme.githubDark,
latex: true,
selectable: true,
onLinkTap: (uri) => launchUrl(uri),
codeBlockBuilder: (lang, code, isComplete) => MyCustomCodeBlock(...),
)
See example/ for six runnable scenarios including a side-by-side comparison with flutter_markdown.
How it works #
Three tricks combined:
- Incremental token-level parser β new tokens extend the trailing AST node; the prefix is never re-tokenized.
- Provisional rendering β an unclosed code fence renders as a code block immediately, then continues filling as lines stream in.
- Diff-stable widget keys β every AST node gets a deterministic key so Flutter's element diff doesn't tear down existing widgets.
See the architecture notes in CLAUDE.md.
Run the demos #
git clone https://github.com/jayu1023/streamdown
cd streamdown/example
flutter run
Six scenarios are included:
- Comparison demo β same stream rendered by
flutter_markdown(janky) vsstreamdown(smooth). - AI chat simulator β mocked LLM stream you can wire to your own provider.
- Syntax theme gallery β
githubLight,atomOneDark, andautoside-by-side. - Custom code block builder β replace the default with your own widget.
- Long-form article β static render of a multi-section markdown doc.
- LaTeX math β inline and block math via
flutter_math_fork.
Status #
| Feature category | Status |
|---|---|
| Streaming engine | β |
| Block-level markdown | β |
| Code blocks (syntax highlighted) | β |
| Tables (GFM, alignment, inline-in-cells) | β |
LaTeX (behind latex: true flag) |
β |
| Example app | β |
| Hero demo GIF | π§ record before publish |
| Pub.dev publish | π― Phase 9 |
See TRACKER.md for live status.
Contributing #
Issues and PRs welcome. See GitHub Issues.
License #
BSD-3-Clause. See LICENSE.