video_play_item_widget.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 || !widget.isActive) return;
  202. final controller = globalVideoController;
  203. if (controller == null) return;
  204. // 检查控制器是否已初始化且未释放
  205. try {
  206. if (controller.isVideoInitialized() != true) {
  207. consoleLog("_togglePlay: controller not initialized");
  208. return;
  209. }
  210. } catch (e) {
  211. consoleLog("_togglePlay: controller check failed: $e");
  212. return;
  213. }
  214. // 分别处理每个操作,确保错误被捕获
  215. if (_isPlaying) {
  216. try {
  217. controller.pause();
  218. // 立即更新状态
  219. setState(() {
  220. _isPlaying = false;
  221. });
  222. } catch (e) {
  223. consoleLog("_togglePlay: pause error: $e");
  224. // 控制器可能已被释放,忽略错误
  225. }
  226. } else {
  227. try {
  228. controller.play();
  229. // 立即更新状态
  230. setState(() {
  231. _isPlaying = true;
  232. });
  233. } catch (e) {
  234. consoleLog("_togglePlay: play error: $e");
  235. // 控制器可能已被释放,忽略错误
  236. }
  237. }
  238. }
  239. @override
  240. Widget build(BuildContext context) {
  241. super.build(context);
  242. final controller = globalVideoController;
  243. return GestureDetector(
  244. onTap: _togglePlay,
  245. child: Stack(
  246. fit: StackFit.expand,
  247. children: [
  248. // 黑色背景
  249. const ColoredBox(color: Colors.black),
  250. // 视频播放器(只在当前页激活时显示)
  251. if (controller != null && widget.isActive)
  252. BetterPlayer(
  253. key: ValueKey('video_${widget.item.contentId}'),
  254. controller: controller,
  255. ),
  256. // 播放按钮(只在视频暂停时显示)
  257. if (!_isPlaying && widget.isActive)
  258. Center(
  259. child: Image.asset(
  260. Assets.images.playIcon.path,
  261. width: 60.w,
  262. height: 60.w,
  263. ),
  264. ),
  265. Positioned(
  266. right: 10.h,
  267. bottom: 100.h,
  268. child: Column(
  269. mainAxisAlignment: MainAxisAlignment.center,
  270. children: [
  271. AuthGestureDetector(
  272. onTap: () {
  273. if (!mounted) return;
  274. ref
  275. .read(recommendListProvider.notifier)
  276. .fetchVideoLike(
  277. videoId: widget.item.contentId,
  278. current: widget.item.isLiked ?? false,
  279. );
  280. },
  281. child: Icon(
  282. Icons.favorite,
  283. color:
  284. widget.item.isLiked == true ? Colors.red : Colors.white,
  285. size: 25.sp,
  286. ),
  287. ),
  288. myTxt(
  289. text:
  290. widget.item.likeCount == null
  291. ? ""
  292. : widget.item.likeCount.toString(),
  293. color: Colors.white,
  294. fontSize: 12.sp,
  295. ),
  296. SizedBox(height: 15.h),
  297. AuthGestureDetector(
  298. onTap: () {
  299. if (!mounted) return;
  300. context.push(
  301. "/video/detail",
  302. extra: VideoParam(
  303. id: widget.item.contentId ?? "",
  304. videoUrl: widget.item.url,
  305. ),
  306. );
  307. },
  308. child: Icon(Icons.message, color: Colors.white, size: 25.sp),
  309. ),
  310. myTxt(
  311. text:
  312. widget.item.commentCount == null
  313. ? "0"
  314. : widget.item.commentCount.toString(),
  315. color: Colors.white,
  316. fontSize: 12.sp,
  317. ),
  318. SizedBox(height: 15.h),
  319. AuthGestureDetector(
  320. onTap: () {
  321. if (!mounted) return;
  322. ref
  323. .read(recommendListProvider.notifier)
  324. .fetchVideoFavorite(
  325. videoId: widget.item.contentId,
  326. current: widget.item.isFavorite ?? false,
  327. );
  328. },
  329. child: Icon(
  330. Icons.star,
  331. color:
  332. widget.item.isFavorite == true
  333. ? Colors.red
  334. : Colors.white,
  335. size: 25.sp,
  336. ),
  337. ),
  338. myTxt(
  339. text:
  340. widget.item.favoriteCount == null
  341. ? "0"
  342. : widget.item.favoriteCount.toString(),
  343. color: Colors.white,
  344. fontSize: 12.sp,
  345. ),
  346. SizedBox(height: 15.h),
  347. GestureDetector(
  348. onTap: () {
  349. if (!mounted) return;
  350. shareAction(widget.item);
  351. if (uuid.isNotEmpty) {
  352. ref
  353. .read(recommendListProvider.notifier)
  354. .fetchVideoShare(contentId: widget.item.contentId);
  355. }
  356. },
  357. child: Icon(
  358. Icons.screen_share,
  359. color: Colors.white,
  360. size: 25.sp,
  361. ),
  362. ),
  363. myTxt(
  364. text:
  365. widget.item.shareCount == null
  366. ? "0"
  367. : widget.item.shareCount.toString(),
  368. color: Colors.white,
  369. fontSize: 12.sp,
  370. ),
  371. ],
  372. ),
  373. ),
  374. ],
  375. ),
  376. );
  377. }
  378. @override
  379. bool get wantKeepAlive => true;
  380. }