You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.8 KiB

3 years ago
import 'package:chewie/chewie.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/image_render.dart';
import 'package:chewie/src/chewie_progress_colors.dart' as chewie;
import 'package:flutter/material.dart';
import 'package:flutter_html/src/replaced_element.dart';
3 years ago
import 'package:flutter_html/style.dart';
3 years ago
import 'package:huixiang/retrofit/data/activity.dart';
import 'package:huixiang/retrofit/data/article.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class WebContent extends StatefulWidget {
final Activity activity;
final Article article;
3 years ago
final Function exitFull;
3 years ago
3 years ago
WebContent(this.activity, this.article, this.exitFull);
3 years ago
@override
State<StatefulWidget> createState() {
return _WebContent();
}
}
class _WebContent extends State<WebContent> {
3 years ago
3 years ago
3 years ago
@override
Widget build(BuildContext context) {
return Html(
3 years ago
data: (widget.activity?.content??widget.article?.content??"")+"<a storeName=\"前进麦味烘焙*海峡姐妹茶(哈乐城店)\" storeId=\"1432164681279078400\" tenantCode=\"1175\" href=\"/\">立即购买</a><img storeName=\"前进麦味烘焙*海峡姐妹茶(哈乐城店)\" storeId=\"1432164681279078400\" tenantCode=\"1175\" src=\"https://pos.upload.gznl.top/MDAwMA==/2021/12/6edfe416-aaf9-4f0e-b2a4-4dc85db03489.jpg\" />",
3 years ago
style: {
"html": Style(
backgroundColor: Colors.white
)
},
3 years ago
onLinkTap: (url, ct, attributes, element) {
if(attributes.containsKey("storeid")){
Navigator.of(context).pushNamed(
'/router/store_order',
arguments: {
"id": attributes["storeid"],
"tenant": attributes["tenantcode"],
"storeName": attributes["storename"]
},
);
}
},
onImageTap: (url,ct,attributes, element) {
if(attributes.containsKey("storeid")){
Navigator.of(context).pushNamed(
'/router/store_order',
arguments: {
"id": attributes["storeid"],
"tenant": attributes["tenantcode"],
"storeName": attributes["storename"]
},
);
}
},
3 years ago
customImageRenders: {
assetUriMatcher(): assetImageRender(),
networkSourceMatcher(extension: "svg"):
svgNetworkImageRender(),
networkSourceMatcher(): networkImageRender(
loadingWidget: () {
return Container();
},
),
},
3 years ago
customRender: {
"video":
(context, parsedChild) {
var src = context.tree.element.attributes["src"];
return videoWidget(
double.tryParse(context.tree.element.attributes["width"] ?? ""),
double.tryParse(
context.tree.element.attributes["height"] ?? ""),
(src != null &&
src != "" &&
src.endsWith(".mp4"))
? src
: context.tree.children.first.attributes["src"],
context.tree.attributes["sandbox"]);
},
"iframe":
(context, parsedChild) {
var src = context.tree.element.children.firstWhere((element) => element.localName == "source").attributes["src"];
return videoWidget(
double.tryParse(context.style.width ?? ""),
double.tryParse(context.style.height ?? ""),
(src != null &&
src != "" &&
src.endsWith(".mp4"))
? src
: context.tree.children.first.attributes["src"],
context.tree.attributes["sandbox"]);
},
"audio":
(context, parsedChild) {
final sources = <String>[
if (context.tree.attributes['src'] != null)
context.tree.attributes['src'],
];
if (sources == null ||
sources.isEmpty ||
sources.first == null) {
return EmptyContentElement();
}
return audioWidget(
context.tree.attributes['controls'] != null,
context.tree.attributes['loop'] != null,
context.tree.attributes['autoplay'] != null,
sources,
context.style.width ?? 300.w,
);
},
},
3 years ago
);
}
VideoPlayerController videoPlayerController;
ChewieController chewieAudioController;
Chewie chewies;
Widget videoWidget(double width, double height, src, sandboxMode) {
print("src : $src");
3 years ago
chewieAudioController = ChewieController(
videoPlayerController: videoPlayerController =
VideoPlayerController.network(
src,
),
aspectRatio: width / height,
//宽高比
autoPlay: false,
//自动播放
looping: false,
//循环播放
allowFullScreen: true,
// systemOverlaysAfterFullScreen: [],
// systemOverlaysOnEnterFullScreen: [],
// deviceOrientationsAfterFullScreen: [],
// deviceOrientationsOnEnterFullScreen: [],
// 拖动条样式颜色
materialProgressColors: chewie.ChewieProgressColors(
playedColor: Colors.white,
handleColor: Colors.white,
backgroundColor: Colors.grey,
bufferedColor: Colors.transparent,
),
autoInitialize: true,
);
chewieAudioController.addListener(_fullScreenListener);
3 years ago
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 0.9,
),
child: Container(
width: MediaQuery.of(context).size.width - 17,
height: (MediaQuery.of(context).size.width) / (width / height),
child: chewies = Chewie(
3 years ago
controller: chewieAudioController,
3 years ago
),
),
);
}
3 years ago
Future<void> _fullScreenListener() async {
print("object: isPlaying: ${videoPlayerController.value.isPlaying}");
print("object: isFullScreen: ${chewieAudioController.isFullScreen}");
if (!chewieAudioController.isFullScreen) {
3 years ago
Future.delayed(Duration(seconds: 1), () {
widget.exitFull();
});
3 years ago
}
}
3 years ago
Widget audioWidget(showControls, loop, autoplay, src, width) {
return Container(
width: width,
child: chewies = Chewie(
controller: chewieAudioController = ChewieController(
videoPlayerController: VideoPlayerController.network(
src.first ?? "",
),
autoPlay: autoplay,
looping: loop,
showControls: showControls,
autoInitialize: true,
),
),
);
}
@override
void dispose() {
if (chewieAudioController != null) {
chewieAudioController.pause();
chewieAudioController.dispose();
chewieAudioController = null;
}
if (videoPlayerController != null) {
videoPlayerController.pause();
videoPlayerController.dispose();
}
super.dispose();
}
}