video_detail_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/constant/config.dart';
  8. import 'package:news_app/constant/size_res.dart';
  9. import 'package:news_app/extension/base.dart';
  10. import 'package:news_app/model/video_new_model.dart';
  11. import 'package:news_app/ui/video/comment_page.dart';
  12. import 'package:news_app/ui/video/video_recommend_list_page.dart';
  13. import 'package:news_app/util/device_util.dart';
  14. import 'package:news_app/util/share_util.dart';
  15. import 'package:news_app/widget/my_txt.dart';
  16. import '../../constant/color_res.dart';
  17. import '../../provider/video_detail_provider.dart';
  18. import '../../util/theme_util.dart';
  19. import '../../widget/auth_gesture_detector.dart';
  20. import '../../widget/load_image.dart';
  21. /// @author: bo.zeng
  22. /// @email: cnhbwds@gmail.com
  23. /// @date: 2025 2025/4/9 16:00
  24. /// @description:
  25. class VideoParam {
  26. final String id;
  27. final String? videoUrl;
  28. VideoParam({required this.id, this.videoUrl});
  29. }
  30. class VideoDetailPage extends ConsumerStatefulWidget {
  31. final VideoParam param;
  32. const VideoDetailPage({super.key, required this.param});
  33. @override
  34. ConsumerState<VideoDetailPage> createState() => _VideoDetailPageState();
  35. }
  36. final videoDetailProvider =
  37. NotifierProvider<VideoDetailProvider, VideoNewModel>(
  38. () => VideoDetailProvider(),
  39. );
  40. class _VideoDetailPageState extends ConsumerState<VideoDetailPage>
  41. with WidgetsBindingObserver {
  42. BetterPlayerController? _betterPlayerController;
  43. ProviderSubscription? subscription;
  44. bool _isWeChatInstalled = false;
  45. Future<void> _checkWeChatInstallation() async {
  46. if (Platform.isAndroid) {
  47. final installed = await isWeChatInstalledOnlyAndroid();
  48. setState(() => _isWeChatInstalled = installed);
  49. } else if (Platform.isIOS) {
  50. final installed = await fluwx.isWeChatInstalled;
  51. setState(() => _isWeChatInstalled = installed);
  52. }
  53. }
  54. Future<void> shareAction(VideoNewModel data) async {
  55. showModalBottomSheet(
  56. context: context,
  57. builder:
  58. (context) => SafeArea(
  59. child: Column(
  60. mainAxisSize: MainAxisSize.min,
  61. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  62. children: [
  63. SizedBox(height: 10.h),
  64. Container(
  65. width: double.infinity,
  66. height: 80.h,
  67. decoration: BoxDecoration(
  68. borderRadius: BorderRadius.only(
  69. topLeft: Radius.circular(10.r),
  70. topRight: Radius.circular(10.r),
  71. ),
  72. color: Colors.white,
  73. ),
  74. child: Row(
  75. crossAxisAlignment: CrossAxisAlignment.center,
  76. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  77. children: [
  78. if (_isWeChatInstalled)
  79. GestureDetector(
  80. onTap: () {
  81. Navigator.pop(context);
  82. shareWeiXinUrl(
  83. title: data.shareDesc ?? "",
  84. url: data.shareUrl ?? "",
  85. );
  86. },
  87. child: Container(
  88. width: 100.w,
  89. height: 80.h,
  90. child: Column(
  91. crossAxisAlignment: CrossAxisAlignment.center,
  92. mainAxisAlignment: MainAxisAlignment.center,
  93. children: [
  94. LoadAssetImage(
  95. 'share_wxhy',
  96. width: 40.w,
  97. height: 40.h,
  98. ),
  99. SizedBox(height: 10.h),
  100. myTxt(
  101. text: "微信好友",
  102. color: Colors.black,
  103. fontSize: 12.sp,
  104. fontWeight: FontWeight.bold,
  105. ),
  106. ],
  107. ),
  108. ),
  109. ),
  110. if (_isWeChatInstalled)
  111. GestureDetector(
  112. onTap: () {
  113. Navigator.pop(context);
  114. shareWeiXinPYUrl(
  115. title: data.shareDesc ?? "",
  116. url: data.shareUrl ?? "",
  117. );
  118. },
  119. child: Container(
  120. width: 100.w,
  121. height: 80.h,
  122. child: Column(
  123. mainAxisAlignment: MainAxisAlignment.center,
  124. children: [
  125. LoadAssetImage(
  126. 'share_pyq',
  127. width: 40.w,
  128. height: 40.h,
  129. ),
  130. SizedBox(height: 10.h),
  131. myTxt(
  132. text: "朋友圈",
  133. color: Colors.black,
  134. fontSize: 12.sp,
  135. fontWeight: FontWeight.bold,
  136. ),
  137. ],
  138. ),
  139. ),
  140. ),
  141. GestureDetector(
  142. onTap: () {
  143. Navigator.pop(context);
  144. shareUrl(
  145. title: data.shareDesc ?? "",
  146. url: data.shareUrl ?? "",
  147. );
  148. },
  149. child: Container(
  150. width: 100.w,
  151. height: 80.h,
  152. child: Column(
  153. mainAxisAlignment: MainAxisAlignment.center,
  154. children: [
  155. LoadAssetImage(
  156. 'share_xtfx',
  157. width: 40.w,
  158. height: 40.h,
  159. ),
  160. SizedBox(height: 10.h),
  161. myTxt(
  162. text: "系统分享",
  163. color: Colors.black,
  164. fontSize: 12.sp,
  165. fontWeight: FontWeight.bold,
  166. ),
  167. ],
  168. ),
  169. ),
  170. ),
  171. ],
  172. ),
  173. ),
  174. ],
  175. ),
  176. ),
  177. );
  178. }
  179. @override
  180. void dispose() {
  181. _betterPlayerController?.pause();
  182. _betterPlayerController?.dispose();
  183. subscription?.close();
  184. super.dispose();
  185. }
  186. @override
  187. void initState() {
  188. super.initState();
  189. setImmersiveStatusBar();
  190. _checkWeChatInstallation();
  191. ref.read(videoDetailProvider.notifier).fetchVideoDetail(widget.param.id);
  192. if (widget.param.videoUrl?.isNotEmpty == true) {
  193. _initPlayer(widget.param.videoUrl);
  194. } else {
  195. subscription = ref.listenManual(videoDetailProvider, (pre, next) {
  196. if (pre?.url != next.url && next.url?.isNotEmpty == true) {
  197. _initPlayer(next.url);
  198. }
  199. });
  200. }
  201. // 监听 App 生命周期变化
  202. WidgetsBinding.instance.addObserver(this);
  203. }
  204. @override
  205. void didChangeAppLifecycleState(AppLifecycleState state) {
  206. super.didChangeAppLifecycleState(state);
  207. if (_betterPlayerController == null) return;
  208. if (state == AppLifecycleState.inactive ||
  209. state == AppLifecycleState.paused) {
  210. // App进入后台或锁屏,暂停播放
  211. _betterPlayerController?.pause();
  212. } else if (state == AppLifecycleState.resumed) {
  213. // App回到前台(是否继续播放可根据需求选择)如果希望回到前台自动播放,就取消注释
  214. // _betterPlayerController?.play();
  215. }
  216. }
  217. void _initPlayer(String? url) {
  218. if (url == null || url.isEmpty) {
  219. return;
  220. }
  221. _betterPlayerController?.dispose(); // dispose 旧的
  222. final dataSource = BetterPlayerDataSource(
  223. BetterPlayerDataSourceType.network,
  224. url,
  225. videoFormat: BetterPlayerVideoFormat.other, // 避免格式识别失败
  226. );
  227. _betterPlayerController = BetterPlayerController(
  228. BetterPlayerConfiguration(
  229. errorBuilder: (context, errorMessage) {
  230. return Center(
  231. child: myTxt(text: "视频加载失败", color: Colors.white, fontSize: 14.sp),
  232. );
  233. },
  234. autoPlay: true,
  235. aspectRatio: 16 / 9,
  236. fit: BoxFit.fitWidth,
  237. controlsConfiguration: const BetterPlayerControlsConfiguration(
  238. showControls: false,
  239. ),
  240. ),
  241. betterPlayerDataSource: dataSource,
  242. );
  243. }
  244. @override
  245. Widget build(BuildContext context) {
  246. final video = ref.watch(videoDetailProvider);
  247. return Material(
  248. color: Colors.black,
  249. child: Stack(
  250. fit: StackFit.expand,
  251. alignment: Alignment.centerRight,
  252. children: [
  253. if (_betterPlayerController != null && video.url?.isNotEmpty == true)
  254. ColoredBox(
  255. color: Colors.black,
  256. child: AspectRatio(
  257. aspectRatio: 16 / 9,
  258. child: BetterPlayer(controller: _betterPlayerController!),
  259. ),
  260. )
  261. else
  262. Center(
  263. child: myTxt(
  264. text: "视频加载中...",
  265. color: Colors.white,
  266. fontSize: 14.sp,
  267. ),
  268. ),
  269. Positioned(
  270. left: 20.w,
  271. top: 54.h,
  272. child: GestureDetector(
  273. onTap: () => context.pop(),
  274. child: Icon(Icons.arrow_back_ios, color: Colors.white),
  275. ),
  276. ),
  277. Positioned(
  278. right: 20.h,
  279. bottom: 150.h,
  280. child: Column(
  281. mainAxisAlignment: MainAxisAlignment.center,
  282. children: [
  283. SizedBox(height: 100.h),
  284. AuthGestureDetector(
  285. onTap: () {
  286. ref
  287. .read(videoDetailProvider.notifier)
  288. .likeVideo(video.isLiked ?? false);
  289. ref
  290. .read(recommendListProvider.notifier)
  291. .fetchVideoLike(
  292. videoId: widget.param.id,
  293. current: video.isLiked ?? false,
  294. );
  295. },
  296. child: Icon(
  297. Icons.favorite,
  298. color: video.isLiked == true ? Colors.red : Colors.white,
  299. size: 25.sp,
  300. ),
  301. ),
  302. myTxt(
  303. text: video.likeCount.toSafeString,
  304. color: Colors.white,
  305. fontSize: 12.sp,
  306. ),
  307. SizedBox(height: 15.h),
  308. AuthGestureDetector(
  309. onTap: () {
  310. String? contentId = video.contentId;
  311. String? title = video.title;
  312. //1.活动中的视频 2.视频Tab进来的普通视频
  313. _showBottomCommentDialog(
  314. context,
  315. contentId,
  316. title,
  317. CommentType.video,
  318. );
  319. },
  320. child: Icon(Icons.message, color: Colors.white, size: 25.sp),
  321. ),
  322. myTxt(
  323. text: video.commentCount.toSafeString,
  324. color: Colors.white,
  325. fontSize: 12.sp,
  326. ),
  327. SizedBox(height: 15.h),
  328. AuthGestureDetector(
  329. onTap: () {
  330. ref
  331. .read(videoDetailProvider.notifier)
  332. .favoriteVideo(video.isFavorite ?? false);
  333. ref
  334. .read(recommendListProvider.notifier)
  335. .fetchVideoFavorite(
  336. videoId: widget.param.id,
  337. current: video.isFavorite ?? false,
  338. );
  339. },
  340. child: Icon(
  341. Icons.star,
  342. color: video.isFavorite == true ? Colors.red : Colors.white,
  343. size: 25.sp,
  344. ),
  345. ),
  346. myTxt(
  347. text: video.favoriteCount.toSafeString,
  348. color: Colors.white,
  349. fontSize: 12.sp,
  350. ),
  351. SizedBox(height: 15.h),
  352. GestureDetector(
  353. onTap: () {
  354. // shareUrl(
  355. // title: video.shareDesc ?? "",
  356. // url: video.shareUrl ?? "",
  357. // );
  358. shareAction(video);
  359. if (uuid.isNotEmpty) {
  360. ref
  361. .read(recommendListProvider.notifier)
  362. .fetchVideoShare(contentId: widget.param.id);
  363. }
  364. },
  365. child: Icon(
  366. Icons.screen_share,
  367. color: Colors.white,
  368. size: 25.sp,
  369. ),
  370. ),
  371. myTxt(
  372. text: video.shareCount.toSafeString,
  373. color: Colors.white,
  374. fontSize: 12.sp,
  375. ),
  376. SizedBox(height: 15.h),
  377. ],
  378. ),
  379. ),
  380. ],
  381. ),
  382. );
  383. }
  384. void _showBottomCommentDialog(
  385. BuildContext ctx,
  386. String? contentId,
  387. String? title,
  388. CommentType commentType,
  389. ) {
  390. showModalBottomSheet(
  391. useSafeArea: true,
  392. backgroundColor: Colors.white,
  393. context: ctx,
  394. isScrollControlled: true,
  395. // 允许内容滚动并控制高度
  396. builder: (context) {
  397. return Container(
  398. padding: EdgeInsets.all(horizontalPadding),
  399. height: screenHeight * 0.7,
  400. child: Column(
  401. crossAxisAlignment: CrossAxisAlignment.start,
  402. children: [
  403. SizedBox(height: 15.h),
  404. myTxt(text: title ?? "", color: color333333, fontSize: 15.sp),
  405. Container(
  406. height: 0.5,
  407. width: screenWidth,
  408. color: colorE5E5E5,
  409. margin: EdgeInsets.symmetric(vertical: 10.h),
  410. ),
  411. Expanded(
  412. child: CommentPage(
  413. type: commentType,
  414. articleId: contentId ?? "",
  415. ),
  416. ),
  417. ],
  418. ),
  419. );
  420. },
  421. );
  422. }
  423. }