video_play_item_widget.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 '../../constant/config.dart';
  9. import '../../gen/assets.gen.dart';
  10. import '../../model/video_new_model.dart';
  11. import '../../util/device_util.dart';
  12. import '../../util/share_util.dart';
  13. import '../../widget/auth_gesture_detector.dart';
  14. import '../../widget/load_image.dart';
  15. import '../../widget/my_txt.dart';
  16. import '../video/video_recommend_list_page.dart';
  17. class VideoPlayItemWidget extends ConsumerStatefulWidget {
  18. final VideoNewModel item;
  19. final bool isActive;
  20. final int index;
  21. const VideoPlayItemWidget({
  22. super.key,
  23. required this.item,
  24. required this.isActive,
  25. required this.index,
  26. });
  27. @override
  28. ConsumerState<VideoPlayItemWidget> createState() => _VideoItemWidgetState();
  29. }
  30. class _VideoItemWidgetState extends ConsumerState<VideoPlayItemWidget>
  31. with AutomaticKeepAliveClientMixin {
  32. bool _isPlaying = false;
  33. bool _isWeChatInstalled = false;
  34. Future<void> _checkWeChatInstallation() async {
  35. if (Platform.isAndroid) {
  36. final installed = await isWeChatInstalledOnlyAndroid();
  37. if (mounted) {
  38. setState(() => _isWeChatInstalled = installed);
  39. }
  40. } else if (Platform.isIOS) {
  41. final installed = await fluwx.isWeChatInstalled;
  42. if (mounted) {
  43. setState(() => _isWeChatInstalled = installed);
  44. }
  45. }
  46. }
  47. Future<void> shareAction(VideoNewModel data) async {
  48. if (!mounted) return;
  49. showModalBottomSheet(
  50. context: context,
  51. builder:
  52. (context) => SafeArea(
  53. child: Column(
  54. mainAxisSize: MainAxisSize.min,
  55. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  56. children: [
  57. SizedBox(height: 10.h),
  58. SizedBox(
  59. width: double.infinity,
  60. height: 80.h,
  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: SizedBox(
  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: SizedBox(
  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: SizedBox(
  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. }
  171. void _togglePlay() {
  172. if (!mounted) return;
  173. final controller = globalVideoController;
  174. if (controller == null) return;
  175. // 分别处理每个操作,确保错误被捕获
  176. if (_isPlaying) {
  177. try {
  178. controller.pause();
  179. } catch (e) {
  180. // 控制器可能已被释放,忽略错误
  181. }
  182. } else {
  183. try {
  184. controller.play();
  185. } catch (e) {
  186. // 控制器可能已被释放,忽略错误
  187. }
  188. }
  189. }
  190. @override
  191. Widget build(BuildContext context) {
  192. super.build(context);
  193. final controller = globalVideoController;
  194. return GestureDetector(
  195. onTap: _togglePlay,
  196. child: Stack(
  197. fit: StackFit.expand,
  198. children: [
  199. ColoredBox(
  200. color: Colors.black,
  201. child: controller != null && widget.isActive
  202. ? AspectRatio(
  203. aspectRatio: 16 / 9,
  204. child: BetterPlayer(controller: controller),
  205. )
  206. : const SizedBox.shrink(),
  207. ),
  208. if (!_isPlaying && widget.isActive && controller != null)
  209. Center(
  210. child: Image.asset(
  211. Assets.images.playIcon.path,
  212. width: 60.w,
  213. height: 60.w,
  214. ),
  215. ),
  216. Positioned(
  217. right: 10.h,
  218. bottom: 100.h,
  219. child: Column(
  220. mainAxisAlignment: MainAxisAlignment.center,
  221. children: [
  222. AuthGestureDetector(
  223. onTap: () {
  224. if (!mounted) return;
  225. ref
  226. .read(recommendListProvider.notifier)
  227. .fetchVideoLike(
  228. videoId: widget.item.contentId,
  229. current: widget.item.isLiked ?? false,
  230. );
  231. },
  232. child: Icon(
  233. Icons.favorite,
  234. color:
  235. widget.item.isLiked == true ? Colors.red : Colors.white,
  236. size: 25.sp,
  237. ),
  238. ),
  239. myTxt(
  240. text:
  241. widget.item.likeCount == null
  242. ? ""
  243. : widget.item.likeCount.toString(),
  244. color: Colors.white,
  245. fontSize: 12.sp,
  246. ),
  247. SizedBox(height: 15.h),
  248. AuthGestureDetector(
  249. onTap: () {
  250. if (!mounted) return;
  251. context.push(
  252. "/video/detail",
  253. extra: VideoParam(
  254. id: widget.item.contentId ?? "",
  255. videoUrl: widget.item.url,
  256. ),
  257. );
  258. },
  259. child: Icon(Icons.message, color: Colors.white, size: 25.sp),
  260. ),
  261. myTxt(
  262. text:
  263. widget.item.commentCount == null
  264. ? "0"
  265. : widget.item.commentCount.toString(),
  266. color: Colors.white,
  267. fontSize: 12.sp,
  268. ),
  269. SizedBox(height: 15.h),
  270. AuthGestureDetector(
  271. onTap: () {
  272. if (!mounted) return;
  273. ref
  274. .read(recommendListProvider.notifier)
  275. .fetchVideoFavorite(
  276. videoId: widget.item.contentId,
  277. current: widget.item.isFavorite ?? false,
  278. );
  279. },
  280. child: Icon(
  281. Icons.star,
  282. color:
  283. widget.item.isFavorite == true
  284. ? Colors.red
  285. : Colors.white,
  286. size: 25.sp,
  287. ),
  288. ),
  289. myTxt(
  290. text:
  291. widget.item.favoriteCount == null
  292. ? "0"
  293. : widget.item.favoriteCount.toString(),
  294. color: Colors.white,
  295. fontSize: 12.sp,
  296. ),
  297. SizedBox(height: 15.h),
  298. GestureDetector(
  299. onTap: () {
  300. if (!mounted) return;
  301. shareAction(widget.item);
  302. if (uuid.isNotEmpty) {
  303. ref
  304. .read(recommendListProvider.notifier)
  305. .fetchVideoShare(contentId: widget.item.contentId);
  306. }
  307. },
  308. child: Icon(
  309. Icons.screen_share,
  310. color: Colors.white,
  311. size: 25.sp,
  312. ),
  313. ),
  314. myTxt(
  315. text:
  316. widget.item.shareCount == null
  317. ? "0"
  318. : widget.item.shareCount.toString(),
  319. color: Colors.white,
  320. fontSize: 12.sp,
  321. ),
  322. ],
  323. ),
  324. ),
  325. ],
  326. ),
  327. );
  328. }
  329. @override
  330. bool get wantKeepAlive => true;
  331. }