Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] How to handle focus? #47

Open
fynn-lemburg opened this issue May 20, 2021 · 20 comments
Open

[QUESTION] How to handle focus? #47

fynn-lemburg opened this issue May 20, 2021 · 20 comments
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@fynn-lemburg
Copy link

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 fynn-lemburg added the question Further information is requested label May 20, 2021
@tneotia
Copy link
Owner

tneotia commented May 22, 2021

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 - pichillilorenzo/flutter_inappwebview#744 - unfortunately it was closed by OP without any further details...

@anamak9
Copy link

anamak9 commented May 26, 2021

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)

@tneotia
Copy link
Owner

tneotia commented May 26, 2021

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.

@anamak9
Copy link

anamak9 commented May 27, 2021

@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)?

@tneotia
Copy link
Owner

tneotia commented Jun 13, 2021

@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.

Android.SR.-.html_editor_enhanced.mp4

@tneotia tneotia added enhancement New feature or request help wanted Extra attention is needed labels Jun 14, 2021
@tneotia
Copy link
Owner

tneotia commented Jun 14, 2021

Is it possible to define the keyboard type ?

Added in v2.2.0, thanks for the suggestion!

(Use HtmlEditorOptions > inputType to use this feature).

@anamak9
Copy link

anamak9 commented Aug 26, 2021

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

@misici234
Copy link

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),
),

@tneotia
Copy link
Owner

tneotia commented Feb 2, 2022

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

@guildenstern70
Copy link

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?

@1963828747
Copy link

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

@vukan-markovic
Copy link

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...

@tneotia
Copy link
Owner

tneotia commented Jun 30, 2022

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)

@misici234
Copy link

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

@misici234
Copy link

@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?

@tneotia
Copy link
Owner

tneotia commented Jun 30, 2022

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)

@jdayssol
Copy link

jdayssol commented Dec 1, 2022

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();
},

@ozguraslanCE
Copy link

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?

@jdayssol
Copy link

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.

@jamesey2001
Copy link

If you need to dismiss the keyboard try this:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

10 participants