Browse Source

refactor(home): 重构首页内容卡片和标签页结构

- 将内容卡片拆分为独立的 ContentCard 和 _CardHeader 组件- 新增 _NetworkImageWidget 优化图片加载与显示
- 抽离推荐和同城列表为独立的 RecommendTab 和 NearbyTab 页面
- 移除原 HomePage 中的列表构建逻辑,提升代码可维护性
-优化图片展示逻辑,统一处理单张及多张图片显示样式
- 增加用户状态标签(在线、实名认证、直播中)的灵活展示
- 实现点击卡片跳转至用户信息页的功能
-保留原有下拉刷新与上拉加载交互逻辑
- 修复可能因图片加载引起的界面闪退问题- 提升组件复用性和页面渲染性能
ios
Jolie 4 months ago
parent
commit
cab0719a6d
5 changed files with 877 additions and 979 deletions
  1. 520
      lib/pages/home/content_card.dart
  2. 638
      lib/pages/home/home_page.dart
  3. 140
      lib/pages/home/nearby_tab.dart
  4. 140
      lib/pages/home/recommend_tab.dart
  5. 418
      pubspec.lock

520
lib/pages/home/content_card.dart

@ -1,193 +1,399 @@
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dating_touchme_app/generated/assets.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dating_touchme_app/model/home/marriage_data.dart';
import 'package:dating_touchme_app/pages/home/user_information_page.dart';
class ContentCard extends StatelessWidget {
// //线//Hi/
class _CardHeader extends StatelessWidget {
final MarriageData item;
const ContentCard({
Key? key,
required this.item,
}) : super(key: key);
const _CardHeader({required this.item});
@override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.only(bottom: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
elevation: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
//
_buildUserHeader(),
//
if (item.description.isNotEmpty)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
child: Text(
item.description,
style: TextStyle(
fontSize: 14,
color: Colors.black87
final bool isLive = true; //item.isLive ?? false;
final bool isOnline = true; //item.isOnline ?? false;
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// +
Stack(
children: [
// +
Container(
padding: isLive ? const EdgeInsets.all(2) : EdgeInsets.zero,
decoration: isLive
? BoxDecoration(
borderRadius: BorderRadius.circular(32),
border: Border.all(
color: const Color(0xFFB58BFF),
width: 2,
),
)
: null,
child: ClipRRect(
borderRadius: BorderRadius.circular(30),
child: CachedNetworkImage(
imageUrl: item.avatar,
width: 60,
height: 60,
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
),
errorWidget: (context, url, error) => Image.asset(
Assets.imagesAvatarsExample,
width: 60,
height: 60,
fit: BoxFit.cover,
),
),
strutStyle: const StrutStyle(height: 1.5),
maxLines: 3,
overflow: TextOverflow.ellipsis,
)
),
),
//
if (item.photoList.isNotEmpty)
_buildImageGrid(),
],
),
);
}
Widget _buildUserHeader() {
return Padding(
padding: const EdgeInsets.all(12),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
//
CircleAvatar(
radius: 30,
backgroundImage: NetworkImage(_cleanImageUrl(item.avatar)),
backgroundColor: Colors.grey[200],
),
//
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 12),
child: Column(
if (isOnline)
Positioned(
right: 6,
bottom: 1,
child: Container(
width: 12,
height: 12,
decoration: BoxDecoration(
color: isOnline
? const Color(0xFF2ED573)
: const Color(0xFFCCCCCC), // 线绿线
shape: BoxShape.circle,
boxShadow: isOnline
? const [
BoxShadow(
color: Color(0x332ED573),
blurRadius: 4,
offset: Offset(0, 2),
),
]
: [],
),
),
),
if (isLive)
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
width: 37,
height: 14,
alignment: Alignment.center, //
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(Assets.imagesBtnBgIcon),
),
),
child: const Text(
'直播中',
style: TextStyle(
color: Colors.white,
fontSize: 8,
fontWeight: FontWeight.w600,
),
),
),
),
],
),
const SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
// 使Wrap组件让所有标签在空间允许时显示在一行
Wrap(
spacing: 6,
runSpacing: 2,
children: [
//
Text(
item.nickName,
item.name ?? '用户',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.black
fontWeight: FontWeight.w500,
color: Color.fromRGBO(51, 51, 51, 1),
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
SizedBox(width: 6),
Text(
'${item.age}',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600]
),
),
// 线
if (isOnline == true)
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 2,
),
decoration: BoxDecoration(
color: Color.fromRGBO(234, 255, 219, 1),
borderRadius: BorderRadius.circular(12),
),
child: Text(
isOnline ? '在线' : '',
style: TextStyle(
fontSize: 9,
color: Color.fromRGBO(38, 199, 124, 1),
),
),
),
//
if (item.isRealNameCertified == true)
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 2,
),
decoration: BoxDecoration(
color: const Color(0xFFF3E9FF),
borderRadius: BorderRadius.circular(12),
),
constraints: BoxConstraints(
minWidth: 40, //
),
child: Row(
mainAxisSize: MainAxisSize.min, // Row只占用必要的宽度
children: [
Image.asset(
Assets.imagesVerifiedIcon,
width: 14,
height: 12,
),
const SizedBox(width: 4),
const Text(
'实名',
style: TextStyle(
fontSize: 9,
color: Color.fromRGBO(160, 92, 255, 1),
),
),
],
),
),
//
if (isLive)
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 2,
),
decoration: BoxDecoration(
color: Color.fromRGBO(234, 255, 219, 1),
borderRadius: BorderRadius.circular(12),
),
child: Text(
'视频相亲中',
style: TextStyle(
fontSize: 9,
color: Color.fromRGBO(38, 199, 124, 1),
),
),
),
],
),
Text(
item.city,
style: TextStyle(
fontSize: 12,
color: Colors.grey[500]
),
strutStyle: const StrutStyle(height: 1.2),
)
],
),
),
),
//
GestureDetector(
onTap: () {
//
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 6),
decoration: BoxDecoration(
color: Color(0xFFFF6B6B),
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Image.asset(
Assets.imagesHiIcon,
width: 16,
height: 16,
const SizedBox(height: 6),
SizedBox(
height: 16,
child: Text(
'${item.age ?? 0}岁 · ${item.cityName ?? ''}${item.districtName != null ? item.districtName : ''}',
style: TextStyle(
fontSize: 12,
color: Color.fromRGBO(51, 51, 51, 1),
),
],
overflow: TextOverflow.ellipsis,
),
),
),
],
),
],
),
),
// - HI按钮相同位置
InkWell(
onTap: () {
//
if (isLive) {
//
print('进入直播间');
} else if (isOnline) {
// HI按钮点击逻辑
print('HI点击');
} else {
//
print('发送消息');
}
},
child: Image.asset(
_getButtonImage(),
// width: (item.isLive ?? false) ? 60 : 40,
width: (true) ? 60 : 40,
height: 20,
),
),
],
);
}
Widget _buildImageGrid() {
int imageCount = item.photoList.length;
if (imageCount == 0) return SizedBox();
//
if (imageCount == 1) {
return Container(
width: double.infinity,
height: 200,
String _getButtonImage() {
// if (item.isLive ?? false) {
// return Assets.imagesLiveIcon; // HI位置
// } else if (item.isOnline ?? false) {
// return Assets.imagesHiIcon; // 线HI按钮
// } else {
// return Assets.imagesSendMessageIcon; // 线HI位置
// }
// }
return Assets.imagesLiveIcon; // HI位置
}
}
//
class ContentCard extends StatelessWidget {
final MarriageData item;
const ContentCard({required this.item});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
//
Get.to(() => UserInformationPage(userData: item));
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(12),
bottomRight: Radius.circular(12),
),
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: const [
BoxShadow(
color: Color(0x14000000),
blurRadius: 8,
offset: Offset(0, 4),
),
],
),
child: Image.network(
_cleanImageUrl(item.photoList[0].photoUrl),
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Image.asset(
Assets.imagesExampleContent,
fit: BoxFit.cover,
);
},
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 线 - HI位置
_CardHeader(item: item),
const SizedBox(height: 8),
// -
_buildContent(),
],
),
);
}
//
return GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 2,
crossAxisSpacing: 2,
),
itemCount: imageCount,
itemBuilder: (context, index) {
return Container(
height: 100,
child: Image.network(
_cleanImageUrl(item.photoList[index].photoUrl),
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Image.asset(
Assets.imagesExampleContent,
fit: BoxFit.cover,
);
},
),
);
}
Widget _buildContent() {
return Builder(
builder: (context) {
final List<Widget> contentWidgets = [];
//
if (item.describeInfo != null && item.describeInfo!.isNotEmpty) {
contentWidgets.add(
SizedBox(
// height: 20, // 20
child: Text(
item.describeInfo!,
style: TextStyle(
fontSize: 13,
color: Color.fromRGBO(51, 51, 51, 0.6),
),
overflow: TextOverflow.ellipsis, // ...
maxLines: 1, //
),
),
);
}
//
if (item.photoList != null && item.photoList!.isNotEmpty) {
contentWidgets.add(const SizedBox(height: 16));
// = ( - padding - ListView左右padding - ) / 3
// padding: 12 * 2 = 24
// ListView左右padding: 12 * 2 = 24
// 3: 12 * 2 = 24 (2312)
final screenWidth = MediaQuery.of(context).size.width;
final cardPadding = 12.0 * 2; // padding
final listPadding = 12.0 * 2; // ListView左右padding
final imageSpacing = 12.0 * 2; // 3
final imageWidth =
(screenWidth - cardPadding - listPadding - imageSpacing) / 3;
final imageHeight = imageWidth * 1.05; // aspectRatio 1.05
// 使123使
// 3
final displayPhotos = item.photoList!.take(3).toList();
contentWidgets.add(
Row(
children: displayPhotos.asMap().entries.map((entry) {
int index = entry.key;
String imageUrl = entry.value.photoUrl ?? '';
return Padding(
padding: EdgeInsets.only(left: index > 0 ? 12 : 0),
child: SizedBox(
width: imageWidth,
height: imageHeight,
child: _NetworkImageWidget(
imageUrl: imageUrl,
aspectRatio: 1.05,
),
),
);
}).toList(),
),
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: contentWidgets,
);
},
);
}
// URL中的空格和多余字符
String _cleanImageUrl(String url) {
return url.trim();
}
// - 退
class _NetworkImageWidget extends StatelessWidget {
final String imageUrl;
final double aspectRatio;
const _NetworkImageWidget({
required this.imageUrl,
required this.aspectRatio,
});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
image: DecorationImage(
image: imageUrl.isNotEmpty
? NetworkImage(imageUrl)
: AssetImage(Assets.imagesAvatarsExample) as ImageProvider,
fit: BoxFit.cover,
),
),
width: double.infinity,
height: double.infinity,
);
}
}
}

