Skip to content

Commit b1d44be

Browse files
author
Alexander Petermann
committed
feature: allow to set initialText on MarkdownAutoPreview without providing a controller.
also, copy over the documentation from MarkdownAutoPreview to MarkdownField
1 parent 7dc8bc6 commit b1d44be

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

lib/widgets/markdown_auto_preview.dart

+13-12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class MarkdownAutoPreview extends StatefulWidget {
3030
this.expands = false,
3131
this.decoration = const InputDecoration(isDense: true),
3232
this.hintText,
33+
this.initialText,
3334
});
3435

3536
/// Markdown syntax to reset the field to
@@ -181,6 +182,11 @@ class MarkdownAutoPreview extends StatefulWidget {
181182
/// Defaults to false.
182183
final bool expands;
183184

185+
/// [initialText] used to initialize the internal [controller] text.
186+
///
187+
/// only one, the [initialText] OR the [controller] can be set, not both.
188+
final String? initialText;
189+
184190
@override
185191
State<MarkdownAutoPreview> createState() => _MarkdownAutoPreviewState();
186192
}
@@ -189,18 +195,17 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
189195
// Internal parameter
190196
late TextEditingController _internalController;
191197

192-
final FocusScopeNode _internalFocus =
193-
FocusScopeNode(debugLabel: '_internalFocus');
194-
final FocusNode _textFieldFocusNode =
195-
FocusNode(debugLabel: '_textFieldFocusNode');
198+
final FocusScopeNode _internalFocus = FocusScopeNode(debugLabel: '_internalFocus');
199+
final FocusNode _textFieldFocusNode = FocusNode(debugLabel: '_textFieldFocusNode');
196200

197201
late Toolbar _toolbar;
198202

199203
bool _focused = false;
200204

201205
@override
202206
void initState() {
203-
_internalController = widget.controller ?? TextEditingController();
207+
assert((widget.controller != null) ^ (widget.initialText != null));
208+
_internalController = widget.controller ?? TextEditingController(text: widget.initialText);
204209

205210
_toolbar = Toolbar(
206211
controller: _internalController,
@@ -222,10 +227,8 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
222227
Widget build(BuildContext context) {
223228
return FocusableActionDetector(
224229
shortcuts: {
225-
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyB):
226-
BoldTextIntent(),
227-
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyI):
228-
ItalicTextIntent(),
230+
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyB): BoldTextIntent(),
231+
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyI): ItalicTextIntent(),
229232
},
230233
actions: {
231234
BoldTextIntent: CallbackAction<BoldTextIntent>(
@@ -273,9 +276,7 @@ class _MarkdownAutoPreviewState extends State<MarkdownAutoPreview> {
273276
alignment: Alignment.centerLeft,
274277
child: MarkdownBody(
275278
key: const ValueKey<String>("zmarkdown-parse-body"),
276-
data: _internalController.text == ""
277-
? widget.hintText ?? "_Markdown text_"
278-
: _internalController.text,
279+
data: _internalController.text == "" ? widget.hintText ?? "_Markdown text_" : _internalController.text,
279280
),
280281
),
281282
),

lib/widgets/markdown_field.dart

+29
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,37 @@ class MarkdownField extends StatelessWidget {
112112
/// Add label, hint etc
113113
final InputDecoration decoration;
114114

115+
/// The maximum number of lines to show at one time, wrapping if necessary.
116+
///
117+
/// This affects the height of the field itself and does not limit the number of lines that can be entered into the field.
118+
///
119+
/// If this is 1 (the default), the text will not wrap, but will scroll horizontally instead.
120+
///
121+
/// If this is null, there is no limit to the number of lines, and the text container will start with enough vertical space for one line and automatically grow to accommodate additional lines as they are entered, up to the height of its constraints.
122+
///
123+
/// If this is not null, the value must be greater than zero, and it will lock the input to the given number of lines and take up enough horizontal space to accommodate that number of lines. Setting [minLines] as well allows the input to grow and shrink between the indicated range.
115124
final int? maxLines;
125+
126+
/// The minimum number of lines to occupy when the content spans fewer lines.
127+
///
128+
/// This affects the height of the field itself and does not limit the number of lines that can be entered into the field.
129+
///
130+
/// If this is null (default), text container starts with enough vertical space for one line and grows to accommodate additional lines as they are entered.
131+
///
132+
/// This can be used in combination with [maxLines] for a varying set of behaviors.
133+
///
134+
/// If the value is set, it must be greater than zero. If the value is greater than 1, [maxLines] should also be set to either null or greater than this value.
135+
///
136+
/// When [maxLines] is set as well, the height will grow between the indicated range of lines. When [maxLines] is null, it will grow as high as needed, starting from [minLines].
116137
final int? minLines;
138+
139+
/// Whether this widget's height will be sized to fill its parent.
140+
///
141+
/// If set to true and wrapped in a parent widget like [Expanded] or [SizedBox], the input will expand to fill the parent.
142+
///
143+
/// [maxLines] and [minLines] must both be null when this is set to true, otherwise an error is thrown.
144+
///
145+
/// Defaults to false.
117146
final bool expands;
118147

119148
@override

0 commit comments

Comments
 (0)