### Verify gpt_markdown installation Source: https://gptmarkdown.com/docs/installation A basic example of how to use the GptMarkdown widget to render Markdown content. ```dart GptMarkdown('# Hello from GPT Markdown!') ``` -------------------------------- ### Import gpt_markdown Source: https://gptmarkdown.com/docs/installation How to import the gpt_markdown package into your Dart file. ```dart import 'package:gpt_markdown/gpt_markdown.dart'; ``` -------------------------------- ### Add gpt_markdown via pubspec.yaml Source: https://gptmarkdown.com/docs/installation Manually add the gpt_markdown package to your pubspec.yaml file and then run 'flutter pub get'. ```yaml dependencies: gpt_markdown: ^1.1.6 ``` -------------------------------- ### All theme properties Source: https://gptmarkdown.com/docs/themes Example demonstrating various theme properties available in GptMarkdownThemeData. ```dart GptMarkdownThemeData( brightness: Brightness.light, // Style each heading level independently h1: const TextStyle(fontSize: 32, fontWeight: FontWeight.w900, letterSpacing: -0.5), h2: const TextStyle(fontSize: 24, fontWeight: FontWeight.w700), h3: const TextStyle(fontSize: 20, fontWeight: FontWeight.w600), // h4, h5, h6 follow the same pattern // Horizontal rule (--- in Markdown) hrLineColor: Colors.grey.shade300, hrLineThickness: 1.0, hrLinePadding: const EdgeInsets.symmetric(vertical: 12), // Inline code / highlight background highlightColor: Colors.amber.withOpacity(0.2), // Whether # headings get a divider line below them autoAddDividerLineAfterH1: false, ) ``` -------------------------------- ### Install GPT Markdown Source: https://gptmarkdown.com/docs Add the gpt_markdown package to your Flutter project. ```bash flutter pub add gpt_markdown ``` -------------------------------- ### Inline Math Examples Source: https://gptmarkdown.com/docs/latex-support Demonstrates the two syntaxes for inline LaTeX math rendering: backslash-paren (always enabled) and dollar-sign (opt-in). ```dart // Backslash-paren syntax (always enabled) GptMarkdown(r'The formula is \( E = mc^2 \) — inline.') // Dollar-sign syntax (opt-in) GptMarkdown( r'The formula is $E = mc^2$ — inline.', useDollarSignsForLatex: true, ) ``` -------------------------------- ### Block Math Examples Source: https://gptmarkdown.com/docs/latex-support Shows how to render block-level LaTeX equations, which are centered and displayed at a larger size. Includes backslash-bracket (always enabled) and double-dollar (opt-in) syntaxes. ```dart // Backslash-bracket syntax (always enabled) GptMarkdown(r''' The quadratic formula: \[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \] ''') // Double-dollar syntax (opt-in) GptMarkdown( r''' $$ \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2} $$ ''', useDollarSignsForLatex: true, ) ``` -------------------------------- ### Minimal example Source: https://gptmarkdown.com/docs/usage The most basic usage of GptMarkdown, passing a simple markdown string. ```dart import 'package:gpt_markdown/gpt_markdown.dart'; // Minimal — just pass your string GptMarkdown('**Hello!** Inline math: \( E = mc^2 \)') ``` -------------------------------- ### Custom image renderer Source: https://gptmarkdown.com/docs/usage Implementing a custom image renderer to control how images are displayed, with an example using `Image.network` and `ClipRRect`. ```dart GptMarkdown( content, imageBuilder: (context, url, alt) { return ClipRRect( borderRadius: BorderRadius.circular(8), child: Image.network(url, semanticLabel: alt), ); }, ) ``` -------------------------------- ### Handling link taps Source: https://gptmarkdown.com/docs/usage Example of how to intercept link taps in Markdown using the `onLinkTap` callback. ```dart GptMarkdown( content, onLinkTap: (url, title) { launchUrl(Uri.parse(url)); }, ) ``` -------------------------------- ### Fixing AI Output Quirks Source: https://gptmarkdown.com/docs/latex-support Provides an example of using `latexWorkaround` to handle AI models that might double-escape backslashes in LaTeX output. ```dart // Some AI models output "\\(" instead of "\(" // Use latexWorkaround to normalize it GptMarkdown( aiResponse, latexWorkaround: (tex) => tex.replaceAll('\\', '\'), ) ``` -------------------------------- ### Streaming AI output Source: https://gptmarkdown.com/docs/usage Demonstrates how to handle streaming AI output by appending chunks to a string buffer and updating the GptMarkdown widget. ```dart import 'package:flutter/material.dart'; import 'package:gpt_markdown/gpt_markdown.dart'; class StreamingChat extends StatefulWidget { const StreamingChat({super.key}); @override State createState() => _StreamingChatState(); } class _StreamingChatState extends State { String _buffer = ''; void _onChunk(String chunk) { setState(() => _buffer += chunk); } @override Widget build(BuildContext context) { return SingleChildScrollView( padding: const EdgeInsets.all(16), child: GptMarkdown(_buffer), ); } } ``` -------------------------------- ### Streaming-Aware Rendering Source: https://gptmarkdown.com/docs/syntax-highlighting Illustrates how to use the `closed` parameter in `codeBuilder` to handle code blocks that are being streamed by an LLM, allowing for different UI states during streaming. ```dart 1// The 'closed' parameter tells you whether the code fence is complete. 2// Use it to show a placeholder while the model is still streaming. 3codeBuilder: (context, name, code, closed) { 4 if (!closed) { 5 return Container( 6 padding: const EdgeInsets.all(12), 7 decoration: BoxDecoration( 8 color: Colors.grey.shade900, 9 borderRadius: BorderRadius.circular(8), 10 ), 11 child: Text( 12 code, 13 style: const TextStyle(fontFamily: 'monospace', color: Colors.white70), 14 ), 15 ); 16 } 17 // Full renderer once complete 18 return MyHighlightedCodeBlock(language: name, code: code); 19} ``` -------------------------------- ### Registering multiple Source: https://gptmarkdown.com/docs/custom-components Pass any number of components in the list — they are tested in order. ```dart GptMarkdown( content, components: [ calloutComponent, sectionDividerComponent, codePlaygroundComponent, ], inlineComponents: [ badgeComponent, tooltipComponent, ], ) ``` -------------------------------- ### Default Syntax Highlighting Source: https://gptmarkdown.com/docs/syntax-highlighting Demonstrates how fenced code blocks with language identifiers are highlighted automatically without extra configuration. ```dart 1// Syntax highlighting works out of the box. 2// Just include a fenced code block in your Markdown: 3GptMarkdown(r''' 4```dart 5void main() { 6 print('Hello, world!'); 7} 8``` 9 10```python 11def greet(name): 12 return f"Hello, {name}!" 13``` 14''') ``` -------------------------------- ### Custom Code Block Renderer Source: https://gptmarkdown.com/docs/syntax-highlighting Shows how to use the `codeBuilder` callback to provide a custom widget for rendering code blocks, receiving language, code, and a closed flag. ```dart 1import 'package:flutter/material.dart'; 2import 'package:gpt_markdown/gpt_markdown.dart'; 3 4GptMarkdown( 5 content, 6 // name = language identifier (e.g. "dart", "python", "js") 7 // code = the raw code string 8 // closed = false while streaming (incomplete fence), true when done 9 codeBuilder: (context, name, code, closed) { 10 return Container( 11 width: double.infinity, 12 decoration: BoxDecoration( 13 color: const Color(0xFF1E1E1E), 14 borderRadius: BorderRadius.circular(8), 15 ), 16 padding: const EdgeInsets.all(16), 17 child: Column( 18 crossAxisAlignment: CrossAxisAlignment.start, 19 children: [ 20 if (name.isNotEmpty) 21 Text( 22 name, 23 style: const TextStyle( 24 color: Colors.grey, 25 fontSize: 12, 26 ), 27 ), 28 const SizedBox(height: 8), 29 SelectableText( 30 code, 31 style: const TextStyle( 32 fontFamily: 'monospace', 33 color: Colors.greenAccent, 34 fontSize: 13, 35 height: 1.5, 36 ), 37 ), 38 ], 39 ), 40 ); 41 }, 42) ``` -------------------------------- ### Custom LaTeX renderer Source: https://gptmarkdown.com/docs/usage Overriding the LaTeX renderer and enabling dollar-sign syntax for inline and display math. ```dart GptMarkdown( content, useDollarSignsForLatex: true, // enables $...$ and $$...$$ syntax latexBuilder: (context, latex, textStyle, inline) { return MyLatexRenderer( equation: latex, inline: inline, ); }, ) ``` -------------------------------- ### Custom LaTeX Renderer Source: https://gptmarkdown.com/docs/latex-support Demonstrates how to replace the default LaTeX renderer with a custom widget using the `latexBuilder` property. ```dart import 'package:flutter_math_fork/flutter_math.dart'; GptMarkdown( content, latexBuilder: (context, latex, textStyle, inline) { if (inline) { return Math.tex( latex, textStyle: textStyle, ); } return Center( child: Math.tex( latex, textStyle: textStyle?.copyWith(fontSize: 20), ), ); }, ) ``` -------------------------------- ### Custom link renderer Source: https://gptmarkdown.com/docs/usage Providing a custom widget for rendering Markdown links, allowing for custom tap handling and styling. ```dart GptMarkdown( content, linkBuilder: (context, text, href, title) { return InkWell( onTap: () => launchUrl(Uri.parse(href)), child: Text( text, style: const TextStyle( color: Colors.blue, decoration: TextDecoration.underline, ), ), ); }, ) ``` -------------------------------- ### Basic Usage Source: https://gptmarkdown.com/docs Render Markdown and LaTeX content using the GptMarkdown widget. ```dart import 'package:gpt_markdown/gpt_markdown.dart'; GptMarkdown( r'**Hello!** Inline math: \( E = mc^2 \)', ) ``` -------------------------------- ### Text style & direction Source: https://gptmarkdown.com/docs/usage Customizing the text style, enabling RTL support, and setting text alignment for the Markdown content. ```dart GptMarkdown( content, style: const TextStyle( fontSize: 16, height: 1.6, color: Colors.black87, ), textDirection: TextDirection.rtl, // RTL language support textAlign: TextAlign.justify, ) ``` -------------------------------- ### Citation / source tags Source: https://gptmarkdown.com/docs/custom-components The package has first-class support for AI citation tags via `sourceTagBuilder`. ```dart // The package has built-in support for AI citation tags like: // 1 or [1] style references. // Use sourceTagBuilder to render them your way: GptMarkdown( content, sourceTagBuilder: (context, sources) { return Row( mainAxisSize: MainAxisSize.min, children: sources.map((src) => GestureDetector( onTap: () => openSource(src), child: Container( margin: const EdgeInsets.only(left: 2), padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1), decoration: BoxDecoration( color: Colors.blue.shade50, borderRadius: BorderRadius.circular(4), ), child: Text( src, style: const TextStyle(fontSize: 10, color: Colors.blue), ), ), )).toList(), ); }, ) ``` -------------------------------- ### Full signature Source: https://gptmarkdown.com/docs/style-configuration The full signature of the GptMarkdown widget, showing all available parameters and their types. ```dart GptMarkdown( 'your markdown string', // required, positional // Text style: TextStyle(...), // base text style textDirection: TextDirection.ltr, // LTR (default) or RTL textAlign: TextAlign.start, // text alignment textScaler: TextScaler.linear(1), // text scaling maxLines: null, // max lines (null = unlimited) overflow: TextOverflow.clip, // overflow behaviour // Links onLinkTap: (url, title) { }, // handle link taps followLinkColor: false, // inherit link color from style linkBuilder: (ctx, text, href, title) => Widget, // LaTeX useDollarSignsForLatex: false, // enable $...$ and $$...$$ syntax latexWorkaround: (tex) => tex, // normalize AI output quirks latexBuilder: (ctx, latex, style, inline) => Widget, // Code blocks codeBuilder: (ctx, lang, code, closed) => Widget, // Inline code / highlight highlightBuilder: (ctx, code, style) => Widget, // Images imageBuilder: (ctx, url, alt) => Widget, // Lists orderedListBuilder: (ctx, num, style) => Widget, unOrderedListBuilder: (ctx, depth, style) => Widget, // Tables tableBuilder: (ctx, headers, rows) => Widget, // Source tags (citations) sourceTagBuilder: (ctx, sources) => Widget, // Custom block-level components components: [...], // Custom inline components inlineComponents: [...], ) ``` -------------------------------- ### Custom code block renderer Source: https://gptmarkdown.com/docs/usage Replacing the default code block renderer with a custom widget using the `codeBuilder` property. ```dart GptMarkdown( content, codeBuilder: (context, name, code, closed) { return MyCustomCodeBlock( language: name, code: code, isClosed: closed, ); }, ) ``` -------------------------------- ### Inside a Scaffold Source: https://gptmarkdown.com/docs/usage Integrating GptMarkdown within a Flutter Scaffold, wrapped in a SingleChildScrollView to prevent overflow. ```dart import 'package:flutter/material.dart'; import 'package:gpt_markdown/gpt_markdown.dart'; class MarkdownScreen extends StatelessWidget { final String content; const MarkdownScreen({super.key, required this.content}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Response')), body: SingleChildScrollView( padding: const EdgeInsets.all(16), child: GptMarkdown(content), ), ); } } ``` -------------------------------- ### App-wide theme via ThemeData Source: https://gptmarkdown.com/docs/themes Register GptMarkdownThemeData as a ThemeData extension to apply themes app-wide. Supports separate light and dark themes. ```dart import 'package:flutter/material.dart'; import 'package:gpt_markdown/gpt_markdown.dart'; MaterialApp( theme: ThemeData.light().copyWith( extensions: [ GptMarkdownThemeData( brightness: Brightness.light, linkColor: Colors.indigo, linkHoverColor: Colors.indigoAccent, highlightColor: Colors.yellow.withOpacity(0.3), autoAddDividerLineAfterH1: true, ), ], ), darkTheme: ThemeData.dark().copyWith( extensions: [ GptMarkdownThemeData( brightness: Brightness.dark, linkColor: Colors.lightBlueAccent, ), ], ), home: const MyApp(), ) ``` -------------------------------- ### Inline component Source: https://gptmarkdown.com/docs/custom-components An inline component that renders `NEW` as a colored pill badge, rendered within a sentence. ```dart import 'package:flutter/material.dart'; import 'package:gpt_markdown/gpt_markdown.dart'; // A custom inline component that renders colored badges. // Triggered by: NEW final badgeComponent = InlineMarkdownComponent( exp: RegExp(r'(.*?)'), builder: (context, match, style) { final color = match.group(1) ?? 'blue'; final label = match.group(2) ?? ''; final colorMap = { 'green': Colors.green, 'blue': Colors.blue, 'red': Colors.red, 'orange': Colors.orange, }; final c = colorMap[color] ?? Colors.blue; return Container( padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2), decoration: BoxDecoration( color: c.withOpacity(0.15), borderRadius: BorderRadius.circular(4), border: Border.all(color: c.withOpacity(0.4)), ), child: Text( label, style: style?.copyWith( color: c.shade700, fontSize: 11, fontWeight: FontWeight.w600, letterSpacing: 0.3, ), ), ); }, ); // Register it: GptMarkdown( content, inlineComponents: [badgeComponent], ) ``` -------------------------------- ### Block component Source: https://gptmarkdown.com/docs/custom-components A block component that turns `...` into a styled callout box — useful when your AI model is prompted to output structured annotations. ```dart import 'package:flutter/material.dart'; import 'package:gpt_markdown/gpt_markdown.dart'; // A custom block component that renders a styled callout box. // Triggered when the AI outputs: text final calloutComponent = MarkdownComponent( exp: RegExp(r'(.*?)', dotAll: true), builder: (context, match, style) { final type = match.group(1) ?? 'info'; final text = match.group(2) ?? ''; final colors = { 'warning': (Colors.orange.shade50, Colors.orange.shade400), 'error': (Colors.red.shade50, Colors.red.shade400), 'info': (Colors.blue.shade50, Colors.blue.shade400), }; final (bg, border) = colors[type] ?? colors['info']!; return Container( margin: const EdgeInsets.symmetric(vertical: 8), padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: bg, border: Border(left: BorderSide(color: border, width: 4)), borderRadius: BorderRadius.circular(4), ), child: Text(text, style: style), ); }, ); // Register it: GptMarkdown( content, components: [calloutComponent], ) ``` -------------------------------- ### Scoped theme with GptMarkdownTheme Source: https://gptmarkdown.com/docs/themes Use the GptMarkdownTheme widget to apply a theme to a specific part of the widget tree. ```dart import 'package:gpt_markdown/gpt_markdown.dart'; // Or wrap a specific widget instead of the whole app GptMarkdownTheme( gptThemeData: GptMarkdownThemeData( brightness: Brightness.light, h1: const TextStyle(fontSize: 28, fontWeight: FontWeight.w900), h2: const TextStyle(fontSize: 22, fontWeight: FontWeight.w700), linkColor: Colors.deepPurple, hrLineColor: Colors.grey.shade300, hrLineThickness: 1.5, hrLinePadding: const EdgeInsets.symmetric(vertical: 8), ), child: GptMarkdown(content), ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.