Skip to content

Commit f0414be

Browse files
committed
added reset and clear buttons
1 parent 3498ffa commit f0414be

File tree

8 files changed

+68
-17
lines changed

8 files changed

+68
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.12 - 30-June-2022
2+
* Added clear and reset buttons in toolbar
3+
* Added readOnly option in Split Markdown Field
4+
15
## 0.2.11+1 - 10-June-2022
26
* Minor fixes
37

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class _HomeScreenState extends State<HomeScreen> {
4444
// expands: true,
4545
),
4646
SplittedMarkdownFormField(
47+
markdownSyntax: '## Headline',
4748
decoration: InputDecoration(
4849
hintText: 'Splitted Markdown FormField',
4950
),

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ packages:
115115
path: ".."
116116
relative: true
117117
source: path
118-
version: "0.2.11+1"
118+
version: "0.2.12"
119119
matcher:
120120
dependency: transitive
121121
description:

lib/widgets/markdown_auto_preview.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MarkdownAutoPreview extends StatefulWidget {
1616
this.expandableBackground,
1717
this.maxLines,
1818
this.minLines,
19+
this.markdownSyntax,
1920
this.emojiConvert = false,
2021
this.enableToolBar = true,
2122
this.showEmojiSelection = true,
@@ -26,6 +27,13 @@ class MarkdownAutoPreview extends StatefulWidget {
2627
this.decoration = const InputDecoration(isDense: true),
2728
}) : super(key: key);
2829

30+
/// Markdown syntax to reset the field to
31+
///
32+
/// ## Headline
33+
/// - some text here
34+
///
35+
final String? markdownSyntax;
36+
2937
/// For enable toolbar options
3038
///
3139
/// if false, toolbar widget will not display
@@ -192,8 +200,6 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
192200
return FocusScope(
193201
debugLabel: 'Markdown-Form-Field-FocusNode',
194202
onFocusChange: (focus) {
195-
// print('Focus Changed: $focus');
196-
197203
setState(() {
198204
_focused = focus;
199205
});
@@ -219,7 +225,7 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
219225
child: MarkdownBody(
220226
key: const ValueKey<String>("zmarkdown-parse-body"),
221227
data: _internalController.text == ""
222-
? "Type here. . ."
228+
? "_Markdown text_"
223229
: _internalController.text,
224230
),
225231
),
@@ -239,25 +245,20 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
239245
// show toolbar
240246
if (!widget.readOnly)
241247
MarkdownToolbar(
248+
markdownSyntax: widget.markdownSyntax,
242249
// key: const ValueKey<String>("zmarkdowntoolbar"),
243250
controller: _internalController,
244251
autoCloseAfterSelectEmoji: widget.autoCloseAfterSelectEmoji,
245252
bringEditorToFocus: () {
246-
// print('bringEditorToFocus');
247-
248253
if (!_textFieldFocusNode.hasFocus) {
249254
setState(() {
250255
_focused = true;
251256
});
252257

253-
// print('Brought Editor to focus');
254-
255258
_textFieldFocusNode.requestFocus();
256259
}
257260
},
258261
onPreviewChanged: () {
259-
// print('onPreviewChanged');
260-
261262
// Remove focus first
262263
_internalFocus.unfocus();
263264

lib/widgets/markdown_field.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ class MarkdownField extends StatelessWidget {
122122
return Padding(
123123
padding: padding,
124124
child: TextField(
125-
key: key,
126-
// const ValueKey<String>("markdown_editor_plus"),
125+
// key: const ValueKey<String>("markdown_editor_plus"),
127126
maxLines: expands ? null : maxLines,
128127
minLines: expands ? null : minLines,
129128
expands: expands,

lib/widgets/markdown_toolbar.dart

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ class MarkdownToolbar extends StatelessWidget {
1919
final bool showPreviewButton;
2020
final bool showEmojiSelection;
2121
final VoidCallback? onActionCompleted;
22+
final String? markdownSyntax;
2223

2324
MarkdownToolbar({
2425
Key? key,
2526
this.onPreviewChanged,
27+
this.markdownSyntax,
2628
required this.controller,
2729
this.emojiConvert = true,
2830
this.unfocus,
@@ -57,6 +59,31 @@ class MarkdownToolbar extends StatelessWidget {
5759
onPressedButton: onPreviewChanged,
5860
tooltip: 'Show/Hide markdown preview',
5961
),
62+
63+
// Clear the field
64+
ToolbarItem(
65+
key: const ValueKey<String>("toolbar_clear_action"),
66+
icon: FontAwesomeIcons.trashCan,
67+
onPressedButton: () {
68+
controller.clear();
69+
onActionCompleted?.call();
70+
},
71+
tooltip: 'Clear the text field',
72+
),
73+
74+
// Reset the text field
75+
ToolbarItem(
76+
key: const ValueKey<String>("toolbar_reset_action"),
77+
icon: FontAwesomeIcons.arrowRotateLeft,
78+
onPressedButton: () {
79+
if (markdownSyntax != null) {
80+
controller.text = markdownSyntax!;
81+
onActionCompleted?.call();
82+
}
83+
},
84+
tooltip: 'Reset the text field to specified format',
85+
),
86+
6087
// select single line
6188
ToolbarItem(
6289
key: const ValueKey<String>("toolbar_selection_action"),
@@ -154,7 +181,7 @@ class MarkdownToolbar extends StatelessWidget {
154181
ToolbarItem(
155182
key: const ValueKey<String>("checkbox"),
156183
icon: FontAwesomeIcons.solidSquareCheck,
157-
tooltip: 'Checked box',
184+
tooltip: 'Checked checkbox',
158185
onPressedButton: () {
159186
toolbar.action("- [x] ", "");
160187
onActionCompleted?.call();
@@ -163,7 +190,7 @@ class MarkdownToolbar extends StatelessWidget {
163190
ToolbarItem(
164191
key: const ValueKey<String>("uncheckbox"),
165192
icon: FontAwesomeIcons.square,
166-
tooltip: 'Unchecked box',
193+
tooltip: 'Unchecked checkbox',
167194
onPressedButton: () {
168195
toolbar.action("- [ ] ", "");
169196
onActionCompleted?.call();
@@ -175,7 +202,7 @@ class MarkdownToolbar extends StatelessWidget {
175202
if (showEmojiSelection)
176203
ToolbarItem(
177204
key: const ValueKey<String>("toolbar_emoji_action"),
178-
icon: FontAwesomeIcons.solidFaceSmile,
205+
icon: FontAwesomeIcons.faceSmile,
179206
tooltip: 'Select emoji',
180207
onPressedButton: () async {
181208
await _showModalSelectEmoji(context, controller.selection);

lib/widgets/splitted_markdown_form_field.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class SplittedMarkdownFormField extends StatefulWidget {
1919
this.validator,
2020
this.autovalidateMode,
2121
this.onSaved,
22+
this.markdownSyntax,
23+
this.readOnly = false,
2224
this.expands = false,
2325
this.emojiConvert = false,
2426
this.enableToolBar = true,
@@ -28,11 +30,25 @@ class SplittedMarkdownFormField extends StatefulWidget {
2830
this.decoration = const InputDecoration(isDense: true),
2931
}) : super(key: key);
3032

33+
/// Markdown syntax to reset the field to
34+
///
35+
/// ## Headline
36+
/// - some text here
37+
///
38+
final String? markdownSyntax;
39+
3140
/// For enable toolbar options
3241
///
3342
/// if false, toolbar widget will not display
3443
final bool enableToolBar;
3544

45+
/// Whether the text can be changed.
46+
///
47+
/// When this is set to true, the text cannot be modified by any shortcut or keyboard operation. The text is still selectable.
48+
///
49+
/// Defaults to false. Must not be null.
50+
final bool readOnly;
51+
3652
/// Enable Emoji options
3753
///
3854
/// if false, Emoji selection widget will not be displayed
@@ -207,6 +223,7 @@ class _SplittedMarkdownFormFieldState extends State<SplittedMarkdownFormField> {
207223
children: [
208224
Expanded(
209225
child: TextFormField(
226+
readOnly: widget.readOnly,
210227
controller: _internalController,
211228
cursorColor: widget.cursorColor,
212229
focusNode: _focusNode,
@@ -236,8 +253,9 @@ class _SplittedMarkdownFormFieldState extends State<SplittedMarkdownFormField> {
236253
child: MarkdownBody(
237254
// key: const ValueKey<String>("zmarkdown-parse-body"),
238255
data: _internalController.text == ""
239-
? "**Text in markdown**"
256+
? "_Markdown text_"
240257
: _internalController.text,
258+
selectable: true,
241259
),
242260
),
243261
],
@@ -246,6 +264,7 @@ class _SplittedMarkdownFormFieldState extends State<SplittedMarkdownFormField> {
246264
// show toolbar
247265
if (widget.enableToolBar)
248266
MarkdownToolbar(
267+
markdownSyntax: widget.markdownSyntax,
249268
showPreviewButton: false,
250269
// key: const ValueKey<String>("zmarkdowntoolbar"),
251270
controller: _internalController,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: markdown_editor_plus
22
description: A TextField Widget that allow you to convert easily what's in the
33
TextField to Markdown with custom toolbar support.
44

5-
version: 0.2.11+1
5+
version: 0.2.12
66
homepage: https://github.com/OmkarDabade/markdown_editor_plus
77
repository: https://github.com/OmkarDabade/markdown_editor_plus
88

0 commit comments

Comments
 (0)