ar_tourism_flutter_unity/lib/pages/message/recieveLike.dart
2025-05-14 17:04:13 +08:00

221 lines
6.4 KiB
Dart

import 'package:flutter/material.dart';
import '../../apis/app.dart';
import '../../components/appbar.dart';
import '../../components/emptyState.dart';
class ReceiveLikePage extends StatefulWidget {
const ReceiveLikePage({super.key});
@override
_ReceiveLikePageState createState() => _ReceiveLikePageState();
}
class _ReceiveLikePageState extends State<ReceiveLikePage> {
List<dynamic> receiveLikeList = []; // 确保初始化列表
bool isLoading = false;
int currentPage = 1;
int totalCount = 0; // 添加总数计数
final int pageSize = 10;
final ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
getLikeList();
_scrollController.addListener(_onScroll);
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
void _onScroll() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
_loadMore();
}
}
Future<void> _loadMore() async {
if (!isLoading && receiveLikeList.length < totalCount) {
await getLikeList();
}
}
// 获取收到的喜爱列表接口
getLikeList() async {
if (isLoading) return;
setState(() {
isLoading = true;
});
try {
final res = await userApi.getLikeList(
page: currentPage,
pageSize: pageSize,
);
print("收到的喜爱列表result:$res");
if (res['code'] == 200) {
final newItems = res['rows'] as List;
setState(() {
if (currentPage == 1) {
receiveLikeList = newItems;
} else {
receiveLikeList.addAll(newItems);
}
totalCount = res['total'] ?? 0; // 添加总数
currentPage++;
isLoading = false;
});
}
} catch (error) {
print("网络错误: $error");
setState(() {
isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(
title: '收到的喜爱',
onBackPressed: () {
Navigator.pop(context, true);
},
),
body: Container(
decoration: const BoxDecoration(color: Color(0xff0C0C16)),
child: RefreshIndicator(
onRefresh: () async {
currentPage = 1;
await getLikeList();
},
child: receiveLikeList.isEmpty && !isLoading
? const EmptyState(message: '暂无收到的喜爱')
: ListView.builder(
controller: _scrollController,
itemCount: receiveLikeList.length + 1,
itemBuilder: (context, index) {
if (index == receiveLikeList.length) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 16),
alignment: Alignment.center,
child: receiveLikeList.length < totalCount
? const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(
strokeWidth: 2.0,
color: Colors.white,
),
)
: const Text(
'已经到底啦',
style: TextStyle(
color: Colors.grey,
fontSize: 14,
),
),
);
}
return _buildReceiveLikeItem(receiveLikeList[index]);
},
),
),
),
);
}
Widget _buildReceiveLikeItem(Map<String, dynamic> user) {
return ListTile(
leading: ClipOval(
child: user['avatar'] != null
? Image.network(
user['avatar'],
width: 50,
height: 50,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(
width: 50,
height: 50,
color: Colors.grey[300],
child: const Icon(Icons.person, color: Colors.grey),
);
},
)
: Container(
width: 50,
height: 50,
color: Colors.grey[300],
child: const Icon(Icons.person, color: Colors.grey),
),
),
title: Text(
user['nickName'] ?? '',
style: const TextStyle(
fontSize: 16,
color: Color(0xffF5F5F5),
),
overflow: TextOverflow.ellipsis,
),
subtitle: Text(
'${user['nickName']}${user['detail'] ?? '暂无详情'}',
style: const TextStyle(
fontSize: 12,
color: Color(0xff6D717A),
),
overflow: TextOverflow.ellipsis,
),
trailing: user['workAddress'] != null
? GestureDetector(
onTap: () {
if (user['workId'] != null) {
Navigator.pushNamed(
context,
'/infoDetail',
arguments: {
'workId': user['workId'],
'isLiked': user['isAddLike'] ?? "0",
'likeCount': user['receiveRockets'] ?? 0,
},
);
}
},
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: Image.network(
user['workAddress'],
width: 50,
height: 50,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(
width: 50,
height: 50,
color: Colors.grey[300],
child: const Icon(Icons.image_not_supported, color: Colors.grey),
);
},
),
),
)
: Container(
width: 50,
height: 50,
color: Colors.grey[300],
child: const Icon(Icons.image_not_supported, color: Colors.grey),
),
);
}
}