From 1b48a1ecf9575179875976790387763dad1ea022 Mon Sep 17 00:00:00 2001 From: BugsOverBugs Date: Wed, 11 Sep 2024 08:59:34 +0200 Subject: [PATCH] new markdownPreviewBuilder, textFieldBuilder and shouldInitTextField in MarkdownAutoPreview --- lib/widgets/markdown_auto_preview.dart | 33 ++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/widgets/markdown_auto_preview.dart b/lib/widgets/markdown_auto_preview.dart index 5b55634..d234730 100644 --- a/lib/widgets/markdown_auto_preview.dart +++ b/lib/widgets/markdown_auto_preview.dart @@ -30,6 +30,9 @@ class MarkdownAutoPreview extends StatefulWidget { this.expands = false, this.decoration = const InputDecoration(isDense: true), this.hintText, + this.markdownPreviewBuilder, + this.textFieldBuilder, + this.shouldInitTextField = false, }); /// Markdown syntax to reset the field to @@ -47,6 +50,16 @@ class MarkdownAutoPreview extends StatefulWidget { /// if false, toolbar widget will not display final bool enableToolBar; + /// Override the default markdown preview + final Widget Function(String data)? markdownPreviewBuilder; + + /// Override the default text field + final Widget Function(TextEditingController controller, FocusNode focusNode)? + textFieldBuilder; + + /// If true by default, the TextField is shown + final bool shouldInitTextField; + /// Enable Emoji options /// /// if false, Emoji selection widget will not be displayed @@ -215,6 +228,7 @@ class _MarkdownAutoPreviewState extends State { }, ); + _focused = widget.shouldInitTextField; super.initState(); } @@ -271,12 +285,18 @@ class _MarkdownAutoPreviewState extends State { }, child: Align( alignment: Alignment.centerLeft, - child: MarkdownBody( - key: const ValueKey("zmarkdown-parse-body"), - data: _internalController.text == "" + child: Builder(builder: (context) { + final data = _internalController.text == "" ? widget.hintText ?? "_Markdown text_" - : _internalController.text, - ), + : _internalController.text; + if (widget.markdownPreviewBuilder != null) { + return widget.markdownPreviewBuilder!(data); + } + return MarkdownBody( + key: const ValueKey("zmarkdown-parse-body"), + data: data, + ); + }), ), ), ); @@ -321,6 +341,9 @@ class _MarkdownAutoPreviewState extends State { } Widget _editor() { + if (widget.textFieldBuilder != null) { + return widget.textFieldBuilder!(_internalController, _textFieldFocusNode); + } return TextField( controller: _internalController, focusNode: _textFieldFocusNode,