Flutter_webview_plugin 与 Http协议

标签:

本文出自jvm123.com-java技术分享站:http://jvm123.com/2020/11/flutter-webview-plugin.html

引入flutter_webview_plugin 以来如下:

dependencies:
  flutter_webview_plugin: ^0.3.5

则直接使用 WebviewScaffold ,如下:

import 'package:flutter/services.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/widget/Utils.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class Browser extends StatelessWidget {

  final String title;
  final String url;
  final String shareUrl;

  final MethodChannel channel = const MethodChannel('flutter_share_me');

  Browser({Key key, @required this.title, @required this.url, this.shareUrl}):super(key: key);

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      appBar: AppBar(
        title: Text(title),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () {
              if(shareUrl == null || shareUrl == "") {
                shareImage(url);
              } else {
                shareImage(shareUrl);
              }
            },
          )
        ],
      ),
      url: url,
      withZoom: false,
      withLocalStorage: true,
      withJavascript: true,
      //本地缓存
      hidden: false,
      //默认状态隐藏
      initialChild: Container(
        color: Colors.white,
        child: Center(
          child: Text('Loading...'),
        ),
      ), //设置初始化界面

    );
  }

  shareImage(String url) async {
    dynamic result;
    try {
      result = await channel.invokeMethod('system', {"msg": url});
      Utils.showToast("分享成功!");
    } catch (e) {
      Utils.showToast("分享失败!");
    }
  }
}

flutter webview 打开 http 网页

默认情况下,android只支持webview打开https协议的网页,如果需要打开http网页,需要配置 android:usesCleartextTraffic=”true”,如下:

    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="jvm123博客"
        android:icon="@mipmap/logo"
        android:usesCleartextTraffic="true">
        <activity
            ......

发表评论