You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
8.4 KiB
236 lines
8.4 KiB
import 'package:dating_touchme_app/generated/assets.dart';
|
|
import 'package:easy_refresh/easy_refresh.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:dating_touchme_app/controller/home/home_controller.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;
|
|
|
|
final ScrollController _scrollController = ScrollController();
|
|
|
|
bool _shrink = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_refreshController = EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true);
|
|
|
|
|
|
_scrollController.addListener(_onScroll);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_refreshController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
|
|
void _onScroll() {
|
|
final offset = _scrollController.offset;
|
|
|
|
final shouldShrink = offset > 200;
|
|
|
|
if (shouldShrink != _shrink) {
|
|
setState(() {
|
|
_shrink = shouldShrink;
|
|
});
|
|
}
|
|
}
|
|
|
|
@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 Stack(
|
|
children: [
|
|
Obx(() {
|
|
if (controller.recommendIsLoading.value && controller.recommendFeed.isEmpty) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
CircularProgressIndicator(),
|
|
SizedBox(height: 16),
|
|
Text('加载数据中...'),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
return EasyRefresh(
|
|
controller: _refreshController,
|
|
header: const ClassicHeader(
|
|
dragText: '下拉刷新',
|
|
armedText: '释放刷新',
|
|
readyText: '刷新中...',
|
|
processingText: '刷新中...',
|
|
processedText: '刷新完成',
|
|
failedText: '刷新失败',
|
|
noMoreText: '没有更多数据',
|
|
showMessage: false
|
|
),
|
|
footer: ClassicFooter(
|
|
dragText: '上拉加载',
|
|
armedText: '释放加载',
|
|
readyText: '加载中...',
|
|
processingText: '加载中...',
|
|
processedText: '加载完成',
|
|
failedText: '加载失败',
|
|
noMoreText: '没有更多数据',
|
|
showMessage: false
|
|
),
|
|
// 下拉刷新
|
|
onRefresh: () async {
|
|
print('推荐列表下拉刷新被触发');
|
|
try {
|
|
await controller.refreshRecommendData();
|
|
print( '推荐列表刷新完成, hasMore: $controller.recommendHasMore.value');
|
|
_refreshController.finishRefresh();
|
|
_refreshController.resetFooter();
|
|
} catch (e) {
|
|
print('推荐列表刷新失败: $e');
|
|
_refreshController.finishRefresh(IndicatorResult.fail);
|
|
}
|
|
},
|
|
// 上拉加载更多
|
|
onLoad: () async {
|
|
print('推荐列表上拉加载被触发, 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);
|
|
}
|
|
},
|
|
child: ListView.separated(
|
|
controller: _scrollController,
|
|
// 关键:始终允许滚动,即使内容不足
|
|
// 移除顶部 padding,让刷新指示器可以正确显示在 AppBar 下方
|
|
padding: EdgeInsets.only(left: 12, right: 12),
|
|
itemBuilder: (context, index) {
|
|
// 空数据状态
|
|
if (controller.recommendFeed.isEmpty && index == 0) {
|
|
// 使用足够的高度确保可以滚动
|
|
if (controller.recommendIsLoading.value) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
CircularProgressIndicator(),
|
|
SizedBox(height: 16),
|
|
Text('加载数据中...'),
|
|
],
|
|
),
|
|
);
|
|
} else {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text('暂无数据'),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
// 数据项
|
|
final item = controller.recommendFeed[index];
|
|
return ContentCard(item: item);
|
|
},
|
|
separatorBuilder: (context, index) {
|
|
// 空状态或加载状态时不显示分隔符
|
|
if (controller.recommendFeed.isEmpty) {
|
|
return const SizedBox.shrink();
|
|
}
|
|
return const SizedBox(height: 12);
|
|
},
|
|
// 至少显示一个 item(用于显示加载或空状态)
|
|
itemCount: controller.recommendFeed.isEmpty ? 1 : controller.recommendFeed.length,
|
|
)
|
|
);
|
|
}),
|
|
Positioned(
|
|
right: 0,
|
|
bottom: 42,
|
|
child: Obx(() {
|
|
return AnimatedContainer(
|
|
duration: const Duration(milliseconds: 250),
|
|
width: _shrink ? 50 : 142,
|
|
height: 50,
|
|
padding: EdgeInsets.all(5),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.all(Radius.circular(50)),
|
|
gradient: LinearGradient(
|
|
begin: Alignment.centerLeft,
|
|
end: Alignment.centerRight,
|
|
colors: [
|
|
Color(0xFF8359FF),
|
|
Color(0xFF3D8AE0),
|
|
],
|
|
),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.all(Radius.circular(40)),
|
|
child: Image.asset(
|
|
Assets.imagesUserAvatar,
|
|
width: 40,
|
|
height: 40,
|
|
),
|
|
),
|
|
if(!_shrink) SizedBox(width: 4,),
|
|
if(!_shrink) Column(
|
|
children: [
|
|
Text(
|
|
"与你匹配的",
|
|
style: TextStyle(
|
|
fontSize: 15,
|
|
color: Colors.white
|
|
),
|
|
),
|
|
Text(
|
|
"${controller.cityCode.value ?? 0}人正在连麦...",
|
|
style: TextStyle(
|
|
fontSize: 11,
|
|
color: Colors.white
|
|
),
|
|
)
|
|
],
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}),
|
|
)
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool get wantKeepAlive => true;
|
|
}
|