news_detail_page.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
  6. import 'package:news_app/constant/config.dart';
  7. import 'package:news_app/constant/size_res.dart';
  8. import 'package:news_app/model/news_detail_model.dart';
  9. import 'package:news_app/util/log.util.dart';
  10. import 'package:news_app/util/share_util.dart';
  11. import 'package:news_app/widget/load_image.dart';
  12. import 'package:news_app/widget/right_action_widget.dart';
  13. import '../../constant/color_res.dart';
  14. import '../../provider/news_detail_provider.dart';
  15. import '../../util/device_util.dart';
  16. import '../../util/time_util.dart';
  17. import '../../widget/my_txt.dart';
  18. import '../video/comment_page.dart';
  19. class NewsDetailPage extends ConsumerStatefulWidget {
  20. final String contentId;
  21. const NewsDetailPage(this.contentId, {super.key});
  22. // const NewsDetailPage({super.key});
  23. @override
  24. ConsumerState<NewsDetailPage> createState() => _NewsDetailPageState();
  25. }
  26. final newsDetailProvider =
  27. NotifierProvider.autoDispose<NewsDetailProvider, NewsDetailModel>(
  28. () => NewsDetailProvider(),
  29. );
  30. class _NewsDetailPageState extends ConsumerState<NewsDetailPage> {
  31. bool _isWeChatInstalled = false;
  32. @override
  33. void initState() {
  34. super.initState();
  35. ref.read(newsDetailProvider.notifier).fetchNewsDetail(widget.contentId);
  36. _checkWeChatInstallation();
  37. }
  38. Future<void> _checkWeChatInstallation() async {
  39. if (Platform.isAndroid) {
  40. final installed = await isWeChatInstalledOnlyAndroid();
  41. setState(() => _isWeChatInstalled = installed);
  42. } else if (Platform.isIOS) {
  43. final installed = await fluwx.isWeChatInstalled;
  44. setState(() => _isWeChatInstalled = installed);
  45. }
  46. }
  47. Future<void> shareAction(NewsDetailModel data) async {
  48. showModalBottomSheet(
  49. context: context,
  50. builder:
  51. (context) => SafeArea(
  52. child: Column(
  53. mainAxisSize: MainAxisSize.min,
  54. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  55. children: [
  56. SizedBox(height: 10.h),
  57. Container(
  58. width: double.infinity,
  59. height: 80.h,
  60. decoration: BoxDecoration(
  61. borderRadius: BorderRadius.only(
  62. topLeft: Radius.circular(10.r),
  63. topRight: Radius.circular(10.r),
  64. ),
  65. color: Colors.white,
  66. ),
  67. child: Row(
  68. crossAxisAlignment: CrossAxisAlignment.center,
  69. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  70. children: [
  71. if (_isWeChatInstalled)
  72. GestureDetector(
  73. onTap: () {
  74. Navigator.pop(context);
  75. shareWeiXinUrl(
  76. title: data.shareDesc ?? "",
  77. url: data.shareUrl ?? "",
  78. );
  79. },
  80. child: Container(
  81. width: 100.w,
  82. height: 80.h,
  83. child: Column(
  84. crossAxisAlignment: CrossAxisAlignment.center,
  85. mainAxisAlignment: MainAxisAlignment.center,
  86. children: [
  87. LoadAssetImage(
  88. 'share_wxhy',
  89. width: 40.w,
  90. height: 40.h,
  91. ),
  92. SizedBox(height: 10.h),
  93. myTxt(
  94. text: "微信好友",
  95. color: Colors.black,
  96. fontSize: 12.sp,
  97. fontWeight: FontWeight.bold,
  98. ),
  99. ],
  100. ),
  101. ),
  102. ),
  103. if (_isWeChatInstalled)
  104. GestureDetector(
  105. onTap: () {
  106. Navigator.pop(context);
  107. shareWeiXinPYUrl(
  108. title: data.shareDesc ?? "",
  109. url: data.shareUrl ?? "",
  110. );
  111. },
  112. child: Container(
  113. width: 100.w,
  114. height: 80.h,
  115. child: Column(
  116. mainAxisAlignment: MainAxisAlignment.center,
  117. children: [
  118. LoadAssetImage(
  119. 'share_pyq',
  120. width: 40.w,
  121. height: 40.h,
  122. ),
  123. SizedBox(height: 10.h),
  124. myTxt(
  125. text: "朋友圈",
  126. color: Colors.black,
  127. fontSize: 12.sp,
  128. fontWeight: FontWeight.bold,
  129. ),
  130. ],
  131. ),
  132. ),
  133. ),
  134. GestureDetector(
  135. onTap: () {
  136. Navigator.pop(context);
  137. shareUrl(
  138. title: data.shareDesc ?? "",
  139. url: data.shareUrl ?? "",
  140. );
  141. },
  142. child: Container(
  143. width: 100.w,
  144. height: 80.h,
  145. child: Column(
  146. mainAxisAlignment: MainAxisAlignment.center,
  147. children: [
  148. LoadAssetImage(
  149. 'share_xtfx',
  150. width: 40.w,
  151. height: 40.h,
  152. ),
  153. SizedBox(height: 10.h),
  154. myTxt(
  155. text: "系统分享",
  156. color: Colors.black,
  157. fontSize: 12.sp,
  158. fontWeight: FontWeight.bold,
  159. ),
  160. ],
  161. ),
  162. ),
  163. ),
  164. // if (_isWeChatInstalled)
  165. // ListTile(
  166. // leading: const Icon(Icons.chat, color: Colors.green),
  167. // title: const Text('微信好友'),
  168. // onTap: () {
  169. // Navigator.pop(context);
  170. // shareWeiXinUrl(
  171. // title: data.shareDesc ?? "",
  172. // url: data.shareUrl ?? "",
  173. // );
  174. // },
  175. // ),
  176. // if (_isWeChatInstalled)
  177. // ListTile(
  178. // leading: const Icon(Icons.chat, color: Colors.green),
  179. // title: const Text('朋友圈'),
  180. // onTap: () {
  181. // Navigator.pop(context);
  182. // shareWeiXinPYUrl(
  183. // title: data.shareDesc ?? "",
  184. // url: data.shareUrl ?? "",
  185. // );
  186. // },
  187. // ),
  188. // ListTile(
  189. // leading: const Icon(Icons.share),
  190. // title: const Text('系统分享'),
  191. // onTap: () {
  192. // Navigator.pop(context);
  193. // shareUrl(
  194. // title: data.shareDesc ?? "",
  195. // url: data.shareUrl ?? "",
  196. // );
  197. // },
  198. // ),
  199. ],
  200. ),
  201. ),
  202. ],
  203. ),
  204. ),
  205. );
  206. }
  207. @override
  208. Widget build(BuildContext context) {
  209. consoleLog("新闻content id:${widget.contentId}");
  210. final data = ref.watch(newsDetailProvider);
  211. return Scaffold(
  212. backgroundColor: Colors.white,
  213. appBar: AppBar(
  214. scrolledUnderElevation: 0,
  215. title: myTxt(
  216. text: "文章详情",
  217. color: Colors.black,
  218. fontSize: 18.sp,
  219. fontWeight: FontWeight.bold,
  220. ),
  221. leading: IconButton(
  222. icon: Icon(Icons.arrow_back_ios, color: Colors.black),
  223. onPressed: () {
  224. Navigator.pop(context);
  225. },
  226. ),
  227. actions: [
  228. Padding(
  229. padding: EdgeInsets.only(right: horizontalPadding),
  230. child: RightActionWidget(
  231. isLike: data.isLiked ?? false,
  232. isFavorite: data.isFavorite ?? false,
  233. tap: (value) {
  234. if (value == 0) {
  235. ref
  236. .read(newsDetailProvider.notifier)
  237. .fetchNewsLike(
  238. contentId: widget.contentId,
  239. current: data.isLiked ?? false,
  240. );
  241. } else if (value == 1) {
  242. ref
  243. .read(newsDetailProvider.notifier)
  244. .fetchNewsFavorite(
  245. contentId: widget.contentId,
  246. current: data.isFavorite ?? false,
  247. );
  248. } else if (value == 2) {
  249. shareAction(data);
  250. if (uuid.isNotEmpty) {
  251. ref
  252. .read(newsDetailProvider.notifier)
  253. .fetchNewsShare(contentId: data.contentId);
  254. }
  255. }
  256. },
  257. ),
  258. ),
  259. ],
  260. ),
  261. body: Padding(
  262. padding: EdgeInsets.symmetric(horizontal: horizontalPadding),
  263. child: NestedScrollView(
  264. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  265. return [
  266. SliverToBoxAdapter(
  267. child: Column(
  268. crossAxisAlignment: CrossAxisAlignment.start,
  269. children: [
  270. myTxt(
  271. text: data.title ?? '',
  272. maxLines: 100,
  273. color: Colors.black,
  274. fontSize: 24.sp,
  275. fontWeight: FontWeight.bold,
  276. textAlign: TextAlign.left,
  277. ),
  278. SizedBox(height: 10.w),
  279. Row(
  280. crossAxisAlignment: CrossAxisAlignment.start,
  281. children: [
  282. Container(
  283. decoration: BoxDecoration(
  284. color: Color(0xFFF3F5FB),
  285. borderRadius: BorderRadius.circular(1.w),
  286. ),
  287. alignment: Alignment.center,
  288. child: myTxt(
  289. text: data.sourceType == 2 ? "转载": "原创",
  290. color: Color(0xFF666666),
  291. fontSize: 12,
  292. fontWeight: FontWeight.bold,
  293. ),
  294. ),
  295. SizedBox(width: 8.w),
  296. myTxt(
  297. text: data.author ?? "",
  298. color: Color(0xFF333333),
  299. fontSize: 12,
  300. ),
  301. SizedBox(width: 8.w),
  302. myTxt(
  303. text:
  304. "${TimeUtil.formatDataTime(data.publishDate, "yyyy年MM月dd日 HH:mm")}",
  305. color: Color(0xFF999999),
  306. fontSize: 12,
  307. ),
  308. ],
  309. ),
  310. SizedBox(height: 10.w),
  311. HtmlWidget(data.contentHtml ?? ""),
  312. Builder(
  313. builder: (context) {
  314. int commentCount =
  315. ref
  316. .watch(commentProvider(CommentType.news))
  317. .total ??
  318. 0;
  319. return commentCount == 0
  320. ? const SizedBox.shrink()
  321. : myTxt(
  322. text:
  323. "评论${ref.watch(commentProvider(CommentType.news)).total}",
  324. fontSize: 14.sp,
  325. color: color333333,
  326. fontWeight: FontWeight.bold,
  327. );
  328. },
  329. ),
  330. SizedBox(height: 12.h),
  331. ],
  332. ),
  333. ),
  334. ];
  335. },
  336. body: Container(
  337. color: Colors.white,
  338. child: CommentPage(
  339. type: CommentType.news,
  340. articleId: widget.contentId,
  341. ),
  342. ),
  343. ),
  344. ),
  345. );
  346. }
  347. }