Encrypted messaging app
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.

121 lines
4.4 KiB

  1. import 'package:flutter/material.dart';
  2. import '/models/messages.dart';
  3. import '/components/custom_circle_avatar.dart';
  4. import '/models/conversations.dart';
  5. import '/views/main/conversation/detail.dart';
  6. import '/utils/time.dart';
  7. class ConversationListItem extends StatefulWidget{
  8. final Conversation conversation;
  9. const ConversationListItem({
  10. Key? key,
  11. required this.conversation,
  12. }) : super(key: key);
  13. @override
  14. _ConversationListItemState createState() => _ConversationListItemState();
  15. }
  16. class _ConversationListItemState extends State<ConversationListItem> {
  17. late Conversation conversation;
  18. late String conversationName;
  19. late Message? recentMessage;
  20. bool loaded = false;
  21. @override
  22. Widget build(BuildContext context) {
  23. return GestureDetector(
  24. behavior: HitTestBehavior.opaque,
  25. onTap: () {
  26. loaded ? Navigator.push(context, MaterialPageRoute(builder: (context){
  27. return ConversationDetail(
  28. conversation: conversation,
  29. );
  30. })).then(onGoBack) : null;
  31. },
  32. child: Container(
  33. padding: const EdgeInsets.only(left: 16,right: 0,top: 10,bottom: 10),
  34. child: !loaded ? null : Row(
  35. children: <Widget>[
  36. Expanded(
  37. child: Row(
  38. children: <Widget>[
  39. CustomCircleAvatar(
  40. initials: conversationName[0].toUpperCase(),
  41. imagePath: null,
  42. ),
  43. const SizedBox(width: 16),
  44. Expanded(
  45. child: Align(
  46. alignment: Alignment.centerLeft,
  47. child: Container(
  48. color: Colors.transparent,
  49. child: Column(
  50. crossAxisAlignment: CrossAxisAlignment.start,
  51. children: <Widget>[
  52. Text(
  53. conversationName,
  54. style: const TextStyle(fontSize: 16)
  55. ),
  56. recentMessage != null ?
  57. const SizedBox(height: 2) :
  58. const SizedBox.shrink()
  59. ,
  60. recentMessage != null ?
  61. Text(
  62. recentMessage!.data,
  63. overflow: TextOverflow.ellipsis,
  64. maxLines: 1,
  65. style: TextStyle(
  66. fontSize: 13,
  67. color: Colors.grey.shade600,
  68. fontWeight: conversation.isRead ? FontWeight.normal : FontWeight.bold,
  69. ),
  70. ) :
  71. const SizedBox.shrink(),
  72. ],
  73. ),
  74. ),
  75. ),
  76. ),
  77. recentMessage != null ?
  78. Padding(
  79. padding: const EdgeInsets.only(left: 10),
  80. child: Text(
  81. convertToAgo(recentMessage!.createdAt, short: true),
  82. style: TextStyle(
  83. fontSize: 13,
  84. color: Colors.grey.shade600,
  85. ),
  86. )
  87. ):
  88. const SizedBox.shrink(),
  89. ],
  90. ),
  91. ),
  92. ],
  93. ),
  94. ),
  95. );
  96. }
  97. @override
  98. void initState() {
  99. super.initState();
  100. getConversationData();
  101. }
  102. Future<void> getConversationData() async {
  103. conversation = widget.conversation;
  104. conversationName = await widget.conversation.getName();
  105. recentMessage = await conversation.getRecentMessage();
  106. loaded = true;
  107. setState(() {});
  108. }
  109. onGoBack(dynamic value) async {
  110. conversation = await getConversationById(widget.conversation.id);
  111. setState(() {});
  112. }
  113. }