| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- 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<NewsDetailPage> createState() => _NewsDetailPageState();
- }
- final newsDetailProvider =
- NotifierProvider.autoDispose<NewsDetailProvider, NewsDetailModel>(
- () => NewsDetailProvider(),
- );
- class _NewsDetailPageState extends ConsumerState<NewsDetailPage> {
- bool _isWeChatInstalled = false;
- @override
- void initState() {
- super.initState();
- ref.read(newsDetailProvider.notifier).fetchNewsDetail(widget.contentId);
- _checkWeChatInstallation();
- }
- Future<void> _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<void> 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,
- ),
- ),
- ),
- ),
- );
- }
- }
|