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.

124 lines
4.1 KiB

  1. import 'package:Envelope/database/repositories/conversations_repository.dart';
  2. import 'package:flutter/material.dart';
  3. import '/database/models/messages.dart';
  4. import '/components/custom_circle_avatar.dart';
  5. import '/database/models/conversations.dart';
  6. import '/views/main/conversation/detail.dart';
  7. import '/utils/time.dart';
  8. class ConversationListItem extends StatefulWidget {
  9. final Conversation conversation;
  10. const ConversationListItem({
  11. Key? key,
  12. required this.conversation,
  13. }) : super(key: key);
  14. @override
  15. _ConversationListItemState createState() => _ConversationListItemState();
  16. }
  17. class _ConversationListItemState extends State<ConversationListItem> {
  18. late Conversation conversation;
  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: widget.conversation.name[0].toUpperCase(),
  41. image: widget.conversation.icon,
  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. widget.conversation.name,
  54. style: const TextStyle(fontSize: 16)
  55. ),
  56. recentMessage != null ?
  57. const SizedBox(height: 2) :
  58. const SizedBox.shrink(),
  59. recentMessage != null ?
  60. Text(
  61. recentMessage!.getContent(),
  62. overflow: TextOverflow.ellipsis,
  63. maxLines: 1,
  64. style: TextStyle(
  65. fontSize: 13,
  66. color: Colors.grey.shade600,
  67. fontWeight: conversation.isRead ? FontWeight.normal : FontWeight.bold,
  68. ),
  69. ) :
  70. const SizedBox.shrink(),
  71. ],
  72. ),
  73. ),
  74. ),
  75. ),
  76. recentMessage != null ?
  77. Padding(
  78. padding: const EdgeInsets.only(left: 10),
  79. child: Text(
  80. convertToAgo(recentMessage!.createdAt, short: true),
  81. style: TextStyle(
  82. fontSize: 13,
  83. color: Colors.grey.shade600,
  84. ),
  85. )
  86. ):
  87. const SizedBox.shrink(),
  88. ],
  89. ),
  90. ),
  91. ],
  92. ),
  93. ),
  94. );
  95. }
  96. @override
  97. void initState() {
  98. super.initState();
  99. getConversationData();
  100. }
  101. Future<void> getConversationData() async {
  102. conversation = widget.conversation;
  103. recentMessage = await conversation.getRecentMessage();
  104. loaded = true;
  105. if (mounted) {
  106. setState(() {});
  107. }
  108. }
  109. onGoBack(dynamic value) async {
  110. conversation = await ConversationsRepository.getConversationById(widget.conversation.id);
  111. if (mounted) {
  112. setState(() {});
  113. }
  114. }
  115. }