123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:webview_flutter/webview_flutter.dart';
- class WebviewPage extends StatefulWidget {
- final String url;
- final bool? isDialog;
- final bool? isFile;
- const WebviewPage({Key? key, required this.url, this.isDialog, this.isFile})
- : super(key: key);
- @override
- _WebviewPageState createState() => _WebviewPageState();
- }
- class _WebviewPageState extends State<WebviewPage> {
- String _title = '...';
- double progress = 0.0;
- bool isPayFlow = false;
- late WebViewController _webViewController;
- late Function payCallback;
- late Function jumpCallback;
- void jumpFunction() {
- Map<String, String> params = {};
- // 点击不显示弹窗
- params.remove('action');
- Get.back();
- }
- @override
- void initState() {
- super.initState();
- final WebViewController controller = WebViewController();
- var userAgent =
- "Mozilla/5.0 (Linuxxxx; Android 10; Redmi K30 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Mobile Safari/537.36";
- controller.setUserAgent(userAgent);
- var loadUrl = Uri.parse(widget.url);
- controller
- ..setJavaScriptMode(JavaScriptMode.unrestricted)
- ..setBackgroundColor((widget.isDialog ?? false)
- ? Colors.transparent
- : const Color(0x00000000))
- ..setNavigationDelegate(
- NavigationDelegate(
- onProgress: (value) {
- progress = value.toDouble();
- debugPrint("loadUrl progress:$progress");
- try {
- setState(() => {});
- } catch (e) {}
- },
- onPageStarted: (String url) {
- debugPrint("loadUrl pageStart:$url");
- if (url.startsWith("mailto") ||
- url.startsWith("tel") ||
- url.startsWith("http") ||
- url.startsWith("file") ||
- url.startsWith("https")) {
- } else {
- _webViewController.goBack();
- }
- },
- onPageFinished: (controller) async {
- // 获取页面标题
- var title = await _webViewController.getTitle();
- setState(() {
- _title = title ?? "";
- });
- },
- onWebResourceError: (WebResourceError error) {
- debugPrint('''
- Page resource error:
- code: ${error.errorCode}
- description: ${error.description}
- errorType: ${error.errorType}
- isForMainFrame: ${error.isForMainFrame}
- ''');
- },
- onNavigationRequest: (NavigationRequest request) {
- String url = Uri.decodeComponent(request.url);
- debugPrint('navigationDelegate decode $url');
- return NavigationDecision.navigate;
- },
- ),
- )
- ..addJavaScriptChannel(
- 'DsBridgeApp',
- onMessageReceived: (JavaScriptMessage message) {},
- )
- ..addJavaScriptChannel(
- 'Alert',
- onMessageReceived: (JavaScriptMessage message) {},
- );
- if (widget.isFile ?? false) {
- // var requestUrl = Uri.decodeComponent('${widget.url}?is_flutter=1');
- loadFileUrl(controller, widget.url);
- controller.loadFile(widget.url);
- } else {
- controller.loadRequest(loadUrl);
- }
- // #enddocregion platform_features
- _webViewController = controller;
- jumpCallback = () {
- jumpFunction();
- };
- }
- Future<void> loadFileUrl(
- WebViewController webViewController, String fileAbsoluteFilePath) async {}
- @override
- Widget build(BuildContext context) {
- if (widget.isDialog ?? false) {
- return Stack(
- children: [
- WebViewWidget(controller: _webViewController),
- const Positioned(child: WebviewTimerClose(time: 30), left: 0, top: 0)
- ],
- );
- } else {
- return Container(
- width: double.infinity,
- height: double.infinity,
- decoration: const BoxDecoration(color: Color(0xFFF4F0EF)),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- // AppBar(
- // leading: IconButton(
- // icon: Image.asset(
- // "assets/images/ic_back_arrow.png",
- // width: 34,
- // height: 34,
- // ),
- // onPressed: () => Get.back(closeOverlays: true)),
- // backgroundColor: Colors.transparent,
- // elevation: 0,
- // centerTitle: true,
- // title: Text(
- // _title,
- // ),
- // // actions: actions ?? [],
- // ),
- Expanded(
- child: WebViewWidget(
- controller: _webViewController,
- )),
- ],
- ),
- );
- }
- }
- void reloadWebView() {
- _webViewController.reload();
- }
- }
- class WebviewTimerClose extends StatefulWidget {
- final int time;
- const WebviewTimerClose({Key? key, required this.time}) : super(key: key);
- @override
- WebviewTimerCloseState createState() => WebviewTimerCloseState();
- }
- class WebviewTimerCloseState extends State<WebviewTimerClose> {
- bool showClose = false;
- @override
- void initState() {
- super.initState();
- Future.delayed(Duration(seconds: widget.time)).then((value) {
- try {
- showClose = true;
- if (mounted) setState(() => {});
- } catch (e) {
- e.printError();
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- if (showClose) {
- return Material(
- color: Colors.transparent,
- child: IconButton(
- icon: Image.asset(
- "assets/images/ic_dialog_close.png",
- width: 25,
- height: 25,
- ),
- onPressed: () => Get.back()),
- );
- }
- return Container();
- }
- }
|