当前位置:网站首页>Shutter WebView example

Shutter WebView example

2022-07-04 21:32:00 Boundless 6688

load url

 final Completer<WebViewController> _controller = Completer<WebViewController>();
  int _progressValue = 0;

  @override
  void initState() {
    super.initState();
    // Enable hybrid composition.
    if (Device.isAndroid) {
      WebView.platform = SurfaceAndroidWebView();
    }
  }
 
@override
Widget build(BuildContext context) {
	return Scaffold(
		body: Stack(
		   children: [
		     WebView(
		       initialUrl: "https://www.baidu.com",
		       javascriptMode: JavascriptMode.unrestricted,
		       allowsInlineMediaPlayback: true,
		       onWebViewCreated: (WebViewController webViewController) {
		         _controller.complete(webViewController);
		       },
		       onProgress: (int progress) {
		         debugPrint('WebView is loading (progress : $progress%)');
		         setState(() {
		           _progressValue = progress;
		         });
		       },
		     ),
		     if (_progressValue != 100) LinearProgressIndicator(
		       value: _progressValue / 100,
		       backgroundColor: Colors.transparent,
		       minHeight: 2,
		     ) else Gaps.empty,
		   ],
		),
	);
}

load html character string

String html = """
   <!DOCTYPE html>
        <html>
        <head><meta name="viewport" content="width=device-width, initial-scale=1.0"></head>
          <body>
            <p>html String loading </p>
          </body>
        </html>
  """;
    return Scaffold(
      appBar: AppBar(title: Text(widget.messageItem.title)),
      body: WebView(
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController controller) {
          controller.loadHtmlString(html);
        },
      ),
    );
原网站

版权声明
本文为[Boundless 6688]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042034502128.html