import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; import 'package:news_app/constant/config.dart'; import 'package:news_app/constant/size_res.dart'; import 'package:news_app/model/news_detail_model.dart'; import 'package:news_app/util/log.util.dart'; import 'package:news_app/util/share_util.dart'; import 'package:news_app/widget/load_image.dart'; import 'package:news_app/widget/right_action_widget.dart'; import '../../constant/color_res.dart'; import '../../provider/news_detail_provider.dart'; import '../../util/device_util.dart'; import '../../util/time_util.dart'; import '../../widget/my_txt.dart'; import '../video/comment_page.dart'; class NewsDetailPage extends ConsumerStatefulWidget { final String contentId; const NewsDetailPage(this.contentId, {super.key}); // const NewsDetailPage({super.key}); @override ConsumerState createState() => _NewsDetailPageState(); } final newsDetailProvider = NotifierProvider.autoDispose( () => NewsDetailProvider(), ); class _NewsDetailPageState extends ConsumerState { bool _isWeChatInstalled = false; @override void initState() { super.initState(); ref.read(newsDetailProvider.notifier).fetchNewsDetail(widget.contentId); _checkWeChatInstallation(); } 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); } } Future shareAction(NewsDetailModel 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, ), ], ), ), ), // if (_isWeChatInstalled) // ListTile( // leading: const Icon(Icons.chat, color: Colors.green), // title: const Text('微信好友'), // onTap: () { // Navigator.pop(context); // shareWeiXinUrl( // title: data.shareDesc ?? "", // url: data.shareUrl ?? "", // ); // }, // ), // if (_isWeChatInstalled) // ListTile( // leading: const Icon(Icons.chat, color: Colors.green), // title: const Text('朋友圈'), // onTap: () { // Navigator.pop(context); // shareWeiXinPYUrl( // title: data.shareDesc ?? "", // url: data.shareUrl ?? "", // ); // }, // ), // ListTile( // leading: const Icon(Icons.share), // title: const Text('系统分享'), // onTap: () { // Navigator.pop(context); // shareUrl( // title: data.shareDesc ?? "", // url: data.shareUrl ?? "", // ); // }, // ), ], ), ), ], ), ), ); } @override Widget build(BuildContext context) { consoleLog("新闻content id:${widget.contentId}"); final data = ref.watch(newsDetailProvider); return Scaffold( backgroundColor: Colors.white, appBar: AppBar( scrolledUnderElevation: 0, title: myTxt( text: "文章详情", color: Colors.black, fontSize: 18.sp, fontWeight: FontWeight.bold, ), leading: IconButton( icon: Icon(Icons.arrow_back_ios, color: Colors.black), onPressed: () { Navigator.pop(context); }, ), actions: [ Padding( padding: EdgeInsets.only(right: horizontalPadding), child: RightActionWidget( isLike: data.isLiked ?? false, isFavorite: data.isFavorite ?? false, tap: (value) { if (value == 0) { ref .read(newsDetailProvider.notifier) .fetchNewsLike( contentId: widget.contentId, current: data.isLiked ?? false, ); } else if (value == 1) { ref .read(newsDetailProvider.notifier) .fetchNewsFavorite( contentId: widget.contentId, current: data.isFavorite ?? false, ); } else if (value == 2) { shareAction(data); if (uuid.isNotEmpty) { ref .read(newsDetailProvider.notifier) .fetchNewsShare(contentId: data.contentId); } } }, ), ), ], ), body: Padding( padding: EdgeInsets.symmetric(horizontal: horizontalPadding), child: NestedScrollView( headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { return [ SliverToBoxAdapter( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ myTxt( text: data.title ?? '', maxLines: 100, color: Colors.black, fontSize: 24.sp, fontWeight: FontWeight.bold, textAlign: TextAlign.left, ), SizedBox(height: 10.w), Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( decoration: BoxDecoration( color: Color(0xFFF3F5FB), borderRadius: BorderRadius.circular(1.w), ), alignment: Alignment.center, child: myTxt( text: data.sourceType == 2 ? "转载": "原创", color: Color(0xFF666666), fontSize: 12, fontWeight: FontWeight.bold, ), ), SizedBox(width: 8.w), myTxt( text: data.author ?? "", color: Color(0xFF333333), fontSize: 12, ), SizedBox(width: 8.w), myTxt( text: "${TimeUtil.formatDataTime(data.publishDate, "yyyy年MM月dd日 HH:mm")}", color: Color(0xFF999999), fontSize: 12, ), ], ), SizedBox(height: 10.w), HtmlWidget(data.contentHtml ?? ""), Builder( builder: (context) { int commentCount = ref .watch(commentProvider(CommentType.news)) .total ?? 0; return commentCount == 0 ? const SizedBox.shrink() : myTxt( text: "评论${ref.watch(commentProvider(CommentType.news)).total}", fontSize: 14.sp, color: color333333, fontWeight: FontWeight.bold, ); }, ), SizedBox(height: 12.h), ], ), ), ]; }, body: Container( color: Colors.white, child: CommentPage( type: CommentType.news, articleId: widget.contentId, ), ), ), ), ); } }