xbr_x5_webview 1.0.3 copy "xbr_x5_webview: ^1.0.3" to clipboard
xbr_x5_webview: ^1.0.3 copied to clipboard

A Flutter plugin that provides a WebView widget on Android and iOS which Android's core is tencent x5.

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';

// import 'package:permission_handler/permission_handler.dart';
import 'package:webview_flutter_example/confirm_dialog.dart';
import 'package:webview_flutter_example/permission_util.dart';
import 'package:xbr_x5_webview/webview_flutter.dart';

import 'restart_widget.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    RestartWidget(
      //使用StatefulWidget包装下
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Webview demp',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter X5'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({this.title = ''});

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late FocusNode _focusNode;
  ChooseFileMode chooseFileMode = ChooseFileMode.auto;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ListView(
          children: <Widget>[
            ElevatedButton(
              child: const Text('初始化及请求权限'),
              onPressed: () {
                WebviewFlutterX5.initX5(needPermissionCallback: (_){
                  PermissionUtil.requestPermissions(context, (success, msg){
                    if(success==false) {
                      Fluttertoast.showToast(msg: msg);
                    } else {
                      Fluttertoast.showToast(msg: "权限请求完成");
                    }
                  });
                });
              },
            ),
            ElevatedButton(
              child: const Text('手动安装'),
              onPressed: () async {
                ConfirmDialog.show(context, title: "是否使用X5内核", text: "X5内核未安装,是否手动安装X5内核", surePass: () async {
                  Directory directory = await getTemporaryDirectory();
                  String path = "${directory.path}/46141_x5.apk";
                  Response response = await HttpService.downloadFile("http://117.187.230.142:9000/app/46141_x5.apk", path, downloadProgressCallBack: (i, ii) {
                    Fluttertoast.showToast(msg: "$i/$ii");
                  }); //46141
                  if (response.statusCode == 200) {
                    bool success = await WebviewFlutterX5.installX5(coreId: 46141, path: path);
                    if (success) {
                      ConfirmDialog.show(context, title: "X5内核安装成功", text: "X5内核安装成功!重启应用后生效",);
                    }
                  }
                });
              },
            ),
            ElevatedButton(
              child: const Text('x5内核调试器'),
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (_) {
                  return Scaffold(
                    appBar: AppBar(
                      title: const Text('x5内核调试器'),
                    ),
                    body: SafeArea(child: WebviewPage('http://debugtbs.qq.com/')),
                  );
                }));
              },
            ),
            ElevatedButton(
              child: const Text('VUE测试页'),
              onPressed: () async {
                if (_focusNode.hasFocus) {
                  _focusNode.unfocus();
                }
                String url2 = 'http://117.187.230.142:5173/';
                await Navigator.push(context, MaterialPageRoute(builder: (_) {
                  return Scaffold(
                    appBar: AppBar(
                      title: const Text('VUE测试页'),
                    ),
                    body: SafeArea(child: WebviewPage(url2)),
                  );
                }));
              },
            ),
            ElevatedButton(
              child: const Text('x5内核版本'),
              onPressed: () async {
                Fluttertoast.showToast(msg: "x5内核版本:${await WebviewFlutterX5.tbsVersion()}");
              },
            ),
            ElevatedButton(
              child: const Text('x5内核SDK版本'),
              onPressed: () async {
                Fluttertoast.showToast(msg: "x5内核版本:${ await WebviewFlutterX5.sdkVersion()}");
              },
            ),
            ElevatedButton(
              child: const Text('打开视频'),
              onPressed: () {

              },
            ),
            ElevatedButton(
              child: const Text('打开文件'),
              onPressed: () {

              },
            ),
          ],
        ),
      ),
    );
  }

  List<PopupMenuEntry<ChooseFileMode>> getWidgets() {
    List<ChooseFileMode> strategyList = ChooseFileMode.values;
    List<PopupMenuEntry<ChooseFileMode>> widgets = [];
    for (int i = 0; i < strategyList.length; i++) {
      var ele = strategyList[i];
      widgets.add(PopupMenuItem<ChooseFileMode>(
          value: ele,
          child: SizedBox(
            // color: Colors.green,
            width: 180,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  '$ele',
                  style: const TextStyle(fontSize: 14.0),
                ),
                if (ele == chooseFileMode)
                  const Icon(
                    Icons.favorite,
                    size: 16.0,
                    color: Colors.blue,
                  )
                else
                  Container(),
              ],
            ),
          )));
      widgets.add(const PopupMenuDivider());
    }
    return widgets;
  }
}

///下载文件到本地
///urlPath 文件Url
///savePath 本地保存位置
///downloadProgressCallBack 下载文件回调
class HttpService {
  static Future<Response> downloadFile(String urlPath, String savePath, {DownloadProgressCallBack? downloadProgressCallBack}) async {
    Dio dio = Dio();
    return await dio.download(urlPath, savePath, onReceiveProgress: downloadProgressCallBack);
  }
}

/// count 当前下载进度
/// total 下载总长度
typedef DownloadProgressCallBack = Function(int count, int total);

class WebviewPage extends StatefulWidget {
  WebviewPage(this.url);

  final String url;

  @override
  _WebviewPageState createState() => _WebviewPageState();
}

class _WebviewPageState extends State<WebviewPage> {
  final Completer<WebViewController> _completer = Completer<WebViewController>();
  bool loading = true;

  @override
  void initState() {
    super.initState();
    // Enable hybrid composition.
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        WebView(
          initialUrl: widget.url,
          onWebViewCreated: _completer.complete,
          javascriptMode: JavascriptMode.unrestricted,
          onPageStarted: (url) {
            print('web view page test: start url:$url');
          },
          onPageFinished: (url) {
            print('web view page test: finish url:$url');
            setState(() {
              loading = false;
            });
          },
          navigationDelegate: (request) {
            print('web view page test: url:${widget.url}');
            // if (request.url.startsWith('http://')) {
            //   request.url.replaceFirst('http://', 'https://');
            // }
            print('web view page test: url:${widget.url}');
            return NavigationDecision.navigate;
          },
        ),
        loading ? Center(child: CircularProgressIndicator()) : Container(),
      ],
    );
  }
}
0
likes
55
points
13
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin that provides a WebView widget on Android and iOS which Android's core is tencent x5.

Documentation

API reference

License

MulanPSL-2.0 (license)

Dependencies

flutter

More

Packages that depend on xbr_x5_webview

Packages that implement xbr_x5_webview