import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:news_getx/data/model/news.dart'; import 'package:news_getx/modules/main/main_controller.dart'; import 'package:news_getx/modules/main/widgets/ad.dart'; import 'package:news_getx/modules/widgets/image.dart'; import 'package:news_getx/theme/app_colors.dart'; import 'package:news_getx/utils/date.dart'; /// 新闻行 Item class NewsListWidget extends GetView { Widget _buildListItem(NewsItem item) { return Container( height: 161.h, padding: EdgeInsets.all(20.w), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ // 左侧图 InkWell( onTap: () { // 到详情页 }, child: SizedBox( width: 121.w, height: 121.w, child: netImageCached( item.thumbnail ?? "", width: 121.w, height: 121.w, ), ), ), // 右侧内容 SizedBox( width: 194.w, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // 作者 Container( margin: EdgeInsets.all(0), child: Text( item.author ?? '', style: TextStyle( fontFamily: 'Avenir', fontWeight: FontWeight.normal, color: AppColors.thirdElementText, fontSize: 14.sp, ), ), ), // 标题 InkWell( onTap: () { // 到详情页 }, child: Container( margin: EdgeInsets.only(top: 10.h), child: Text( item.title ?? '', overflow: TextOverflow.clip, maxLines: 3, style: TextStyle( fontFamily: 'Montserrat', fontWeight: FontWeight.w500, color: AppColors.primaryText, fontSize: 16.sp, ), ), ), ), Spacer(), // 一行 3 列 Container( child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ ConstrainedBox( // 分类 constraints: BoxConstraints(maxWidth: 60.w), child: Text( item.category ?? "", style: TextStyle( fontFamily: 'Avenir', fontWeight: FontWeight.normal, color: AppColors.secondaryElementText, fontSize: 14.sp, height: 1, ), overflow: TextOverflow.clip, maxLines: 1, ), ), // 添加时间 Container( width: 15.w, ), ConstrainedBox( constraints: BoxConstraints(maxWidth: 100.w), child: Text( '• ${timeLineFormat(item.addtime ?? DateTime(0))}', style: TextStyle( fontFamily: 'Avenir', fontWeight: FontWeight.normal, color: AppColors.thirdElementText, fontSize: 14.sp, height: 1, ), overflow: TextOverflow.clip, maxLines: 1, ), ), // 占满剩余空间 Spacer(), // 更多 InkWell( child: Icon( Icons.more_horiz, color: AppColors.primaryText, size: 24, ), onTap: () { print('查看更多...'); }, ), ], ), ), ], ), ), ], ), ); } @override Widget build(BuildContext context) { // 这不是应该用ListView.build吗 return Obx( () => controller.state.newsPageList == null ? Container() : Column( children: controller.state.newsPageList!.items!.map((item) { // 新闻行 List widgets = [ _buildListItem(item), Divider(height: 1), ]; // 每 5 条 显示广告 int index = controller.state.newsPageList!.items!.indexOf(item); if ((index + 1) % 5 == 0) { widgets.addAll([ AdWidget(), Divider(height: 1), ]); } return Column( children: widgets, ); }).toList(), ), ); } }