177 lines
5.9 KiB
Dart
177 lines
5.9 KiB
Dart
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/routes/app_pages.dart';
|
|
import 'package:news_getx/theme/app_colors.dart';
|
|
import 'package:news_getx/utils/date.dart';
|
|
|
|
/// 新闻行 Item
|
|
class NewsListWidget extends GetView<MainController> {
|
|
Widget _buildListItem(NewsItem item) {
|
|
return Container(
|
|
height: 170.h,
|
|
padding: EdgeInsets.all(20.w),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
// 左侧图
|
|
InkWell(
|
|
onTap: () {
|
|
Get.toNamed(AppRoutes.Detail, arguments: item);
|
|
},
|
|
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.zero,
|
|
child: Text(
|
|
item.author ?? '',
|
|
style: TextStyle(
|
|
fontFamily: 'Avenir',
|
|
fontWeight: FontWeight.normal,
|
|
color: AppColors.thirdElementText,
|
|
fontSize: 14.sp,
|
|
height: 1,
|
|
),
|
|
),
|
|
),
|
|
// 标题
|
|
InkWell(
|
|
onTap: () {
|
|
// 到详情页
|
|
Get.toNamed(AppRoutes.Detail, arguments: item);
|
|
},
|
|
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,
|
|
height: 1,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
Spacer(),
|
|
// 一行 3 列
|
|
Container(
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: <Widget>[
|
|
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.sp,
|
|
),
|
|
onTap: () {
|
|
print('查看更多...');
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// 这不是应该用ListView.build吗
|
|
return Obx(
|
|
() => controller.state.newsPageList == null
|
|
? Container()
|
|
: Column(
|
|
children:
|
|
controller.state.newsPageList!.items!.map<Widget>((item) {
|
|
// 新闻行
|
|
List<Widget> 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(),
|
|
),
|
|
);
|
|
}
|
|
}
|