video_play_item_widget.dart 13 KB

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