import 'dart:io';
|
|
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/material.dart';
|
import 'package:flutter/services.dart';
|
import '../../utils/ui_constant.dart';
|
import '../../utils/ui_utils.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
import '../../ui/widget/nav.dart';
|
import '../../utils/jsinterface.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
|
class VideoPlayerWebPage extends StatefulWidget {
|
VideoPlayerWebPage({Key? key, required this.title, required this.url})
|
: super(key: key);
|
final String title;
|
final String url;
|
|
@override
|
_VideoPlayerWebPageState createState() =>
|
_VideoPlayerWebPageState(title, url);
|
}
|
|
class _VideoPlayerWebPageState extends State<VideoPlayerWebPage>
|
with SingleTickerProviderStateMixin {
|
String title = "";
|
String? url;
|
double progress = 0;
|
|
_VideoPlayerWebPageState(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);
|
}
|
});
|
}
|
}
|
|
Future<void> _launchInBrowser(String url) async {
|
if (!await launch(url)) throw 'Could not launch $url';
|
}
|
|
@override
|
Widget build(BuildContext context) {
|
return WillPopScope(
|
onWillPop: () async {
|
_back();
|
return false;
|
},
|
child: Scaffold(
|
backgroundColor: Colors.white,
|
body: Flex(
|
direction: Axis.vertical,
|
children: [
|
Flex(direction: Axis.vertical, children: [
|
Container(
|
height: MediaQuery.of(context).viewPadding.top,
|
color: Colors.white,
|
),
|
Container(
|
color: Colors.white,
|
height: 48,
|
child: Stack(
|
alignment: Alignment.centerLeft,
|
children: [
|
Positioned(
|
child: Container(
|
alignment: Alignment.center,
|
child: Flex(
|
direction: Axis.horizontal,
|
mainAxisAlignment: MainAxisAlignment.center,
|
children: [
|
Container(
|
width: 70,
|
),
|
Expanded(
|
child: Column(
|
crossAxisAlignment:
|
CrossAxisAlignment.start,
|
mainAxisAlignment:
|
MainAxisAlignment.center,
|
children: [
|
Text(
|
title,
|
maxLines: 1,
|
overflow: TextOverflow.ellipsis,
|
style: const TextStyle(
|
fontSize: 18,
|
color: Colors.black),
|
),
|
Text(
|
url!,
|
maxLines: 1,
|
overflow: TextOverflow.ellipsis,
|
style: const TextStyle(
|
fontSize: 10,
|
color: Color(0xFF999999)),
|
)
|
])),
|
Container(
|
width: 50,
|
)
|
],
|
))),
|
Positioned(
|
right: 0,
|
top: 0,
|
bottom: 0,
|
child: InkWell(
|
onTap: () {},
|
child: Container(
|
alignment: Alignment.center,
|
padding: const EdgeInsets.only(right: 10),
|
child: PopupMenuButton(
|
offset: const Offset(0, 48),
|
icon: const Icon(
|
Icons.more_horiz,
|
size: 38,
|
color: Color(0xFF666666),
|
),
|
itemBuilder: (BuildContext context) {
|
return [
|
PopupMenuItem(
|
textStyle: const TextStyle(
|
fontSize: 15,
|
color: Color(0xFF666666)),
|
child: Row(
|
mainAxisSize: MainAxisSize.min,
|
children: [
|
Image.asset(
|
"assets/imgs/common/icon_browser_refresh.png",
|
width: 17,
|
),
|
Container(
|
width: 9,
|
),
|
const Text("刷新")
|
]),
|
onTap: () {
|
if (_webViewController == null) {
|
return;
|
}
|
_webViewController!.reload();
|
},
|
),
|
PopupMenuItem(
|
textStyle: const TextStyle(
|
fontSize: 15,
|
color: Color(0xFF666666)),
|
child: Row(
|
mainAxisSize: MainAxisSize.min,
|
children: [
|
Image.asset(
|
"assets/imgs/common/icon_browser_link.png",
|
width: 17,
|
),
|
Container(
|
width: 9,
|
),
|
const Text("复制链接")
|
]),
|
onTap: () {
|
if (url == null) {
|
return;
|
}
|
Clipboard.setData(
|
ClipboardData(text: url));
|
ToastUtil.toast("复制成功", context);
|
},
|
),
|
PopupMenuItem(
|
textStyle: const TextStyle(
|
fontSize: 15,
|
color: Color(0xFF666666)),
|
child: Row(
|
mainAxisSize: MainAxisSize.min,
|
children: [
|
Image.asset(
|
"assets/imgs/common/icon_browser_out.png",
|
width: 17,
|
),
|
Container(
|
width: 9,
|
),
|
const Text("外部浏览器打开")
|
]),
|
onTap: () {
|
if (url == null) {
|
return;
|
}
|
_launchInBrowser(url!);
|
},
|
),
|
];
|
},
|
),
|
))),
|
Positioned(
|
left: 30,
|
child: InkWell(
|
onTap: () {
|
popPage(context);
|
},
|
child: const Icon(
|
Icons.clear,
|
color: Color(0xFF666666),
|
size: 35,
|
)),
|
),
|
InkWell(
|
onTap: () {
|
_webViewController!.canGoBack().then((value) {
|
if (value) {
|
_webViewController!.goBack();
|
} else {
|
popPage(context);
|
}
|
});
|
},
|
child: Container(
|
padding: const EdgeInsets.only(left: 10),
|
height: 48,
|
child: Row(
|
mainAxisSize: MainAxisSize.min,
|
crossAxisAlignment: CrossAxisAlignment.center,
|
children: const [
|
Icon(
|
Icons.arrow_back_ios,
|
color: Color(0xFF666666),
|
size: 30,
|
),
|
]),
|
))
|
],
|
),
|
)
|
]),
|
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");
|
if (progress == 0) {
|
if (_webViewController != null) {
|
_webViewController!.currentUrl().then((value) {
|
setState(() {
|
url = value;
|
});
|
});
|
}
|
}
|
setState(() {
|
if (process == 100) {
|
progress = 0;
|
} else {
|
progress = process / 100.0;
|
}
|
});
|
},
|
))
|
],
|
)));
|
}
|
}
|