html-editor-enhanced icon indicating copy to clipboard operation
html-editor-enhanced copied to clipboard

[QUESTION] How to handle focus?

Open fynn-lemburg opened this issue 4 years ago • 23 comments

Hi, I have a question regarding the focus of the editor. I couldn't figure out how to handle it, especially how to request or unfocus it programmaticaly. I tried controller.setFocus() and controller.clearFocus() but both did not seem to work. Another question: Is it possible to define the keyboard type ?

fynn-lemburg avatar May 20 '21 14:05 fynn-lemburg

Controller unfocus should work with controller.clearFocus()... on my end it removes the cursor, removes the keyboard, and resets the height.

However, if you are trying to create an implementation where you can, for example, jump from a TextField to the editor to another TextField by pressing the enter key on the keyboard, this is not possible. Unfortunately this is a limitation of Flutter - technically controller.setFocus() is focusing the editor. You can see this because the onFocus callback fires. Flutter doesn't see this though, so the keyboard and cursor do not show. As a result you can't make a form style page where the FocusNode can jump between native components and webview components.

With time hopefully this can be sorted out but as of right now it is still an issue.

A quick search reveals this issue on flutter_inappwebview - https://github.com/pichillilorenzo/flutter_inappwebview/issues/744 - unfortunately it was closed by OP without any further details...

tneotia avatar May 22 '21 17:05 tneotia

yeah, I was quite frustrated with this too. In my case, I've a TextFormField and a HTML editor together in my widget. There are three issues I've seen so far,

  1. Tab button press doesn't jump to the html editor.
  2. The onscreen keyboard pops up on and off couple of times (in other words, editor focus is not stable).
  3. The editor doesn't take in physical keyboard input (can type only with the onscreen keyboard)

anamak9 avatar May 26 '21 13:05 anamak9

The onscreen keyboard pops up on and off couple of times (in other words, editor focus is not stable).

@anamak9 would you be able to provide device details and a video/reproducible example for the above?

The editor doesn't take in physical keyboard input (can type only with the onscreen keyboard)

Judging by this you are using the Simulator on macOS, this is a known issue with the simulator - the hardware keyboard doesn't work to type in any webview, not just this package. It works fine on a real iOS device or Safari afaik.

tneotia avatar May 26 '21 21:05 tneotia

@tneotia I may not able to produce a video for now but why don't you try to create a widget just like the one I did and run it on an emulator and see yourself (I had used an android emulator)?

anamak9 avatar May 27 '21 18:05 anamak9

@anamak9 finally got a chance to test on an emulator. I can't reproduce your 2nd and 3rd issues at all on the master branch of the plugin, Flutter beta, and the example app. Focus seems stable and hardware keyboard works great.

https://user-images.githubusercontent.com/50850142/121817722-0dbf7300-cc51-11eb-85f2-9cb457298755.mp4

tneotia avatar Jun 13 '21 18:06 tneotia

Is it possible to define the keyboard type ?

Added in v2.2.0, thanks for the suggestion!

(Use HtmlEditorOptions > inputType to use this feature).

tneotia avatar Jun 14 '21 22:06 tneotia

@tneotia - I will test it again and let you know.

anamak9 avatar Aug 26 '21 20:08 anamak9

Is there any progress on resolving this problem? I still have the same issue running the latest version of html_editor_enhanced: ^2.4.0+1 Here is the sample of my code. I have a TextField and BasicHtmlEditor which is a wrapper around your HtmlEditor widget.

final _htmlController = HtmlEditorController();
final _textFocusNode = FocusNode();
final _textController = TextEditingController();
bool _editAsHtml = false;

              IconButton(
                color: Theme.of(context).primaryColor,
                icon: Icon(MdiIcons.formatFont /*formatTextVariantOutline*/),
                onPressed: widget.chatGroup!.groupId == null || _isSending
                    ? null
                    : () {
                        setState(() {
                          _editAsHtml = !_editAsHtml;
                          Future.delayed(const Duration(milliseconds: 100), () {
                            if (_editAsHtml) {
                              _textController.clear();
                              _textFocusNode.unfocus();
                              _htmlController.setFocus();
                            } else {
                              // _htmlController.clear();
                              _htmlController.clearFocus();
                              _textFocusNode.requestFocus();
                            }
                          });
                        });
                      },
              ),
              Expanded(
                child: _editAsHtml
                    ? BasicHtmlEditor(
                        controller: _htmlController,
                        height: 120,
                        hint: 'Type your message here...',
                      )
: TextField(something here),
),

misici234 avatar Jan 31 '22 17:01 misici234

Unfortunately no, this is pretty much a Flutter limitation due to how it works with platform views (in this case webview).

tneotia avatar Feb 02 '22 00:02 tneotia

Same here. It is quite common to have, let's say, a TextField for Subject and the Html Editor for the Body of the message. Because of this, it's not possible to correctly move focus from the TextField to the Html Editor. Any hint?

guildenstern70 avatar Mar 28 '22 14:03 guildenstern70

When I click menu list options, focus will land on the edit box, cause I can't choose. How do I solve this problem

1963828747 avatar May 03 '22 02:05 1963828747

Any update on this? setFocus function is not working so it is not possible to autofocus the editor which is a big issue I think...

vukan-markovic avatar Jun 16 '22 08:06 vukan-markovic

Unfortunately no, this is still a Flutter limitation with platform views. I've messed with the setFocus on numerous occasions and I don't think that propagates outside the webview (ie it doesn't tell Flutter that an element in the webview is focused, and should open the keyboard)

tneotia avatar Jun 30 '22 01:06 tneotia

@tneotia have you reported this to the flutter_inappwebview team? Have you tried to use another webview plugin instead of flutter_inappwebview?

misici234 avatar Jun 30 '22 03:06 misici234

@tneotia Sorry I see that you did try. Seriously, this is what drives me nuts about Flutter. A group of good guys does something good and people, like us, start using it and the good guys give up. Flutter team doesn't care and we get left in a shit. Anyway, have you tried to use another webview plugin instead of flutter_inappwebview?

misici234 avatar Jun 30 '22 03:06 misici234

All webview plugins have the same issues, and by far flutter_inappwebview is better than the rest so I chose it. Until Flutter team improves platform view functionality, nothing can be done here (and it is a P4 issue so not high priority at this time)

tneotia avatar Jun 30 '22 15:06 tneotia

Just to let you know, a possible solution is to update the onFocus function in the editor like this. It works for the web, I did n t test for mobile:

onFocus: () { // To fix the bug that leave the focus to another text field entering the editor, when we click on the editor // we clear first the focus on other component, then we have to put the focus again in the editor FocusScope.of(context).requestFocus(FocusNode()); controller.setFocus(); },

jdayssol avatar Dec 01 '22 14:12 jdayssol

Screenshot 2023-01-12 at 23 48 08

When popup a widget or drawer I cannot click any button which is on html_editor_enhanced.Is there any solutuion?

ozguraslanCE avatar Jan 12 '23 20:01 ozguraslanCE

Yes I have also the problem that when I have a alertDialog that overlap the editor, the mouse still change to a cursor and when I click on my dialog the focus go to my editor that is behind it.

jdayssol avatar Jan 18 '23 13:01 jdayssol

If you need to dismiss the keyboard try this:

      var result = await _htmlController.editorController
          ?.evaluateJavascript(source: "document.activeElement.blur();");

jamesey2001 avatar Sep 04 '23 10:09 jamesey2001