Home > Software design >  How can I load WebView on Dialog?
How can I load WebView on Dialog?

Time:08-12

I want to pop-up a web browser by webview-flutter when I tabbed a button, So I made some code which does not work and donot know why.

Widget customCardCategory(BuildContext context, String url)
{
  String title = 'test';
  void Function()? _showWeb(String title, String url)
  {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius:
                  BorderRadius.circular(10.0)), //this right here

              child:
              Container(height: MediaQuery.of(context).size.height*0.6,
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  child:
                  Column(
                    children: [
                      Padding(padding: EdgeInsets.symmetric(vertical: 5)),
                      Text(title),
                      WebView(
                        initialUrl: url,
                        javascriptMode: JavascriptMode.unrestricted,
                      ),
                      Text('end of popup'),
                    ],
                  )

              )

          );

        });
  }

...
return GestureDetector(
                          onTap: (){
                            print(list_ref_right[index].toString() ' has tabbed.');
                          },
                          child:ref_box(context,list_ref_right[index]),
                        );
} 

I shortened redundant codes.

When running my code, The popup only shows the text 'test' and shows error :

E/flutter ( 3274): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter ( 3274): #0      WebSettingsHostApi.dispose (package:webview_flutter_android/src/android_webview.pigeon.dart:892:7)
E/flutter ( 3274): <asynchronous suspension>
E/flutter ( 3274): 
E/flutter ( 3274): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter ( 3274): #0      WebViewHostApi.dispose (package:webview_flutter_android/src/android_webview.pigeon.dart:197:7)
E/flutter ( 3274): <asynchronous suspension>
E/flutter ( 3274): #1      WebViewHostApiImpl.disposeFromInstance (package:webview_flutter_android/src/android_webview_api_impls.dart:108:7)
E/flutter ( 3274): <asynchronous suspension>
E/flutter ( 3274): 

Is WebView() unable to load inside Widget ? I saw many examples that calls WebView() directly in class but never saw one calls in Widget. Somebody please tell me what is wrong.

CodePudding user response:

did you add this part from its documentation to your code:

 void initState() {
 super.initState();
 // Enable virtual display.
 if (Platform.isAndroid) WebView.platform = AndroidWebView();

}

  • Related