Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preload a webview in flutter using the official webview_flutter plugin?

I'm developing an app in flutter, and i got stuck while trying to preload a page in webview. Basically, the app uses tabbar, with a few tabs with normal listview content in them, and 2 tabs that should have webview as content. Any idea how to preload the webviews and keep them alive in background while the app loads, so when the user swipes between tabs, and gets on the webview ones, the sites are already loaded and rendered so the UX is seamless. I have tried creating the WebView instance and assigning it to a variable using setstate on app load, but to no avail.

Any help would be appreciated.

like image 328
C. Zoe Avatar asked May 18 '20 21:05

C. Zoe


People also ask

Does Flutter support WebView?

Introduction. With the WebView Flutter plugin you can add a WebView widget to your Android or iOS Flutter app. On iOS the WebView widget is backed by a WKWebView, while on Android the WebView widget is backed by a WebView. The plugin can render Flutter widgets over the web view.


1 Answers

So what you can try doing is this:

Wrap the widgets you are calling in your TabBar with a PageView like shown here.

You can also ensure that the WebView stays alive after loading once by extending your stateful widget with AutomaticKeepAliveClientMixin. While using this ensure that you declare:

 bool get wantKeepAlive => true;

Edit: In order to preload all your pages when your app launches you can use Indexed Stack in the class your TabBar is declared. Wrap the body and call all the pages there like this:

  Widget build(BuildContext context) {
    return
     Scaffold(
      body:IndexedStack(
              index: _selectedIndex,
              children: _children,
            ),),}
like image 67
Simran Aswani Avatar answered Nov 15 '22 04:11

Simran Aswani