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 = SurfaceAndroidWebView();
|
}
|
}
|
|
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(
|
backgroundColor: Colors.white,
|
body: Flex(
|
direction: Axis.vertical,
|
children: [
|
TopNavBar(
|
title: title,
|
back: () {
|
_back();
|
},
|
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
|
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;
|
}
|
});
|
},
|
))
|
],
|
)));
|
}
|
}
|