| | |
| | | import 'package:locations/utils/jsinterface.dart'; |
| | | import 'package:locations/utils/string_util.dart'; |
| | | import 'package:webview_flutter/webview_flutter.dart'; |
| | | import 'package:url_launcher/url_launcher.dart'; |
| | | |
| | | class BrowserPage extends StatefulWidget { |
| | | BrowserPage({Key? key, required this.title, required this.url}) |
| | |
| | | |
| | | WebViewController? _webViewController; |
| | | |
| | | _back() { |
| | | if (_webViewController == null) { |
| | | Navigator.of(context).pop(); |
| | | } else { |
| | | _webViewController!.canGoBack().then((value) { |
| | | if (value) { |
| | | _webViewController!.goBack(); |
| | | } else { |
| | | Navigator.of(context).pop(); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | @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: const AlwaysStoppedAnimation(Color(0xFF0E96FF)), |
| | | value: progress, |
| | | ), |
| | | ), |
| | | Expanded( |
| | | child: WebView( |
| | | //http://192.168.3.122:8848/test/JsTest.html |
| | | initialUrl: "http://192.168.3.122:8848/location/abountOur/tousu.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"); |
| | | _webViewController!.getTitle().then((value) { |
| | | if (value != null) { |
| | | setState(() { |
| | | title = value; |
| | | return WillPopScope( |
| | | onWillPop: () async { |
| | | _back(); |
| | | return false; |
| | | }, |
| | | child: Scaffold( |
| | | backgroundColor: Colors.white, |
| | | body: Flex( |
| | | direction: Axis.vertical, |
| | | children: [ |
| | | TopNavBar( |
| | | title: title, |
| | | back: () { |
| | | _back(); |
| | | }, |
| | | rightIcon: Image.asset( |
| | | "assets/images/common/icon_refresh.png", |
| | | width: 22, |
| | | ), |
| | | rightClick: (){ |
| | | if(_webViewController!=null){ |
| | | _webViewController!.reload(); |
| | | } |
| | | }, |
| | | ), |
| | | SizedBox( |
| | | height: 1, |
| | | child: LinearProgressIndicator( |
| | | backgroundColor: Colors.white, |
| | | valueColor: const AlwaysStoppedAnimation(Color(0xFF0E96FF)), |
| | | value: progress, |
| | | ), |
| | | ), |
| | | Expanded( |
| | | child: WebView( |
| | | //http://192.168.3.122:8848/test/JsTest.html |
| | | 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; |
| | | } |
| | | }); |
| | | }, |
| | | )) |
| | | ], |
| | | )); |
| | | }, |
| | | onProgress: (int process) { |
| | | print("process:$process"); |
| | | setState(() { |
| | | if (process == 100) { |
| | | progress = 0; |
| | | } else { |
| | | progress = process / 100.0; |
| | | } |
| | | }); |
| | | }, |
| | | )) |
| | | ], |
| | | ))); |
| | | } |
| | | } |