Blame view

lib/common/widgets/webview_dialog.dart 3.03 KB
2a9895f6   吴启风   feat:首次启动隐私协议弹窗
1
2
3
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
  import 'package:flutter/material.dart';
  import 'package:flutter_easyloading/flutter_easyloading.dart';
  import 'package:webview_flutter/webview_flutter.dart';
  
  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>[
          TextButton(
            child:
                const Text('同意并继续', style: TextStyle(color: Color(0xFFFBB621))),
            onPressed: () {
              // 处理接受按钮的点击事件
              leftTap(); // 关闭对话框
            },
          ),
          TextButton(
            child: const Text('不同意,退出应用'),
            onPressed: () {
              // 处理拒绝按钮的点击事件
              rightTap(); // 关闭对话框
            },
          ),
        ],
      );
    }
  
    Future<Widget> buildWebViewController(String url) async {
      Widget res;
      try {
        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);
      } catch (error) {
        res = Text("加载失败:${error.toString()}");
        debugPrint("WebViewController加载失败:${error.toString()}");
      }
      return res;
    }
  }