Showing
7 changed files
with
86 additions
and
24 deletions
| @@ -76,6 +76,16 @@ class _MyHomePageState extends State<MyHomePage> { | @@ -76,6 +76,16 @@ class _MyHomePageState extends State<MyHomePage> { | ||
| 76 | TextDirection _direction = TextDirection.ltr; | 76 | TextDirection _direction = TextDirection.ltr; |
| 77 | final TextEditingController _controller = TextEditingController( | 77 | final TextEditingController _controller = TextEditingController( |
| 78 | text: r''' | 78 | text: r''' |
| 79 | +This is a sample markdown document. | ||
| 80 | + | ||
| 81 | +* **bold** | ||
| 82 | +* *italic* | ||
| 83 | +* **_bold and italic_** | ||
| 84 | +* ~~strikethrough~~ | ||
| 85 | +* `code` | ||
| 86 | +* [link](https://www.google.com)  | ||
| 87 | + | ||
| 88 | + | ||
| 79 | ```markdown | 89 | ```markdown |
| 80 | # Complex Markdown Document for Testing | 90 | # Complex Markdown Document for Testing |
| 81 | 91 |
| @@ -198,4 +198,27 @@ class GptMarkdownConfig { | @@ -198,4 +198,27 @@ class GptMarkdownConfig { | ||
| 198 | overflow: overflow, | 198 | overflow: overflow, |
| 199 | ); | 199 | ); |
| 200 | } | 200 | } |
| 201 | + | ||
| 202 | + /// A method to check if the configuration is the same. | ||
| 203 | + bool isSame(GptMarkdownConfig other) { | ||
| 204 | + return style == other.style && | ||
| 205 | + textAlign == other.textAlign && | ||
| 206 | + textScaler == other.textScaler && | ||
| 207 | + maxLines == other.maxLines && | ||
| 208 | + overflow == other.overflow && | ||
| 209 | + followLinkColor == other.followLinkColor && | ||
| 210 | + // latexWorkaround == other.latexWorkaround && | ||
| 211 | + // components == other.components && | ||
| 212 | + // inlineComponents == other.inlineComponents && | ||
| 213 | + // latexBuilder == other.latexBuilder && | ||
| 214 | + // sourceTagBuilder == other.sourceTagBuilder && | ||
| 215 | + // codeBuilder == other.codeBuilder && | ||
| 216 | + // orderedListBuilder == other.orderedListBuilder && | ||
| 217 | + // unOrderedListBuilder == other.unOrderedListBuilder && | ||
| 218 | + // linkBuilder == other.linkBuilder && | ||
| 219 | + // imageBuilder == other.imageBuilder && | ||
| 220 | + // highlightBuilder == other.highlightBuilder && | ||
| 221 | + // onLinkTab == other.onLinkTab && | ||
| 222 | + textDirection == other.textDirection; | ||
| 223 | + } | ||
| 201 | } | 224 | } |
| @@ -179,6 +179,7 @@ class GptMarkdown extends StatelessWidget { | @@ -179,6 +179,7 @@ class GptMarkdown extends StatelessWidget { | ||
| 179 | // tex = _removeExtraLinesInsideBlockLatex(tex); | 179 | // tex = _removeExtraLinesInsideBlockLatex(tex); |
| 180 | return ClipRRect( | 180 | return ClipRRect( |
| 181 | child: MdWidget( | 181 | child: MdWidget( |
| 182 | + context, | ||
| 182 | tex, | 183 | tex, |
| 183 | true, | 184 | true, |
| 184 | config: GptMarkdownConfig( | 185 | config: GptMarkdownConfig( |
| @@ -294,7 +294,7 @@ class CheckBoxMd extends BlockMd { | @@ -294,7 +294,7 @@ class CheckBoxMd extends BlockMd { | ||
| 294 | return CustomCb( | 294 | return CustomCb( |
| 295 | value: ("${match?[1]}" == "x"), | 295 | value: ("${match?[1]}" == "x"), |
| 296 | textDirection: config.textDirection, | 296 | textDirection: config.textDirection, |
| 297 | - child: MdWidget("${match?[2]}", false, config: config), | 297 | + child: MdWidget(context, "${match?[2]}", false, config: config), |
| 298 | ); | 298 | ); |
| 299 | } | 299 | } |
| 300 | } | 300 | } |
| @@ -315,7 +315,7 @@ class RadioButtonMd extends BlockMd { | @@ -315,7 +315,7 @@ class RadioButtonMd extends BlockMd { | ||
| 315 | return CustomRb( | 315 | return CustomRb( |
| 316 | value: ("${match?[1]}" == "x"), | 316 | value: ("${match?[1]}" == "x"), |
| 317 | textDirection: config.textDirection, | 317 | textDirection: config.textDirection, |
| 318 | - child: MdWidget("${match?[2]}", false, config: config), | 318 | + child: MdWidget(context, "${match?[2]}", false, config: config), |
| 319 | ); | 319 | ); |
| 320 | } | 320 | } |
| 321 | } | 321 | } |
| @@ -394,7 +394,7 @@ class UnOrderedList extends BlockMd { | @@ -394,7 +394,7 @@ class UnOrderedList extends BlockMd { | ||
| 394 | ) { | 394 | ) { |
| 395 | var match = this.exp.firstMatch(text); | 395 | var match = this.exp.firstMatch(text); |
| 396 | 396 | ||
| 397 | - var child = MdWidget("${match?[1]?.trim()}", true, config: config); | 397 | + var child = MdWidget(context, "${match?[1]?.trim()}", true, config: config); |
| 398 | 398 | ||
| 399 | return config.unOrderedListBuilder?.call( | 399 | return config.unOrderedListBuilder?.call( |
| 400 | context, | 400 | context, |
| @@ -432,7 +432,7 @@ class OrderedList extends BlockMd { | @@ -432,7 +432,7 @@ class OrderedList extends BlockMd { | ||
| 432 | 432 | ||
| 433 | var no = "${match?[1]}"; | 433 | var no = "${match?[1]}"; |
| 434 | 434 | ||
| 435 | - var child = MdWidget("${match?[2]?.trim()}", true, config: config); | 435 | + var child = MdWidget(context, "${match?[2]?.trim()}", true, config: config); |
| 436 | return config.orderedListBuilder?.call( | 436 | return config.orderedListBuilder?.call( |
| 437 | context, | 437 | context, |
| 438 | no, | 438 | no, |
| @@ -971,6 +971,7 @@ class TableMd extends BlockMd { | @@ -971,6 +971,7 @@ class TableMd extends BlockMd { | ||
| 971 | vertical: 4, | 971 | vertical: 4, |
| 972 | ), | 972 | ), |
| 973 | child: MdWidget( | 973 | child: MdWidget( |
| 974 | + context, | ||
| 974 | (e[index] ?? "").trim(), | 975 | (e[index] ?? "").trim(), |
| 975 | false, | 976 | false, |
| 976 | config: config, | 977 | config: config, |
| 1 | part of 'gpt_markdown.dart'; | 1 | part of 'gpt_markdown.dart'; |
| 2 | 2 | ||
| 3 | /// It creates a markdown widget closed to each other. | 3 | /// It creates a markdown widget closed to each other. |
| 4 | -class MdWidget extends StatelessWidget { | 4 | +class MdWidget extends StatefulWidget { |
| 5 | const MdWidget( | 5 | const MdWidget( |
| 6 | + this.context, | ||
| 6 | this.exp, | 7 | this.exp, |
| 7 | this.includeGlobalComponents, { | 8 | this.includeGlobalComponents, { |
| 8 | super.key, | 9 | super.key, |
| @@ -11,6 +12,7 @@ class MdWidget extends StatelessWidget { | @@ -11,6 +12,7 @@ class MdWidget extends StatelessWidget { | ||
| 11 | 12 | ||
| 12 | /// The expression to be displayed. | 13 | /// The expression to be displayed. |
| 13 | final String exp; | 14 | final String exp; |
| 15 | + final BuildContext context; | ||
| 14 | 16 | ||
| 15 | /// Whether to include global components. | 17 | /// Whether to include global components. |
| 16 | final bool includeGlobalComponents; | 18 | final bool includeGlobalComponents; |
| @@ -19,25 +21,46 @@ class MdWidget extends StatelessWidget { | @@ -19,25 +21,46 @@ class MdWidget extends StatelessWidget { | ||
| 19 | final GptMarkdownConfig config; | 21 | final GptMarkdownConfig config; |
| 20 | 22 | ||
| 21 | @override | 23 | @override |
| 22 | - Widget build(BuildContext context) { | ||
| 23 | - List<InlineSpan> list = MarkdownComponent.generate( | ||
| 24 | - context, | ||
| 25 | - exp, | ||
| 26 | - // .replaceAllMapped( | ||
| 27 | - // RegExp( | ||
| 28 | - // r"\\\[(.*?)\\\]|(\\begin.*?\\end{.*?})", | ||
| 29 | - // multiLine: true, | ||
| 30 | - // dotAll: true, | ||
| 31 | - // ), (match) { | ||
| 32 | - // // | ||
| 33 | - // String body = (match[1] ?? match[2])?.replaceAll("\n", " ") ?? ""; | ||
| 34 | - // return "\\[$body\\]"; | ||
| 35 | - // }), | ||
| 36 | - config, | ||
| 37 | - includeGlobalComponents, | 24 | + State<MdWidget> createState() => _MdWidgetState(); |
| 25 | +} | ||
| 26 | + | ||
| 27 | +class _MdWidgetState extends State<MdWidget> { | ||
| 28 | + List<InlineSpan> list = []; | ||
| 29 | + @override | ||
| 30 | + void initState() { | ||
| 31 | + super.initState(); | ||
| 32 | + list = MarkdownComponent.generate( | ||
| 33 | + widget.context, | ||
| 34 | + widget.exp, | ||
| 35 | + widget.config, | ||
| 36 | + widget.includeGlobalComponents, | ||
| 38 | ); | 37 | ); |
| 39 | - return config.getRich( | ||
| 40 | - TextSpan(children: list, style: config.style?.copyWith()), | 38 | + } |
| 39 | + | ||
| 40 | + @override | ||
| 41 | + void didUpdateWidget(covariant MdWidget oldWidget) { | ||
| 42 | + super.didUpdateWidget(oldWidget); | ||
| 43 | + if (oldWidget.exp != widget.exp || | ||
| 44 | + !oldWidget.config.isSame(widget.config)) { | ||
| 45 | + list = MarkdownComponent.generate( | ||
| 46 | + context, | ||
| 47 | + widget.exp, | ||
| 48 | + widget.config, | ||
| 49 | + widget.includeGlobalComponents, | ||
| 50 | + ); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + @override | ||
| 55 | + Widget build(BuildContext context) { | ||
| 56 | + // List<InlineSpan> list = MarkdownComponent.generate( | ||
| 57 | + // context, | ||
| 58 | + // widget.exp, | ||
| 59 | + // widget.config, | ||
| 60 | + // widget.includeGlobalComponents, | ||
| 61 | + // ); | ||
| 62 | + return widget.config.getRich( | ||
| 63 | + TextSpan(children: list, style: widget.config.style?.copyWith()), | ||
| 41 | ); | 64 | ); |
| 42 | } | 65 | } |
| 43 | } | 66 | } |
| 1 | name: gpt_markdown | 1 | name: gpt_markdown |
| 2 | description: "Powerful Markdown & LaTeX Renderer for Flutter: Rich Text, Math, Tables, Links, and Text Selection. Ideal for ChatGPT, Gemini, and more." | 2 | description: "Powerful Markdown & LaTeX Renderer for Flutter: Rich Text, Math, Tables, Links, and Text Selection. Ideal for ChatGPT, Gemini, and more." |
| 3 | -version: 1.0.18 | 3 | +version: 1.0.19 |
| 4 | homepage: https://github.com/Infinitix-LLC/gpt_markdown | 4 | homepage: https://github.com/Infinitix-LLC/gpt_markdown |
| 5 | 5 | ||
| 6 | environment: | 6 | environment: |
-
Please register or login to post a comment