Blame view

lib/common/widgets/webview_dialog.dart 4.06 KB
4b858f67   吴启风   feat:webview库替换
1
  import 'dart:collection';
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
2
  import 'package:flutter/material.dart';
4b858f67   吴启风   feat:webview库替换
3
  import 'package:flutter_inappwebview/flutter_inappwebview.dart';
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
  class WebviewDialog extends StatelessWidget {
    final String title;
    final String webUrl;
    final VoidCallback leftTap;
    final VoidCallback rightTap;
  
    const WebviewDialog(
        {super.key,
        required this.title,
        required this.webUrl,
        required this.leftTap,
        required this.rightTap});
  
    @override
    Widget build(BuildContext context) {
      return AlertDialog(
        title: Center(
          child: Text(title),
        ),
        content: SizedBox(
            width: MediaQuery.of(context).size.height - 100,
            height: MediaQuery.of(context).size.width - 100,
            child: FutureBuilder(
                // 异步方法
                future: buildWebViewController(webUrl),
                builder: (context, snapshot) {
                  // 等待状态显示的widget
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return const Center(
                      child: CircularProgressIndicator(),
                    );
                    //  错误时显示的widget
                  } else if (snapshot.hasError) {
                    return const Text('Error');
                  } else {
                    return snapshot.data ?? const Text('No data');
                  }
                })),
        actions: <Widget>[
ddc26958   吴启风   feat:隐私协议弹窗按钮布局优化
44
45
46
47
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              TextButton(
4b858f67   吴启风   feat:webview库替换
48
49
                child: const Text('同意并继续',
                    style: TextStyle(color: Color(0xFFFBB621))),
ddc26958   吴启风   feat:隐私协议弹窗按钮布局优化
50
51
52
53
54
55
56
57
58
59
60
61
62
63
                onPressed: () {
                  // 处理接受按钮的点击事件
                  leftTap(); // 关闭对话框
                },
              ),
              TextButton(
                child: const Text('不同意,退出应用'),
                onPressed: () {
                  // 处理拒绝按钮的点击事件
                  rightTap(); // 关闭对话框
                },
              ),
            ],
          )
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
64
65
66
67
68
69
70
        ],
      );
    }
  
    Future<Widget> buildWebViewController(String url) async {
      Widget res;
      try {
4b858f67   吴启风   feat:webview库替换
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        // WebViewController controller = WebViewController()
        //   ..setJavaScriptMode(JavaScriptMode.unrestricted)
        //   ..setBackgroundColor(const Color(0x00000000))
        //   ..setNavigationDelegate(
        //     NavigationDelegate(
        //       onProgress: (int progress) {
        //         // Update loading bar.
        //       },
        //       onPageStarted: (String url) {
        //         EasyLoading.show();
        //       },
        //       onPageFinished: (String url) {
        //         EasyLoading.dismiss();
        //       },
        //       onWebResourceError: (WebResourceError error) {
        //         EasyLoading.showError(error.description);
        //       },
        //       onNavigationRequest: (NavigationRequest request) {
        //         return NavigationDecision.navigate;
        //       },
        //     ),
        //   );
        // await controller.loadRequest(Uri.parse(url));
        // res = WebViewWidget(controller: controller);
  
        res = InAppWebView(
            initialUrlRequest: URLRequest(url: Uri.parse(url)),
            initialUserScripts: UnmodifiableListView<UserScript>([]),
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                useShouldOverrideUrlLoading: true, // 是否需要跳转
                mediaPlaybackRequiresUserGesture: false, // 设置为true,方式H5的音频自动播放
                transparentBackground: true
              ),
              android: AndroidInAppWebViewOptions(
                useHybridComposition: true,
              ),
              ios: IOSInAppWebViewOptions(
                allowsInlineMediaPlayback: true,
              ),
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
111
            ),
4b858f67   吴启风   feat:webview库替换
112
113
114
115
            onWebViewCreated: (controller) {
              // _inAppWebViewController = controller;
            },
        );
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
116
117
118
119
120
121
122
      } catch (error) {
        res = Text("加载失败:${error.toString()}");
        debugPrint("WebViewController加载失败:${error.toString()}");
      }
      return res;
    }
  }