Is there any way to get the latest message from channel?
I am showing list of channel like shown below, however in the subtitle here
section I want to show the latest message of that channel, how can I achieve that?
import 'package:amity_sdk/amity_sdk.dart';
import 'package:app/constants/app_theme.dart';
import 'package:app/constants/images.dart';
import 'package:app/screens/home/social/invite_members_screen.dart';
import 'package:app/services/amity/chat/chat_room.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
class ChatRoomInfo {
final String title;
final String subtitle;
final IconData? icon;
final String? profileUrl;
final Function? onTap;
ChatRoomInfo({
required this.title,
required this.subtitle,
this.icon,
this.profileUrl,
this.onTap,
});
}
class ChannelListScreen extends StatefulWidget {
const ChannelListScreen({super.key});
@override
State<ChannelListScreen> createState() => _ChannelListScreenState();
}
class _ChannelListScreenState extends State<ChannelListScreen> {
final _amitychannel = <AmityChannel>[];
late PagingController<AmityChannel> _channelController;
final activityChatRoomInfo = ChatRoomInfo(
title: 'Activity',
subtitle: 'Welcome to the community',
icon: Icons.favorite,
);
@override
void initState() {
_queryChannel();
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_channelController.fetchNextPage();
});
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
scrolledUnderElevation: 0,
title: const Text('Inbox'),
centerTitle: true, // Center the title
backgroundColor: AppColors
.primaryBackground, // Assuming a dark theme as in the image
actions: [
GestureDetector(
onTap: () {
Get.to(
() => const InviteMemberScreen(
inviteType: InviteType.chat,
communityId: "",
),
);
},
child: SvgPicture.asset(
chatPlus,
color: Colors.white,
),
),
const SizedBox(
width: 20,
)
],
),
backgroundColor:
AppColors.primaryBackground, // Assuming a dark theme as in the image
body: ListView.separated(
itemCount: _amitychannel.length + 1,
separatorBuilder: (context, index) =>
const Divider(color: AppColors.secondaryBackground),
itemBuilder: (context, index) {
if (index == 0) {
return _buildActivityChatRoomInfoTile();
}
final chatRoom = _amitychannel[index - 1];
return ListTile(
leading: chatRoom.avatar?.fileUrl != null
? CircleAvatar(
backgroundImage: NetworkImage(chatRoom.avatar!.fileUrl!),
)
: CircleAvatar(
backgroundColor: Colors.grey[800],
child: const Icon(
Icons.favorite,
color: Colors.white,
),
),
title: Text(
chatRoom.displayName ?? "No Name",
style: const TextStyle(color: Colors.white),
),
subtitle: Text(
"Subtitle here", //TODO retrieve last chat from channel
style: TextStyle(color: Colors.grey[500]),
),
trailing: const Icon(
Icons.chevron_right,
color: Colors.white,
),
onTap: () {
Get.to(() => ChatRoom(channel: chatRoom));
},
);
},
),
);
}
// Available Channel Type options
// AmityChannelType.COMMUNITY;
// AmityChannelType.LIVE;
// AmityChannelType.BROADCAST;
// AmityChannelType.CONVERSATION;
void _queryChannel() {
// Query for Community type
_channelController = PagingController(
pageFuture: (token) => AmityChatClient.newChannelRepository()
.getChannels()
.getPagingData(token: token, limit: 20),
pageSize: 20,
)..addListener(
() {
if (_channelController.error == null) {
//handle results, we suggest to clear the previous items
//and add with the latest _controller.loadedItems
_amitychannel.clear();
_amitychannel.addAll(_channelController.loadedItems);
//update widgets
} else {
//error on pagination controller
//update widgets
}
setState(() {});
},
);
_channelController.addListener(
() {
if (_channelController.error == null) {
//handle results, we suggest to clear the previous items
//and add with the latest _controller.loadedItems
_amitychannel.clear();
_amitychannel.addAll(_channelController.loadedItems);
//update widgets
} else {
//error on pagination controller
//update widgets
}
setState(() {});
},
);
}
Widget _buildActivityChatRoomInfoTile() {
return ListTile(
leading: CircleAvatar(
backgroundColor: Colors.grey[800],
child: const Icon(
Icons.favorite,
color: Colors.white,
),
),
title: Text(
activityChatRoomInfo.title,
style: const TextStyle(color: Colors.white),
),
subtitle: Text(
activityChatRoomInfo.subtitle, //TODO retrieve last chat from channel
style: TextStyle(color: Colors.grey[500]),
),
trailing: const Icon(
Icons.chevron_right,
color: Colors.white,
),
onTap: () {},
);
}
}