import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:news_app/constant/color_res.dart'; import 'package:news_app/model/topic_item_model.dart'; import 'package:news_app/ui/topic/main_topic_page.dart'; import 'package:news_app/ui/topic/topic_item_widget.dart'; import '../../constant/config.dart'; import '../../constant/size_res.dart'; import '../../provider/topic_detail_provider.dart'; import '../../util/device_util.dart'; import '../../util/share_util.dart'; import '../../widget/load_image.dart'; import '../../widget/my_txt.dart'; import '../../widget/right_action_widget.dart'; import '../video/comment_page.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/18 9:06 /// @description: class TopicDetailPage extends ConsumerStatefulWidget { final String contentId; const TopicDetailPage({super.key, required this.contentId}); @override ConsumerState createState() => _TopicDetailPageState(); } final topicDetailProvider = NotifierProvider(() { return TopicDetailProvider(); }); class _TopicDetailPageState extends ConsumerState { BuildContext? currentContext; bool _isWeChatInstalled = false; Future _checkWeChatInstallation() async { if (Platform.isAndroid) { final installed = await isWeChatInstalledOnlyAndroid(); setState(() => _isWeChatInstalled = installed); } else if (Platform.isIOS) { final installed = await fluwx.isWeChatInstalled; setState(() => _isWeChatInstalled = installed); } } @override void initState() { super.initState(); ref.read(topicDetailProvider.notifier).fetchTopicDetail(widget.contentId); _checkWeChatInstallation(); } Future shareAction(TopicRecordModel data) async { showModalBottomSheet( context: context, builder: (context) => SafeArea( child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ SizedBox(height: 10.h), Container( width: double.infinity, height: 80.h, decoration: BoxDecoration( borderRadius: BorderRadius.only( topLeft: Radius.circular(10.r), topRight: Radius.circular(10.r), ), color: Colors.white, ), child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ if (_isWeChatInstalled) GestureDetector( onTap: () { Navigator.pop(context); shareWeiXinUrl( title: data.shareDesc ?? "", url: data.shareUrl ?? "", ); }, child: Container( width: 100.w, height: 80.h, child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ LoadAssetImage( 'share_wxhy', width: 40.w, height: 40.h, ), SizedBox(height: 10.h), myTxt( text: "微信好友", color: Colors.black, fontSize: 12.sp, fontWeight: FontWeight.bold, ), ], ), ), ), if (_isWeChatInstalled) GestureDetector( onTap: () { Navigator.pop(context); shareWeiXinPYUrl( title: data.shareDesc ?? "", url: data.shareUrl ?? "", ); }, child: Container( width: 100.w, height: 80.h, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ LoadAssetImage( 'share_pyq', width: 40.w, height: 40.h, ), SizedBox(height: 10.h), myTxt( text: "朋友圈", color: Colors.black, fontSize: 12.sp, fontWeight: FontWeight.bold, ), ], ), ), ), GestureDetector( onTap: () { Navigator.pop(context); shareUrl( title: data.shareDesc ?? "", url: data.shareUrl ?? "", ); }, child: Container( width: 100.w, height: 80.h, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ LoadAssetImage( 'share_xtfx', width: 40.w, height: 40.h, ), SizedBox(height: 10.h), myTxt( text: "系统分享", color: Colors.black, fontSize: 12.sp, fontWeight: FontWeight.bold, ), ], ), ), ), ], ), ), ], ), ), ); } void longTapAction(String contentId) { showCupertinoDialog( context: context, builder: (context) => CupertinoAlertDialog( title: const Text("举报该评论"), content: const Text("您确定要执行此操作吗?"), actions: [ CupertinoDialogAction( child: const Text("取消"), onPressed: () => Navigator.pop(context), ), CupertinoDialogAction( isDestructiveAction: true, // 红色警示按钮 child: const Text("确定"), onPressed: () { ref .read(topicDetailProvider.notifier) .reportComment("topicArticle", contentId); Navigator.pop(context); Future.delayed(const Duration(milliseconds: 500), () { if (mounted && currentContext != null) { eventBus.fire('topicCall'); Navigator.of(currentContext!).pop(); } }); // 执行删除操作 }, ), ], ), ); } @override Widget build(BuildContext context) { currentContext = context; final data = ref.watch(topicDetailProvider); return Scaffold( appBar: AppBar( title: myTxt(text: '话题', fontSize: 18.sp, fontWeight: FontWeight.bold), centerTitle: true, // 移除阴影 scrolledUnderElevation: 0, actions: [ Padding( padding: EdgeInsets.only(right: horizontalPadding), child: RightActionWidget( isLike: data.isLiked ?? false, isFavorite: data.isFavorite ?? false, tap: (value) { if (value == 0) { ref .read(topicListProvider.notifier) .fetchTopicLike( contentId: data.contentId, current: data.isLiked ?? false, ); ref.read(topicDetailProvider.notifier).updateLike(); } else if (value == 1) { ref .read(topicListProvider.notifier) .fetchTopicFavorite( contentId: data.contentId, current: data.isFavorite ?? false, ); ref.read(topicDetailProvider.notifier).updateFavorite(); } else if (value == 2) { // shareUrl(title: data.content ?? "", url: data.shareUrl ?? ""); shareAction(data); if (uuid.isNotEmpty) { ref .read(topicDetailProvider.notifier) .fetchTopicShare(contentId: data.contentId); } } }, ), ), ], // 禁用滚动时的阴影变化 ), body: NestedScrollView( headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { return [ SliverToBoxAdapter( child: TopicItemWidget(longTapAction, isShow: true, item: data), ), SliverToBoxAdapter( child: Container( height: 8.h, width: screenWidth, color: colorF5F7FD, ), ), ]; }, body: Container( color: Colors.white, padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 12.h), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ myTxt( text: "评论${ref.watch(commentProvider(CommentType.topic)).total}", fontSize: 14.sp, color: color333333, fontWeight: FontWeight.bold, ), SizedBox(height: 12.h), Expanded( child: CommentPage( type: CommentType.topic, articleId: widget.contentId, ), ), ], ), ), ), ); } }