Skip to content

Commit

Permalink
Added option for external focus node in markdown_text_input.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiennM authored Oct 13, 2023
2 parents f239f43 + 8a1e823 commit 5f7d8fa
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/markdown_text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class MarkdownTextInput extends StatefulWidget {
/// Default value is true.
final bool insertLinksByDialog;

///Optional focusNode, the Widget creates it's own if not provided
final FocusNode? focusNode;

/// If you prefer to use the dialog to insert image, you can choose to use the markdown syntax directly by setting [insertImageByDialog] to false. In this case, the selected text will be used as label and link.
/// Default value is true.
final bool insertImageByDialog;
Expand Down Expand Up @@ -76,6 +79,7 @@ class MarkdownTextInput extends StatefulWidget {
this.controller,
this.insertLinksByDialog = true,
this.insertImageByDialog = true,
this.focusNode,
this.linkDialogLinkDecoration,
this.linkDialogTextDecoration,
this.imageDialogLinkDecoration,
Expand All @@ -84,14 +88,15 @@ class MarkdownTextInput extends StatefulWidget {
this.customSubmitDialogText,
this.optionnalActionButtons = const []});


@override
_MarkdownTextInputState createState() => _MarkdownTextInputState(controller ?? TextEditingController());
}

class _MarkdownTextInputState extends State<MarkdownTextInput> {
final TextEditingController _controller;
TextSelection textSelection = const TextSelection(baseOffset: 0, extentOffset: 0);
FocusNode focusNode = FocusNode();
late final FocusNode focusNode;

_MarkdownTextInputState(this._controller);

Expand All @@ -115,6 +120,7 @@ class _MarkdownTextInputState extends State<MarkdownTextInput> {

@override
void initState() {
focusNode = widget.focusNode ?? FocusNode();
_controller.text = widget.initialValue;
_controller.addListener(() {
if (_controller.selection.baseOffset != -1) textSelection = _controller.selection;
Expand All @@ -126,7 +132,7 @@ class _MarkdownTextInputState extends State<MarkdownTextInput> {
@override
void dispose() {
if (widget.controller == null) _controller.dispose();
focusNode.dispose();
if (widget.focusNode == null) focusNode.dispose();
super.dispose();
}

Expand Down

0 comments on commit 5f7d8fa

Please sign in to comment.