DEV Community

BC
BC

Posted on

Fix: physical keyboard can't type in Flutter's Webview

Problem

We are using a webview for our app built with Flutter (version 2.2.3). The main part code like this:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Keyboard Test')),
        body: WebView(
          initialUrl: 'https://duckduckgo.com',
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

When our iOS device connects to a bluetooth physical keyboard and open the app, inside the input box, the keyboard doesn't work, can't type anything.

Solution

This seems a Flutter bug (view the discussion here), and it looks like it has been fixed in the master branch, but hasn't been released to stable. At the time of writing this, the latest Flutter stable version is 2.2.3, so the later version might fix this issue.

But if you want to fix it now w/o waiting for the new version release, here is a temporary solution:

Go to your project folder: "ios > Runner > AppDelegate.swift", then simply add these code to that file:

extension FlutterViewController {
    open override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        super.pressesBegan(presses, with: event)
    }

    open override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        super.pressesEnded(presses, with: event)
    }

    open override func pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        super.pressesCancelled(presses, with: event)
    }
}
Enter fullscreen mode Exit fullscreen mode

Recompile your app, the problem should be fixed.

Reference

Top comments (2)

Collapse
 
karfau profile image
Christian Bewernitz

Do you happen to know how this workaround/fix will impact the behavior once the fix will be available on a flutter stable release?
Does it need to be reverted?

Collapse
 
bitecode profile image
BC

Not sure, but when it is merged, we can test. My guess is we can remove the swift patch.