Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions lib/widgets/markdown_auto_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -215,6 +228,7 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
},
);

_focused = widget.shouldInitTextField;
super.initState();
}

Expand Down Expand Up @@ -271,12 +285,18 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
},
child: Align(
alignment: Alignment.centerLeft,
child: MarkdownBody(
key: const ValueKey<String>("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<String>("zmarkdown-parse-body"),
data: data,
);
}),
),
),
);
Expand Down Expand Up @@ -321,6 +341,9 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
}

Widget _editor() {
if (widget.textFieldBuilder != null) {
return widget.textFieldBuilder!(_internalController, _textFieldFocusNode);
}
return TextField(
controller: _internalController,
focusNode: _textFieldFocusNode,
Expand Down