当前位置:网站首页>Flutter WebView示例

Flutter WebView示例

2022-07-04 20:35:00 海阔天空6688

加载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,
		   ],
		),
	);
}

加载html字符串

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

版权声明
本文为[海阔天空6688]所创,转载请带上原文链接,感谢
https://haikuotiankong.blog.csdn.net/article/details/125598256