Flutter-浏览器

narakuyang
2021-09-15 / 0 评论 / 61 阅读 / 正在检测是否收录...

import 'package:flutter_inappwebview/flutter_inappwebview.dart';


class commonAppWebView extends StatefulWidget {
  final String url;
  final String? appbartitle;
  const commonAppWebView({Key? key, required this.url, this.appbartitle})
      : super(key: key);

  @override
  State<commonAppWebView> createState() => _commonAppWebViewState();
}

class _commonAppWebViewState extends State<commonAppWebView> {
  final GlobalKey webViewKey = GlobalKey();
  InAppWebViewController? webViewController;
  int httpcode = 200;
  double loadprogress = 0;
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
      ),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.appbartitle == null ? '' : '${widget.appbartitle}'),
        leading: IconButton(
          icon: Icon(Icons.navigate_before_rounded),
          onPressed: () {
            Get.back();
          },
        ),
        actions: [
          IconButton(
            onPressed: () {
              Get.back();
            },
            icon: Icon(Icons.close_rounded),
          )
        ],
      ),
      body: Stack(
        alignment: Alignment.topCenter,
        children: [
          httpcode == 200
              ? WillPopScope(
                  onWillPop: () async {
                    Future<bool> canGoBack = webViewController!.canGoBack();
                    canGoBack.then((value) {
                      if (value) {
                        webViewController!.goBack();
                      } else {
                        Get.back();
                      }
                    });
                    return false;
                  },
                  child: Padding(
                    padding: EdgeInsets.all(0),
                    child: InAppWebView(
                      key: webViewKey,
                      initialOptions: options,
                      onWebViewCreated: (controller) {
                        setState(() {
                          webViewController = controller;
                        });
                      },
                      initialUrlRequest:
                          URLRequest(url: Uri.parse('${widget.url}')),
                      onLoadError: (controller, url, code, message) {
                        setState(() {
                          httpcode = code;
                        });
                      },
                      onLoadHttpError:
                          (controller, url, statusCode, description) {},
                      onProgressChanged: (controller, progress) {
                        setState(() {
                          loadprogress = double.parse(progress.toString());
                        });
                      },
                      shouldOverrideUrlLoading:
                          (controller, navigationAction) async {
                        String url = navigationAction.request.url.toString();
                        if (![
                          // "http",
                          // "https",
                          "file",
                          "chrome",
                          "data",
                          "javascript",
                          "about",
                        ].contains(url)) {
                          launchUrlString(url,
                              mode: LaunchMode.externalApplication);
                          return NavigationActionPolicy.CANCEL;
                        }
                        return NavigationActionPolicy.ALLOW;
                      },
                    ),
                  ),
                )
              : Center(
                  child: GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      setState(() {
                        httpcode = 200;
                        loadprogress = 0;
                      });
                      webViewController!.loadUrl(
                          urlRequest: URLRequest(url: Uri.parse(widget.url)));
                    },
                    child: Image.asset(
                      'assets/images/1655122521085.png',
                      width: 180.w,
                      height: 180.h,
                    ),
                  ),
                ),
          Visibility(
            visible: loadprogress == 100 || httpcode != 200 ? false : true,
            child: LinearProgressIndicator(
              color: appColor.Primarythree,
              backgroundColor: appColor.Neutralgarydeep,
              minHeight: 2.h,
              value: loadprogress,
            ),
          ),
          Visibility(
            visible: loadprogress == 100 || httpcode != 200 ? false : true,
            child: Center(
              child: LoadingAnimationWidget.inkDrop(
                size: 20,
                color: appColor.Primarythree,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
0

评论

博主关闭了所有页面的评论