638
lib/pages/home/home_page.dart

@ -1,48 +1,26 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dating_touchme_app/generated/assets.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:dating_touchme_app/controller/home/home_controller.dart';
import 'package:dating_touchme_app/model/home/marriage_data.dart';
import 'package:dating_touchme_app/pages/home/user_information_page.dart';
import 'package:dating_touchme_app/pages/home/recommend_tab.dart';
import 'package:dating_touchme_app/pages/home/nearby_tab.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
// 使GetX注入HomeController
final HomeController ctrl = Get.put(HomeController());
//
late final EasyRefreshController _recommendRefreshController;
//
late final EasyRefreshController _nearbyRefreshController;
class _HomePageState extends State<HomePage>
with AutomaticKeepAliveClientMixin {
@override
void initState() {
super.initState();
//
_recommendRefreshController = EasyRefreshController(
controlFinishRefresh: true,
controlFinishLoad: true,
);
//
_nearbyRefreshController = EasyRefreshController(
controlFinishRefresh: true,
controlFinishLoad: true,
);
}
@override
void dispose() {
_recommendRefreshController.dispose();
_nearbyRefreshController.dispose();
super.dispose();
// HomeController
if (!Get.isRegistered<HomeController>()) {
Get.put(HomeController());
}
}
@override
@ -50,11 +28,6 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
super.build(context);
return GetBuilder<HomeController>(
builder: (controller) {
// tabbar 64
final bottomPadding = MediaQuery.of(context).padding.bottom;
final tabBarHeight = 64.0;
final totalBottomPadding = bottomPadding + tabBarHeight;
return Stack(
children: [
// -
@ -71,18 +44,18 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
// 使 IndexedStack
return IndexedStack(
index: controller.selectedTabIndex.value,
children: [
children: const [
//
_buildRecommendList(controller, totalBottomPadding),
RecommendTab(),
//
_buildNearbyList(controller, totalBottomPadding),
NearbyTab(),
],
);
}),
),
],
);
}
},
);
}
@ -102,16 +75,6 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
_buildTabButton(title: '同城', index: 1, controller: controller),
],
),
// actions: [
// Padding(
// padding: const EdgeInsets.only(right: 16),
// child: Image.asset(
// Assets.imagesOnlineIcon,
// width: 20,
// height: 20,
// ),
// ),
// ],
bottom: const PreferredSize(
preferredSize: Size.fromHeight(4),
child: SizedBox(height: 4),
@ -119,7 +82,11 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
);
}
Widget _buildTabButton({required String title, required int index, required HomeController controller}) {
Widget _buildTabButton({
required String title,
required int index,
required HomeController controller,
}) {
final bool selected = controller.selectedTabIndex.value == index;
return GestureDetector(
onTap: () {
@ -139,575 +106,20 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
style: TextStyle(
fontSize: selected ? 19 : 17,
fontWeight: selected ? FontWeight.w700 : FontWeight.w400,
color: selected ? const Color(0xFF333333) : const Color(0xFF999999),
color: selected
? const Color(0xFF333333)
: const Color(0xFF999999),
),
),
const SizedBox(height: 6),
selected
? Image.asset(
Assets.imagesTabChangeIcon,
width: 32,
height: 8,
)
? Image.asset(Assets.imagesTabChangeIcon, width: 32, height: 8)
: const SizedBox(height: 8),
],
),
);
}
//
Widget _buildRecommendList(HomeController controller, double totalBottomPadding) {
return Obx(() {
final List<MarriageData> dataSource = controller.recommendFeed;
final bool isLoading = controller.recommendIsLoading.value;
final bool hasMore = controller.recommendHasMore.value;
return EasyRefresh(
controller: _recommendRefreshController,
header: MaterialHeader(
backgroundColor: Colors.red.withOpacity(0.9),
),
footer: MaterialFooter(
backgroundColor: Colors.red.withOpacity(0.9),
),
//
onRefresh: () async {
print('推荐列表下拉刷新被触发');
try {
await controller.refreshRecommendData();
_recommendRefreshController.finishRefresh(IndicatorResult.success);
print('推荐列表刷新完成');
} catch (e) {
print('推荐列表刷新失败: $e');
_recommendRefreshController.finishRefresh(IndicatorResult.fail);
}
},
//
onLoad: () async {
print('推荐列表上拉加载被触发, hasMore: $hasMore');
if (hasMore && controller.recommendHasMore.value) {
try {
await controller.loadRecommendMoreData();
//
if (controller.recommendHasMore.value) {
_recommendRefreshController.finishLoad(IndicatorResult.success);
print('推荐列表加载更多成功');
} else {
_recommendRefreshController.finishLoad(IndicatorResult.noMore);
print('推荐列表没有更多数据了');
}
} catch (e) {
print('推荐列表加载更多失败: $e');
_recommendRefreshController.finishLoad(IndicatorResult.fail);
}
} else {
_recommendRefreshController.finishLoad(IndicatorResult.noMore);
print('推荐列表没有更多数据');
}
},
// EasyRefresh child Widget
child: ListView.separated(
// 使
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
// padding AppBar
padding: EdgeInsets.only(
left: 12,
right: 12,
bottom: totalBottomPadding + 12,
),
itemBuilder: (context, index) {
//
if (isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(
child: CircularProgressIndicator(),
),
);
}
//
if (!isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(
child: Text(
"暂无数据",
style: TextStyle(
fontSize: 14,
color: Color(0xFF999999),
),
),
),
);
}
//
final item = dataSource[index];
return ContentCard(item: item);
},
separatorBuilder: (context, index) {
//
if (dataSource.isEmpty) return const SizedBox.shrink();
return const SizedBox(height: 12);
},
// item
itemCount: dataSource.isEmpty ? 1 : dataSource.length,
),
);
});
}
//
Widget _buildNearbyList(HomeController controller, double totalBottomPadding) {
return Obx(() {
final List<MarriageData> dataSource = controller.nearbyFeed;
final bool isLoading = controller.nearbyIsLoading.value;
final bool hasMore = controller.nearbyHasMore.value;
return EasyRefresh(
controller: _nearbyRefreshController,
header: MaterialHeader(
backgroundColor: Colors.red.withOpacity(0.9),
),
footer: MaterialFooter(
backgroundColor: Colors.red.withOpacity(0.9),
),
//
onRefresh: () async {
print('同城列表下拉刷新被触发');
try {
await controller.refreshNearbyData();
_nearbyRefreshController.finishRefresh(IndicatorResult.success);
print('同城列表刷新完成');
} catch (e) {
print('同城列表刷新失败: $e');
_nearbyRefreshController.finishRefresh(IndicatorResult.fail);
}
},
//
onLoad: () async {
print('同城列表上拉加载被触发, hasMore: $hasMore');
if (hasMore && controller.nearbyHasMore.value) {
try {
await controller.loadNearbyMoreData();
//
if (controller.nearbyHasMore.value) {
_nearbyRefreshController.finishLoad(IndicatorResult.success);
print('同城列表加载更多成功');
} else {
_nearbyRefreshController.finishLoad(IndicatorResult.noMore);
print('同城列表没有更多数据了');
}
} catch (e) {
print('同城列表加载更多失败: $e');
_nearbyRefreshController.finishLoad(IndicatorResult.fail);
}
} else {
_nearbyRefreshController.finishLoad(IndicatorResult.noMore);
print('同城列表没有更多数据');
}
},
// EasyRefresh child Widget
child: ListView.separated(
// 使
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
// padding AppBar
padding: EdgeInsets.only(
left: 12,
right: 12,
bottom: totalBottomPadding + 12,
),
itemBuilder: (context, index) {
//
if (isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(
child: CircularProgressIndicator(),
),
);
}
//
if (!isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(
child: Text(
"暂无数据",
style: TextStyle(
fontSize: 14,
color: Color(0xFF999999),
),
),
),
);
}
//
final item = dataSource[index];
return ContentCard(item: item);
},
separatorBuilder: (context, index) {
//
if (dataSource.isEmpty) return const SizedBox.shrink();
return const SizedBox(height: 12);
},
// item
itemCount: dataSource.isEmpty ? 1 : dataSource.length,
),
);
});
}
@override
bool get wantKeepAlive => true;
}
// //线//Hi/
class _CardHeader extends StatelessWidget {
final MarriageData item;
const _CardHeader({required this.item});
@override
Widget build(BuildContext context) {
final bool isLive = true;//item.isLive ?? false;
final bool isOnline = true;//item.isOnline ?? false;
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// +
Stack(
children: [
// +
Container(
padding: isLive ? const EdgeInsets.all(2) : EdgeInsets.zero,
decoration: isLive
? BoxDecoration(
borderRadius: BorderRadius.circular(32),
border: Border.all(color: const Color(0xFFB58BFF), width: 2),
)
: null,
child: ClipRRect(
borderRadius: BorderRadius.circular(30),
child: CachedNetworkImage(
imageUrl: item.avatar,
width: 60,
height: 60,
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
),
errorWidget: (context, url, error) => Image.asset(Assets.imagesAvatarsExample, width: 60, height: 60, fit: BoxFit.cover),
),
),
),
if (isOnline)
Positioned(
right: 6,
bottom: 1,
child: Container(
width: 12,
height: 12,
decoration: BoxDecoration(
color: isOnline ? const Color(0xFF2ED573) : const Color(0xFFCCCCCC), // 线绿线
shape: BoxShape.circle,
boxShadow: isOnline ? const [
BoxShadow(color: Color(0x332ED573), blurRadius: 4, offset: Offset(0, 2)),
] : [],
),
),
),
if (isLive)
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
width: 37,
height: 14,
alignment: Alignment.center, //
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(Assets.imagesBtnBgIcon),
),
),
child: const Text(
'直播中',
style: TextStyle(color: Colors.white, fontSize: 8, fontWeight: FontWeight.w600),
),
),
),
],
),
const SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 使Wrap组件让所有标签在空间允许时显示在一行
Wrap(
spacing: 6,
runSpacing: 2,
children: [
//
Text(
item.name ?? '用户',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: Color.fromRGBO(51, 51, 51, 1)),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
// 线
if (isOnline == true)
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: Color.fromRGBO(234, 255, 219, 1),
borderRadius: BorderRadius.circular(12),
),
child: Text(
isOnline ? '在线' : '',
style: TextStyle(fontSize: 9, color: Color.fromRGBO(38, 199, 124, 1) ),
),
),
//
if (item.isRealNameCertified == true)
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: const Color(0xFFF3E9FF),
borderRadius: BorderRadius.circular(12),
),
constraints: BoxConstraints(
minWidth: 40, //
),
child: Row(
mainAxisSize: MainAxisSize.min, // Row只占用必要的宽度
children: [
Image.asset(Assets.imagesVerifiedIcon, width: 14, height: 12),
const SizedBox(width: 4),
const Text(
'实名',
style: TextStyle(fontSize: 9, color: Color.fromRGBO(160, 92, 255, 1)),
),
],
),
),
//
if (isLive)
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: Color.fromRGBO(234, 255, 219, 1),
borderRadius: BorderRadius.circular(12),
),
child: Text(
'视频相亲中',
style: TextStyle(fontSize: 9, color: Color.fromRGBO(38, 199, 124, 1)),
),
),
],
),
],
),
const SizedBox(height: 6),
SizedBox(
height: 16,
child: Text(
'${item.age ?? 0}岁 · ${item.cityName ?? ''}${item.districtName != null ? item.districtName : ''}',
style: TextStyle(fontSize: 12, color: Color.fromRGBO(51, 51, 51, 1)),
overflow: TextOverflow.ellipsis,
),
)
],
),
),
// - HI按钮相同位置
InkWell(
onTap: () {
//
if (isLive) {
//
print('进入直播间');
} else if (isOnline) {
// HI按钮点击逻辑
print('HI点击');
} else {
//
print('发送消息');
}
},
child: Image.asset(
_getButtonImage(),
// width: (item.isLive ?? false) ? 60 : 40,
width: (true) ? 60 : 40,
height: 20,
),
),
],
);
}
String _getButtonImage() {
// if (item.isLive ?? false) {
// return Assets.imagesLiveIcon; // HI位置
// } else if (item.isOnline ?? false) {
// return Assets.imagesHiIcon; // 线HI按钮
// } else {
// return Assets.imagesSendMessageIcon; // 线HI位置
// }
// }
return Assets.imagesLiveIcon; // HI位置
}
}
//
class ContentCard extends StatelessWidget {
final MarriageData item;
const ContentCard({required this.item});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
//
Get.to(() => UserInformationPage(userData: item));
},
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: const [
BoxShadow(
color: Color(0x14000000),
blurRadius: 8,
offset: Offset(0, 4),
),
],
),
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 线 - HI位置
_CardHeader(item: item),
const SizedBox(height: 8),
// -
_buildContent(),
],
),
),
);
}
Widget _buildContent() {
return Builder(
builder: (context) {
final List<Widget> contentWidgets = [];
//
if (item.describeInfo != null && item.describeInfo!.isNotEmpty) {
contentWidgets.add(
SizedBox(
// height: 20, // 20
child: Text(
item.describeInfo!,
style: TextStyle(
fontSize: 13,
color: Color.fromRGBO(51, 51, 51, 0.6),
),
overflow: TextOverflow.ellipsis, // ...
maxLines: 1, //
),
),
);
}
//
if (item.photoList != null && item.photoList!.isNotEmpty) {
contentWidgets.add(const SizedBox(height: 16));
// = ( - padding - ListView左右padding - ) / 3
// padding: 12 * 2 = 24
// ListView左右padding: 12 * 2 = 24
// 3: 12 * 2 = 24 (2312)
final screenWidth = MediaQuery.of(context).size.width;
final cardPadding = 12.0 * 2; // padding
final listPadding = 12.0 * 2; // ListView左右padding
final imageSpacing = 12.0 * 2; // 3
final imageWidth = (screenWidth - cardPadding - listPadding - imageSpacing) / 3;
final imageHeight = imageWidth * 1.05; // aspectRatio 1.05
// 使123使
// 3
final displayPhotos = item.photoList!.take(3).toList();
contentWidgets.add(
Row(
children: displayPhotos.asMap().entries.map((entry) {
int index = entry.key;
String imageUrl = entry.value.photoUrl ?? '';
return Padding(
padding: EdgeInsets.only(left: index > 0 ? 12 : 0),
child: SizedBox(
width: imageWidth,
height: imageHeight,
child: _NetworkImageWidget(imageUrl: imageUrl, aspectRatio: 1.05),
// child: CachedNetworkImage(
// imageUrl: imageUrl,
// height: imageWidth,
// width: imageWidth,
// ),
),
);
}).toList(),
),
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: contentWidgets,
);
},
);
}
}
// 使
// - 退
class _NetworkImageWidget extends StatelessWidget {
final String imageUrl;
final double aspectRatio;
const _NetworkImageWidget({required this.imageUrl, required this.aspectRatio});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
image: DecorationImage(
image: imageUrl.isNotEmpty ? NetworkImage(imageUrl) : AssetImage(Assets.imagesAvatarsExample) as ImageProvider,
fit: BoxFit.cover,
),
),
width: double.infinity,
height: double.infinity,
);
}
}

