video_play_item_widget.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import 'dart:io';
  2. import 'package:better_player_plus/better_player_plus.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_riverpod/flutter_riverpod.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:go_router/go_router.dart';
  7. import 'package:news_app/ui/video/video_detail_page.dart';
  8. import 'package:news_app/ui/video/video_recommend_list_page.dart';
  9. import '../../constant/config.dart';
  10. import '../../gen/assets.gen.dart';
  11. import '../../model/video_new_model.dart';
  12. import '../../util/device_util.dart';
  13. import '../../util/share_util.dart';
  14. import '../../widget/auth_gesture_detector.dart';
  15. import '../../widget/load_image.dart';
  16. import '../../widget/my_txt.dart';
  17. class VideoPlayItemWidget extends ConsumerStatefulWidget {
  18. final VideoNewModel item;
  19. final bool isActive;
  20. const VideoPlayItemWidget({
  21. super.key,
  22. required this.item,
  23. required this.isActive,
  24. });
  25. @override
  26. ConsumerState<VideoPlayItemWidget> createState() => _VideoItemWidgetState();
  27. }
  28. class _VideoItemWidgetState extends ConsumerState<VideoPlayItemWidget> {
  29. late BetterPlayerController _controller;
  30. bool _isPlaying = false;
  31. bool _isWeChatInstalled = false;
  32. Future<void> _checkWeChatInstallation() async {
  33. if (Platform.isAndroid) {
  34. final installed = await isWeChatInstalledOnlyAndroid();
  35. setState(() => _isWeChatInstalled = installed);
  36. } else if (Platform.isIOS) {
  37. final installed = await fluwx.isWeChatInstalled;
  38. setState(() => _isWeChatInstalled = installed);
  39. }
  40. }
  41. Future<void> shareAction(VideoNewModel data) async {
  42. showModalBottomSheet(
  43. context: context,
  44. builder:
  45. (context) => SafeArea(
  46. child: Column(
  47. mainAxisSize: MainAxisSize.min,
  48. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  49. children: [
  50. SizedBox(height: 10.h),
  51. Container(
  52. width: double.infinity,
  53. height: 80.h,
  54. decoration: BoxDecoration(
  55. borderRadius: BorderRadius.only(
  56. topLeft: Radius.circular(10.r),
  57. topRight: Radius.circular(10.r),
  58. ),
  59. color: Colors.white,
  60. ),
  61. child: Row(
  62. crossAxisAlignment: CrossAxisAlignment.center,
  63. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  64. children: [
  65. if (_isWeChatInstalled)
  66. GestureDetector(
  67. onTap: () {
  68. Navigator.pop(context);
  69. shareWeiXinUrl(
  70. title: data.shareDesc ?? "",
  71. url: data.shareUrl ?? "",
  72. );
  73. },
  74. child: Container(
  75. width: 100.w,
  76. height: 80.h,
  77. child: Column(
  78. crossAxisAlignment: CrossAxisAlignment.center,
  79. mainAxisAlignment: MainAxisAlignment.center,
  80. children: [
  81. LoadAssetImage(
  82. 'share_wxhy',
  83. width: 40.w,
  84. height: 40.h,
  85. ),
  86. SizedBox(height: 10.h),
  87. myTxt(
  88. text: "微信好友",
  89. color: Colors.black,
  90. fontSize: 12.sp,
  91. fontWeight: FontWeight.bold,
  92. ),
  93. ],
  94. ),
  95. ),
  96. ),
  97. if (_isWeChatInstalled)
  98. GestureDetector(
  99. onTap: () {
  100. Navigator.pop(context);
  101. shareWeiXinPYUrl(
  102. title: data.shareDesc ?? "",
  103. url: data.shareUrl ?? "",
  104. );
  105. },
  106. child: Container(
  107. width: 100.w,
  108. height: 80.h,
  109. child: Column(
  110. mainAxisAlignment: MainAxisAlignment.center,
  111. children: [
  112. LoadAssetImage(
  113. 'share_pyq',
  114. width: 40.w,
  115. height: 40.h,
  116. ),
  117. SizedBox(height: 10.h),
  118. myTxt(
  119. text: "朋友圈",
  120. color: Colors.black,
  121. fontSize: 12.sp,
  122. fontWeight: FontWeight.bold,
  123. ),
  124. ],
  125. ),
  126. ),
  127. ),
  128. GestureDetector(
  129. onTap: () {
  130. Navigator.pop(context);
  131. shareUrl(
  132. title: data.shareDesc ?? "",
  133. url: data.shareUrl ?? "",
  134. );
  135. },
  136. child: Container(
  137. width: 100.w,
  138. height: 80.h,
  139. child: Column(
  140. mainAxisAlignment: MainAxisAlignment.center,
  141. children: [
  142. LoadAssetImage(
  143. 'share_xtfx',
  144. width: 40.w,
  145. height: 40.h,
  146. ),
  147. SizedBox(height: 10.h),
  148. myTxt(
  149. text: "系统分享",
  150. color: Colors.black,
  151. fontSize: 12.sp,
  152. fontWeight: FontWeight.bold,
  153. ),
  154. ],
  155. ),
  156. ),
  157. ),
  158. ],
  159. ),
  160. ),
  161. ],
  162. ),
  163. ),
  164. );
  165. }
  166. @override
  167. void initState() {
  168. super.initState();
  169. _checkWeChatInstallation();
  170. _controller = BetterPlayerController(
  171. BetterPlayerConfiguration(
  172. autoPlay: false,
  173. looping: true,
  174. aspectRatio: 16 / 9,
  175. fit: BoxFit.fitWidth,
  176. controlsConfiguration: const BetterPlayerControlsConfiguration(
  177. showControls: false,
  178. ),
  179. ),
  180. betterPlayerDataSource: BetterPlayerDataSource(
  181. BetterPlayerDataSourceType.network,
  182. widget.item.url ?? "",
  183. ),
  184. );
  185. _controller.addEventsListener(_handlePlayerEvent);
  186. // 如果初始化时就是激活状态,立即播放
  187. if (widget.isActive && mounted) {
  188. _controller.play();
  189. }
  190. }
  191. void _handlePlayerEvent(BetterPlayerEvent event) {
  192. if (!mounted) return;
  193. if (event.betterPlayerEventType == BetterPlayerEventType.play ||
  194. event.betterPlayerEventType == BetterPlayerEventType.pause ||
  195. event.betterPlayerEventType == BetterPlayerEventType.finished) {
  196. final isPlaying = _controller.isPlaying() ?? false;
  197. if (mounted && isPlaying != _isPlaying) {
  198. setState(() {
  199. _isPlaying = isPlaying;
  200. });
  201. }
  202. }
  203. }
  204. @override
  205. void didUpdateWidget(covariant VideoPlayItemWidget oldWidget) {
  206. super.didUpdateWidget(oldWidget);
  207. if (!mounted) return;
  208. if (widget.isActive && !oldWidget.isActive) {
  209. _controller.play();
  210. } else if (!widget.isActive && oldWidget.isActive) {
  211. _controller.pause();
  212. }
  213. }
  214. @override
  215. void dispose() {
  216. _controller.removeEventsListener(_handlePlayerEvent);
  217. _controller.dispose();
  218. super.dispose();
  219. }
  220. void _togglePlay() {
  221. if (!mounted) return;
  222. if (_isPlaying) {
  223. _controller.pause();
  224. } else {
  225. _controller.play();
  226. }
  227. }
  228. @override
  229. Widget build(BuildContext context) {
  230. return GestureDetector(
  231. onTap: _togglePlay,
  232. child: Stack(
  233. fit: StackFit.expand,
  234. children: [
  235. ColoredBox(
  236. color: Colors.black,
  237. child: BetterPlayer(controller: _controller),
  238. ),
  239. if (!_isPlaying)
  240. Center(
  241. child: Image.asset(
  242. Assets.images.playIcon.path,
  243. width: 60.w,
  244. height: 60.w,
  245. ),
  246. ),
  247. Positioned(
  248. right: 10.h,
  249. bottom: 100.h,
  250. child: Column(
  251. mainAxisAlignment: MainAxisAlignment.center,
  252. children: [
  253. AuthGestureDetector(
  254. onTap: () {
  255. ref
  256. .read(recommendListProvider.notifier)
  257. .fetchVideoLike(
  258. videoId: widget.item.contentId,
  259. current: widget.item.isLiked ?? false,
  260. );
  261. },
  262. child: Icon(
  263. Icons.favorite,
  264. color:
  265. widget.item.isLiked == true ? Colors.red : Colors.white,
  266. size: 25.sp,
  267. ),
  268. ),
  269. myTxt(
  270. text:
  271. widget.item.likeCount == null
  272. ? ""
  273. : widget.item.likeCount.toString(),
  274. color: Colors.white,
  275. fontSize: 12.sp,
  276. ),
  277. SizedBox(height: 15.h),
  278. AuthGestureDetector(
  279. onTap: () {
  280. context.push(
  281. "/video/detail",
  282. extra: VideoParam(
  283. id: widget.item.contentId ?? "",
  284. videoUrl: widget.item.url,
  285. ),
  286. );
  287. },
  288. child: Icon(Icons.message, color: Colors.white, size: 25.sp),
  289. ),
  290. myTxt(
  291. text:
  292. widget.item.commentCount == null
  293. ? "0"
  294. : widget.item.commentCount.toString(),
  295. color: Colors.white,
  296. fontSize: 12.sp,
  297. ),
  298. SizedBox(height: 15.h),
  299. AuthGestureDetector(
  300. onTap: () {
  301. ref
  302. .read(recommendListProvider.notifier)
  303. .fetchVideoFavorite(
  304. videoId: widget.item.contentId,
  305. current: widget.item.isFavorite ?? false,
  306. );
  307. },
  308. child: Icon(
  309. Icons.star,
  310. color:
  311. widget.item.isFavorite == true
  312. ? Colors.red
  313. : Colors.white,
  314. size: 25.sp,
  315. ),
  316. ),
  317. myTxt(
  318. text:
  319. widget.item.favoriteCount == null
  320. ? "0"
  321. : widget.item.favoriteCount.toString(),
  322. color: Colors.white,
  323. fontSize: 12.sp,
  324. ),
  325. SizedBox(height: 15.h),
  326. GestureDetector(
  327. onTap: () {
  328. // shareUrl(
  329. // title: widget.item.shareDesc ?? "",
  330. // url: widget.item.shareUrl ?? "",
  331. // );
  332. shareAction(widget.item);
  333. if (uuid.isNotEmpty) {
  334. ref
  335. .read(recommendListProvider.notifier)
  336. .fetchVideoShare(contentId: widget.item.contentId);
  337. }
  338. },
  339. child: Icon(
  340. Icons.screen_share,
  341. color: Colors.white,
  342. size: 25.sp,
  343. ),
  344. ),
  345. myTxt(
  346. text:
  347. widget.item.shareCount == null
  348. ? "0"
  349. : widget.item.shareCount.toString(),
  350. color: Colors.white,
  351. fontSize: 12.sp,
  352. ),
  353. ],
  354. ),
  355. ),
  356. ],
  357. ),
  358. );
  359. }
  360. }