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.

119 lines
4.0 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 Message? recentMessage;
  19. bool loaded = false;
  20. @override
  21. Widget build(BuildContext context) {
  22. return GestureDetector(
  23. behavior: HitTestBehavior.opaque,
  24. onTap: () {
  25. loaded ? Navigator.push(context, MaterialPageRoute(builder: (context){
  26. return ConversationDetail(
  27. conversation: conversation,
  28. );
  29. })).then(onGoBack) : null;
  30. },
  31. child: Container(
  32. padding: const EdgeInsets.only(left: 16,right: 0,top: 10,bottom: 10),
  33. child: !loaded ? null : Row(
  34. children: <Widget>[
  35. Expanded(
  36. child: Row(
  37. children: <Widget>[
  38. CustomCircleAvatar(
  39. initials: widget.conversation.name[0].toUpperCase(),
  40. image: widget.conversation.icon,
  41. ),
  42. const SizedBox(width: 16),
  43. Expanded(
  44. child: Align(
  45. alignment: Alignment.centerLeft,
  46. child: Container(
  47. color: Colors.transparent,
  48. child: Column(
  49. crossAxisAlignment: CrossAxisAlignment.start,
  50. children: <Widget>[
  51. Text(
  52. widget.conversation.name,
  53. style: const TextStyle(fontSize: 16)
  54. ),
  55. recentMessage != null ?
  56. const SizedBox(height: 2) :
  57. const SizedBox.shrink(),
  58. recentMessage != null ?
  59. Text(
  60. recentMessage!.getContent(),
  61. overflow: TextOverflow.ellipsis,
  62. maxLines: 1,
  63. style: TextStyle(
  64. fontSize: 13,
  65. color: Colors.grey.shade600,
  66. fontWeight: conversation.isRead ? FontWeight.normal : FontWeight.bold,
  67. ),
  68. ) :
  69. const SizedBox.shrink(),
  70. ],
  71. ),
  72. ),
  73. ),
  74. ),
  75. recentMessage != null ?
  76. Padding(
  77. padding: const EdgeInsets.only(left: 10),
  78. child: Text(
  79. convertToAgo(recentMessage!.createdAt, short: true),
  80. style: TextStyle(
  81. fontSize: 13,
  82. color: Colors.grey.shade600,
  83. ),
  84. )
  85. ):
  86. const SizedBox.shrink(),
  87. ],
  88. ),
  89. ),
  90. ],
  91. ),
  92. ),
  93. );
  94. }
  95. @override
  96. void initState() {
  97. super.initState();
  98. getConversationData();
  99. }
  100. Future<void> getConversationData() async {
  101. conversation = widget.conversation;
  102. recentMessage = await conversation.getRecentMessage();
  103. loaded = true;
  104. setState(() {});
  105. }
  106. onGoBack(dynamic value) async {
  107. conversation = await getConversationById(widget.conversation.id);
  108. setState(() {});
  109. }
  110. }