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.3 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. imagePath: null,
  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. ,
  59. recentMessage != null ?
  60. Text(
  61. recentMessage!.data,
  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. setState(() {});
  106. }
  107. onGoBack(dynamic value) async {
  108. conversation = await getConversationById(widget.conversation.id);
  109. setState(() {});
  110. }
  111. }