140
lib/pages/home/nearby_tab.dart

@ -0,0 +1,140 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:dating_touchme_app/controller/home/home_controller.dart';
import 'package:dating_touchme_app/model/home/marriage_data.dart';
import 'package:dating_touchme_app/pages/home/content_card.dart';
/// Tab
class NearbyTab extends StatefulWidget {
const NearbyTab({super.key});
@override
State<NearbyTab> createState() => _NearbyTabState();
}
class _NearbyTabState extends State<NearbyTab>
with AutomaticKeepAliveClientMixin {
final HomeController controller = Get.find<HomeController>();
late final EasyRefreshController _refreshController;
@override
void initState() {
super.initState();
_refreshController = EasyRefreshController(
controlFinishRefresh: true,
controlFinishLoad: true,
);
}
@override
void dispose() {
_refreshController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context);
// tabbar 64
final bottomPadding = MediaQuery.of(context).padding.bottom;
final tabBarHeight = 64.0;
final totalBottomPadding = bottomPadding + tabBarHeight;
return Obx(() {
final List<MarriageData> dataSource = controller.nearbyFeed;
final bool isLoading = controller.nearbyIsLoading.value;
final bool hasMore = controller.nearbyHasMore.value;
return EasyRefresh(
controller: _refreshController,
header: MaterialHeader(backgroundColor: Colors.red.withOpacity(0.9)),
footer: MaterialFooter(backgroundColor: Colors.red.withOpacity(0.9)),
//
onRefresh: () async {
print('同城列表下拉刷新被触发');
try {
await controller.refreshNearbyData();
_refreshController.finishRefresh(IndicatorResult.success);
print('同城列表刷新完成');
} catch (e) {
print('同城列表刷新失败: $e');
_refreshController.finishRefresh(IndicatorResult.fail);
}
},
//
onLoad: () async {
print('同城列表上拉加载被触发, hasMore: $hasMore');
if (hasMore && controller.nearbyHasMore.value) {
try {
await controller.loadNearbyMoreData();
//
if (controller.nearbyHasMore.value) {
_refreshController.finishLoad(IndicatorResult.success);
print('同城列表加载更多成功');
} else {
_refreshController.finishLoad(IndicatorResult.noMore);
print('同城列表没有更多数据了');
}
} catch (e) {
print('同城列表加载更多失败: $e');
_refreshController.finishLoad(IndicatorResult.fail);
}
} else {
_refreshController.finishLoad(IndicatorResult.noMore);
print('同城列表没有更多数据');
}
},
// EasyRefresh child Widget
child: ListView.separated(
// 使
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
// padding AppBar
padding: EdgeInsets.only(
left: 12,
right: 12,
bottom: totalBottomPadding + 12,
),
itemBuilder: (context, index) {
//
if (isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(child: CircularProgressIndicator()),
);
}
//
if (!isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(
child: Text(
"暂无数据",
style: TextStyle(fontSize: 14, color: Color(0xFF999999)),
),
),
);
}
//
final item = dataSource[index];
return ContentCard(item: item);
},
separatorBuilder: (context, index) {
//
if (dataSource.isEmpty) return const SizedBox.shrink();
return const SizedBox(height: 12);
},
// item
itemCount: dataSource.isEmpty ? 1 : dataSource.length,
),
);
});
}
@override
bool get wantKeepAlive => true;
}

