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

How to request focus for the webview #1974

Open
jswirbs opened this issue Jan 12, 2024 · 10 comments
Open

How to request focus for the webview #1974

jswirbs opened this issue Jan 12, 2024 · 10 comments

Comments

@jswirbs
Copy link

jswirbs commented Jan 12, 2024

Description

What you'd like to happen:

I would like to be able to request focus for the webview such that a window.focus event is triggered within it. I would like my webview to be focused on initial load and to be able to give it focus programmatically in certain cases (like when another overlaying widget closes) and it seems the only way to do this currently is via user gestures (i.e. a user clicking on the webview). My webview needs window focus in these cases as I have logic within the website I'm loading in it that depends on focus.

There is a clearFocus() method on the InAppWebViewController class, which will clear focus and trigger a window blur event. A requestFocus() method which does the opposite could be implemented similarly, using requestFocus() for android implementation and becomeFirstResponder() for ios implementation. I'm not sure if this is the best approach though or if there is a deeper issue here that should be addressed on the flutter side or something else. I forked the flutter_inappwebview repo and tested a requestFocus() impl as mentioned above, which works, but curious what the correct solution is for this problem.

Alternatives you've considered:

I've tried using flutter focus, and while platform views participate in the flutter focus tree, there is no way to set focus or otherwise interact with the view's focus node (as is the case with other focusable widgets). And if I were to wrap my webview widget in a focus node and control focus at that level, it would not propagate down to the webview and trigger a window focus event, as there is no connection there.

I've tried faking a click on the webview via gestures without any luck (maybe I'm missing something there, but regardless this is still a hack).

The javascript code within the website cannot give itself focus or do anything focus related while the window does not have focus.

Copy link

👋 @jswirbs

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

@jswirbs
Copy link
Author

jswirbs commented Jan 12, 2024

This issue/question has been brought up before, for reference, but with no follow up.

#744

@yamaha252
Copy link
Contributor

Same here. Just needed webview to be in focus after initialisation to have tv remote d-pad navigation available.

@pichillilorenzo
Copy link
Owner

Probably using those methods on native side should be ok.
I will take a look at this as soon as possible, thanks.

@jlcool
Copy link

jlcool commented Apr 29, 2024

Probably using those methods on native side should be ok. I will take a look at this as soon as possible, thanks.

Have there been any updates?

@daviddt
Copy link

daviddt commented Jun 4, 2024

Would love to have this implement too

@yamaha252
Copy link
Contributor

I've tested the implementation as @jswirbs suggested.

requestFocus() for android implementation and becomeFirstResponder() for ios

It works fine as expected

@Antc1993
Copy link

Hello, is there any solution to this problem?

@h3nr1ke
Copy link

h3nr1ke commented Jun 30, 2024

Hey all

just to comment that I found a "not so elegant" workaround to get the focus working for android devices

Stack(
  children: [
    // ADD A TEXT FIELD WITH TRANSPARENT COLOR FOR THE VISIBLE ITEMS
    // THIS SHOULD BE ADDED ONLY FOR ANDROID APPs IF YOU IMPLEMENTED THE 
    // REQUESTFOCUS FUNCTION AVAILABLE HERE
    // https://github.com/pichillilorenzo/flutter_inappwebview/pull/2168
    Platform.isAndroid ? 
      const TextField(
        autofocus: true,
        decoration: InputDecoration(
          hintText: '',
          hintStyle: TextStyle(color: Colors.transparent),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.transparent),
          ),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.transparent),
          ),
        ),
        cursorColor: Colors.transparent,
        style: TextStyle(color: Colors.transparent),
      )
      : const SizedBox.shrink(),
    InAppWebView(
      key: webViewKey,
      initialUrlRequest: URLRequest(
        headers: initialHeaders,
        url: WebUri(widget.loadingUrl),
      ),
      initialSettings: configs.getOptions(),

      onLoadStop: (controller, url) async {
        // this method is still not in the main code
        // please take a look here
        // https://github.com/pichillilorenzo/flutter_inappwebview/pull/2168
        await webViewController.requestFocus();

        // request focus for text element inside the page
        // in this example, search-box-field is the id of a input element
        // It was added a timeout just in case, but you can try with or without 
        // to see what happen in your app
        await webViewController.evaluateJavascript(
          source:'''
            setTimeout(()=>{
              console.log("timeout do search-box-field focus"); 
              document.getElementById("search-box-field").focus();},
              200
            );
          ''',
        );
      },
    ),
  ]
);

The main idea is to get the focus in the widget and open the keyboard

Once the keyboard is visible we asked the focus to the webview, and it works

the tradeoff is that the keyboard opens very fast so it looks like a delay, but it works for Android devices.

Please take in consideration that this is using the new requestFocus implementation that is not yet in the main branch

please take a look here

#2168

@Manguelo
Copy link

Manguelo commented Jan 9, 2025

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants