admin
2022-05-07 4c7cde7ae5ed57335405459e47de4bbd2726c4ba
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import 'dart:io';
 
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../utils/ui_constant.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';
 
import '../../ui/widget/nav.dart';
import '../../utils/jsinterface.dart';
 
class BrowserPage extends StatefulWidget {
  BrowserPage({Key? key, required this.title, required this.url})
      : super(key: key);
  final String title;
  final String url;
 
  @override
  _BrowserPageState createState() => _BrowserPageState(title, url);
}
 
class _BrowserPageState extends State<BrowserPage>
    with SingleTickerProviderStateMixin {
  String title = "";
  String? url;
  double progress = 0;
 
  _BrowserPageState(this.title, this.url);
 
  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      WebView.platform = AndroidWebView();
    }
  }
 
  WebViewController? _webViewController;
 
  _back() {
    if (_webViewController == null) {
      popPage(context);
    } else {
      _webViewController!.canGoBack().then((value) {
        if (value) {
          _webViewController!.goBack();
        } else {
          popPage(context);
        }
      });
    }
  }
 
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          _back();
          return false;
        },
        child: Scaffold(
            resizeToAvoidBottomInset: true,
            backgroundColor: Colors.white,
            body: Flex(
              direction: Axis.vertical,
              children: [
                TopNavBar(
                  title: title,
                  back: () {
                    _back();
                  },
                  leftWidget: const Padding(
                      padding: EdgeInsets.only(left: 5, right: 10),
                      child: Icon(
                        Icons.close_rounded,
                        size: 30,
                      )),
                  leftWidgetClick: () {
                    popPage(context);
                  },
                  rightIcon: const Icon(
                    Icons.refresh,
                    size: 30,
                  ),
                  rightClick: () {
                    if (_webViewController != null) {
                      _webViewController!.reload();
                    }
                  },
                ),
                SizedBox(
                  height: 1,
                  child: LinearProgressIndicator(
                    backgroundColor: Colors.white,
                    valueColor:
                        const AlwaysStoppedAnimation(ColorConstant.theme),
                    value: progress,
                  ),
                ),
                Expanded(
                    child: WebView(
                  //http://192.168.3.122:8848/test/JsTest.html
                  backgroundColor: Colors.white,
                  initialUrl: url,
                  onWebViewCreated: (WebViewController webViewController) {
                    _webViewController = webViewController;
                  },
                  javascriptMode: JavascriptMode.unrestricted,
                  javascriptChannels:
                      JavascriptInterface(context, _webViewController)
                          .getInterfaces(),
                  navigationDelegate: (NavigationRequest request) {
                    print("链接:${request.url}");
                    if (!request.url.startsWith("http")) {
                      launch(request.url);
                      return NavigationDecision.prevent;
                    }
                    return NavigationDecision.navigate;
                  },
                  onPageStarted: (url) {
                    print("process:onPageStarted-$url");
                  },
                  onPageFinished: (url) {
                    print("process:onPageFinished-$url");
                    _webViewController!.getTitle().then((value) {
                      if (value != null) {
                        setState(() {
                          title = value;
                        });
                      }
                    });
                  },
                  onProgress: (int process) {
                    print("process:$process");
                    setState(() {
                      if (process == 100) {
                        progress = 0;
                      } else {
                        progress = process / 100.0;
                      }
                    });
                  },
                ))
              ],
            )));
  }
}