140
lib/pages/home/recommend_tab.dart

@ -0,0 +1,140 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:dating_touchme_app/controller/home/home_controller.dart';
import 'package:dating_touchme_app/model/home/marriage_data.dart';
import 'package:dating_touchme_app/pages/home/content_card.dart';
/// Tab
class RecommendTab extends StatefulWidget {
const RecommendTab({super.key});
@override
State<RecommendTab> createState() => _RecommendTabState();
}
class _RecommendTabState extends State<RecommendTab>
with AutomaticKeepAliveClientMixin {
final HomeController controller = Get.find<HomeController>();
late final EasyRefreshController _refreshController;
@override
void initState() {
super.initState();
_refreshController = EasyRefreshController(
controlFinishRefresh: true,
controlFinishLoad: true,
);
}
@override
void dispose() {
_refreshController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context);
// tabbar 64
final bottomPadding = MediaQuery.of(context).padding.bottom;
final tabBarHeight = 64.0;
final totalBottomPadding = bottomPadding + tabBarHeight;
return Obx(() {
final List<MarriageData> dataSource = controller.recommendFeed;
final bool isLoading = controller.recommendIsLoading.value;
final bool hasMore = controller.recommendHasMore.value;
return EasyRefresh(
controller: _refreshController,
header: MaterialHeader(backgroundColor: Colors.red.withOpacity(0.9)),
footer: MaterialFooter(backgroundColor: Colors.red.withOpacity(0.9)),
//
onRefresh: () async {
print('推荐列表下拉刷新被触发');
try {
await controller.refreshRecommendData();
_refreshController.finishRefresh(IndicatorResult.success);
print('推荐列表刷新完成');
} catch (e) {
print('推荐列表刷新失败: $e');
_refreshController.finishRefresh(IndicatorResult.fail);
}
},
//
onLoad: () async {
print('推荐列表上拉加载被触发, hasMore: $hasMore');
if (hasMore && controller.recommendHasMore.value) {
try {
await controller.loadRecommendMoreData();
//
if (controller.recommendHasMore.value) {
_refreshController.finishLoad(IndicatorResult.success);
print('推荐列表加载更多成功');
} else {
_refreshController.finishLoad(IndicatorResult.noMore);
print('推荐列表没有更多数据了');
}
} catch (e) {
print('推荐列表加载更多失败: $e');
_refreshController.finishLoad(IndicatorResult.fail);
}
} else {
_refreshController.finishLoad(IndicatorResult.noMore);
print('推荐列表没有更多数据');
}
},
// EasyRefresh child Widget
child: ListView.separated(
// 使
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
// padding AppBar
padding: EdgeInsets.only(
left: 12,
right: 12,
bottom: totalBottomPadding + 12,
),
itemBuilder: (context, index) {
//
if (isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(child: CircularProgressIndicator()),
);
}
//
if (!isLoading && dataSource.isEmpty && index == 0) {
// 使
return SizedBox(
height: MediaQuery.of(context).size.height * 1.2,
child: const Center(
child: Text(
"暂无数据",
style: TextStyle(fontSize: 14, color: Color(0xFF999999)),
),
),
);
}
//
final item = dataSource[index];
return ContentCard(item: item);
},
separatorBuilder: (context, index) {
//
if (dataSource.isEmpty) return const SizedBox.shrink();
return const SizedBox(height: 12);
},
// item
itemCount: dataSource.isEmpty ? 1 : dataSource.length,
),
);
});
}
@override
bool get wantKeepAlive => true;
}

418
pubspec.lock
File diff suppressed because it is too large
View File

Loading…
Cancel
Save