admin
2021-11-03 0daa197dbaf611eacdf9eeb3763ddcb10038585b
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
import 'dart:io';
 
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:location/ui/widget/nav.dart';
import 'package:location/utils/jsinterface.dart';
import 'package:webview_flutter/webview_flutter.dart';
 
class BrowserPage extends StatefulWidget {
  BrowserPage({Key? key, required this.title}) : super(key: key);
  final String title;
 
  @override
  _BrowserPageState createState() => _BrowserPageState();
}
 
class _BrowserPageState extends State<BrowserPage>
    with SingleTickerProviderStateMixin {
  String title = "";
  double progress = 0;
 
  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      WebView.platform = SurfaceAndroidWebView();
    }
  }
 
  WebViewController? _webViewController;
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: Flex(
          direction: Axis.vertical,
          children: [
            TopNavBar(title: title),
            SizedBox(
              height: 1,
              child: LinearProgressIndicator(
                backgroundColor: Colors.white,
                valueColor: AlwaysStoppedAnimation(Color(0xFF0E96FF)),
                value: progress,
              ),
            ),
            Expanded(
                child: WebView(
                  //http://192.168.3.122:8848/test/JsTest.html
              initialUrl: 'http://192.168.3.122:8848/test/JsTest.html',
              onWebViewCreated: (WebViewController webViewController){
                _webViewController=webViewController;
              },
              javascriptMode: JavascriptMode.unrestricted,
              javascriptChannels: JavascriptInterface(context,_webViewController).getInterfaces(),
              navigationDelegate: (NavigationRequest request) {
                print("链接:${request.url}");
                if (!request.url.startsWith("http")) {
                  return NavigationDecision.prevent;
                }
                return NavigationDecision.navigate;
              },
              onPageStarted: (url) {
                print("process:onPageStarted-$url");
              },
              onPageFinished: (url) {
                print("process:onPageFinished-$url");
              },
              onProgress: (int process) {
                print("process:$process");
                setState(() {
                  if (process == 100) {
                    progress = 0;
                  } else {
                    progress = process / 100.0;
                  }
                });
              },
            ))
          ],
        ));
  }
}