video_detail_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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?.dispose();
  182. subscription?.close();
  183. super.dispose();
  184. }
  185. @override
  186. void deactivate() {
  187. super.deactivate();
  188. _betterPlayerController?.pause();
  189. }
  190. @override
  191. void initState() {
  192. super.initState();
  193. setImmersiveStatusBar();
  194. _checkWeChatInstallation();
  195. ref.read(videoDetailProvider.notifier).fetchVideoDetail(widget.param.id);
  196. if (widget.param.videoUrl?.isNotEmpty == true) {
  197. _initPlayer(widget.param.videoUrl);
  198. } else {
  199. subscription = ref.listenManual(videoDetailProvider, (pre, next) {
  200. if (pre?.url != next.url && next.url?.isNotEmpty == true) {
  201. _initPlayer(next.url);
  202. }
  203. });
  204. }
  205. // 监听 App 生命周期变化
  206. WidgetsBinding.instance.addObserver(this);
  207. }
  208. @override
  209. void didChangeAppLifecycleState(AppLifecycleState state) {
  210. super.didChangeAppLifecycleState(state);
  211. if (_betterPlayerController == null) return;
  212. if (state == AppLifecycleState.inactive ||
  213. state == AppLifecycleState.paused) {
  214. // App进入后台或锁屏,暂停播放
  215. _betterPlayerController?.pause();
  216. } else if (state == AppLifecycleState.resumed) {
  217. // App回到前台(是否继续播放可根据需求选择)如果希望回到前台自动播放,就取消注释
  218. // _betterPlayerController?.play();
  219. }
  220. }
  221. void _initPlayer(String? url) {
  222. if (url == null || url.isEmpty) {
  223. return;
  224. }
  225. _betterPlayerController?.dispose(); // dispose 旧的
  226. final dataSource = BetterPlayerDataSource(
  227. BetterPlayerDataSourceType.network,
  228. url,
  229. videoFormat: BetterPlayerVideoFormat.other, // 避免格式识别失败
  230. );
  231. _betterPlayerController = BetterPlayerController(
  232. BetterPlayerConfiguration(
  233. errorBuilder: (context, errorMessage) {
  234. return Center(
  235. child: myTxt(text: "视频加载失败", color: Colors.white, fontSize: 14.sp),
  236. );
  237. },
  238. autoPlay: true,
  239. aspectRatio: 16 / 9,
  240. fit: BoxFit.fitWidth,
  241. controlsConfiguration: const BetterPlayerControlsConfiguration(
  242. showControls: false,
  243. ),
  244. ),
  245. betterPlayerDataSource: dataSource,
  246. );
  247. }
  248. @override
  249. Widget build(BuildContext context) {
  250. final video = ref.watch(videoDetailProvider);
  251. return Material(
  252. color: Colors.black,
  253. child: Stack(
  254. fit: StackFit.expand,
  255. alignment: Alignment.centerRight,
  256. children: [
  257. if (_betterPlayerController != null && video.url?.isNotEmpty == true)
  258. ColoredBox(
  259. color: Colors.black,
  260. child: AspectRatio(
  261. aspectRatio: 16 / 9,
  262. child: BetterPlayer(controller: _betterPlayerController!),
  263. ),
  264. )
  265. else
  266. Center(
  267. child: myTxt(
  268. text: "视频加载中...",
  269. color: Colors.white,
  270. fontSize: 14.sp,
  271. ),
  272. ),
  273. Positioned(
  274. left: 20.w,
  275. top: 54.h,
  276. child: GestureDetector(
  277. onTap: () => context.pop(),
  278. child: Icon(Icons.arrow_back_ios, color: Colors.white),
  279. ),
  280. ),
  281. Positioned(
  282. right: 20.h,
  283. bottom: 150.h,
  284. child: Column(
  285. mainAxisAlignment: MainAxisAlignment.center,
  286. children: [
  287. SizedBox(height: 100.h),
  288. AuthGestureDetector(
  289. onTap: () {
  290. ref
  291. .read(videoDetailProvider.notifier)
  292. .likeVideo(video.isLiked ?? false);
  293. ref
  294. .read(recommendListProvider.notifier)
  295. .fetchVideoLike(
  296. videoId: widget.param.id,
  297. current: video.isLiked ?? false,
  298. );
  299. },
  300. child: Icon(
  301. Icons.favorite,
  302. color: video.isLiked == true ? Colors.red : Colors.white,
  303. size: 25.sp,
  304. ),
  305. ),
  306. myTxt(
  307. text: video.likeCount.toSafeString,
  308. color: Colors.white,
  309. fontSize: 12.sp,
  310. ),
  311. SizedBox(height: 15.h),
  312. AuthGestureDetector(
  313. onTap: () {
  314. String? contentId = video.contentId;
  315. String? title = video.title;
  316. //1.活动中的视频 2.视频Tab进来的普通视频
  317. _showBottomCommentDialog(
  318. context,
  319. contentId,
  320. title,
  321. CommentType.video,
  322. );
  323. },
  324. child: Icon(Icons.message, color: Colors.white, size: 25.sp),
  325. ),
  326. myTxt(
  327. text: video.commentCount.toSafeString,
  328. color: Colors.white,
  329. fontSize: 12.sp,
  330. ),
  331. SizedBox(height: 15.h),
  332. AuthGestureDetector(
  333. onTap: () {
  334. ref
  335. .read(videoDetailProvider.notifier)
  336. .favoriteVideo(video.isFavorite ?? false);
  337. ref
  338. .read(recommendListProvider.notifier)
  339. .fetchVideoFavorite(
  340. videoId: widget.param.id,
  341. current: video.isFavorite ?? false,
  342. );
  343. },
  344. child: Icon(
  345. Icons.star,
  346. color: video.isFavorite == true ? Colors.red : Colors.white,
  347. size: 25.sp,
  348. ),
  349. ),
  350. myTxt(
  351. text: video.favoriteCount.toSafeString,
  352. color: Colors.white,
  353. fontSize: 12.sp,
  354. ),
  355. SizedBox(height: 15.h),
  356. GestureDetector(
  357. onTap: () {
  358. // shareUrl(
  359. // title: video.shareDesc ?? "",
  360. // url: video.shareUrl ?? "",
  361. // );
  362. shareAction(video);
  363. if (uuid.isNotEmpty) {
  364. ref
  365. .read(recommendListProvider.notifier)
  366. .fetchVideoShare(contentId: widget.param.id);
  367. }
  368. },
  369. child: Icon(
  370. Icons.screen_share,
  371. color: Colors.white,
  372. size: 25.sp,
  373. ),
  374. ),
  375. myTxt(
  376. text: video.shareCount.toSafeString,
  377. color: Colors.white,
  378. fontSize: 12.sp,
  379. ),
  380. SizedBox(height: 15.h),
  381. ],
  382. ),
  383. ),
  384. ],
  385. ),
  386. );
  387. }
  388. void _showBottomCommentDialog(
  389. BuildContext ctx,
  390. String? contentId,
  391. String? title,
  392. CommentType commentType,
  393. ) {
  394. showModalBottomSheet(
  395. useSafeArea: true,
  396. backgroundColor: Colors.white,
  397. context: ctx,
  398. isScrollControlled: true,
  399. // 允许内容滚动并控制高度
  400. builder: (context) {
  401. return Container(
  402. padding: EdgeInsets.all(horizontalPadding),
  403. height: screenHeight * 0.7,
  404. child: Column(
  405. crossAxisAlignment: CrossAxisAlignment.start,
  406. children: [
  407. SizedBox(height: 15.h),
  408. myTxt(text: title ?? "", color: color333333, fontSize: 15.sp),
  409. Container(
  410. height: 0.5,
  411. width: screenWidth,
  412. color: colorE5E5E5,
  413. margin: EdgeInsets.symmetric(vertical: 10.h),
  414. ),
  415. Expanded(
  416. child: CommentPage(
  417. type: commentType,
  418. articleId: contentId ?? "",
  419. ),
  420. ),
  421. ],
  422. ),
  423. );
  424. },
  425. );
  426. }
  427. }