message.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'package:e2ee_chat/common/global.dart';
  2. import 'package:e2ee_chat/presenter/session.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_slidable/flutter_slidable.dart';
  5. import 'package:provider/provider.dart';
  6. class MessageList extends StatefulWidget {
  7. @override
  8. _MessageListState createState() => _MessageListState();
  9. }
  10. class _MessageListState extends State<MessageList> {
  11. @override
  12. Widget build(BuildContext context) {
  13. return ChangeNotifierProvider<SessionListModel>(
  14. create: (context) {
  15. return SessionListModel();
  16. },
  17. child: Builder(
  18. builder: (context) {
  19. var provider = Provider.of<SessionListModel>(context);
  20. return ListView.builder(
  21. itemCount: provider.count,
  22. itemBuilder: (context, index) {
  23. var _item = provider.sessions[index];
  24. return Slidable(
  25. actionPane: SlidableDrawerActionPane(),
  26. actionExtentRatio: 0.25,
  27. secondaryActions: <Widget>[
  28. IconSlideAction(
  29. caption: '取消置顶',
  30. color: Colors.black45,
  31. icon: Icons.more_horiz,
  32. onTap: () {},
  33. ),
  34. IconSlideAction(
  35. caption: '删除',
  36. color: Colors.redAccent,
  37. icon: Icons.delete,
  38. onTap: () => {},
  39. ),
  40. ],
  41. child: Padding(
  42. padding: EdgeInsets.only(left: 36, bottom: 20, top: 20),
  43. child: Row(
  44. crossAxisAlignment: CrossAxisAlignment.start,
  45. children: <Widget>[
  46. Expanded(child: MessageItem(_item)),
  47. _item.isStick
  48. ? Container(
  49. width: 40,
  50. height: 48,
  51. margin: EdgeInsets.only(top: 10, right: 10),
  52. child: Icon(Icons.star))
  53. : Padding(
  54. padding: EdgeInsets.symmetric(horizontal: 24),
  55. )
  56. ],
  57. ),
  58. ),
  59. );
  60. });
  61. },
  62. ),
  63. );
  64. }
  65. }
  66. class MessageItem extends StatelessWidget {
  67. final SessionModel model;
  68. MessageItem(this.model);
  69. @override
  70. Widget build(BuildContext context) {
  71. return Row(
  72. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  73. children: <Widget>[
  74. model.avatar ?? Global.defaultAvatar,
  75. Expanded(
  76. child: Column(
  77. children: <Widget>[
  78. Padding(
  79. padding: EdgeInsets.symmetric(vertical: 8),
  80. child: Row(
  81. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  82. children: <Widget>[
  83. Expanded(
  84. child: Text(
  85. model.chatName,
  86. style: TextStyle(fontSize: 34),
  87. overflow: TextOverflow.ellipsis,
  88. ),
  89. ),
  90. Text(
  91. _formatDate(),
  92. style: TextStyle(fontSize: 26),
  93. overflow: TextOverflow.ellipsis,
  94. ),
  95. ],
  96. ),
  97. ),
  98. Row(
  99. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  100. children: <Widget>[
  101. Expanded(
  102. child: RichText(
  103. text: TextSpan(children: [
  104. TextSpan(
  105. text: model.isAtYou ? "[@你]" : "",
  106. style: TextStyle(fontSize: 28),
  107. ),
  108. TextSpan(
  109. text: model.isSpecialAttention ? "[特别关注]" : "",
  110. style: TextStyle(fontSize: 28),
  111. ),
  112. TextSpan(
  113. text: model.isAtAll ? "[@所有人]" : "",
  114. style: TextStyle(fontSize: 28),
  115. ),
  116. TextSpan(
  117. text: model.lastMessage.content.target!.plaintext,
  118. style: TextStyle(fontSize: 28),
  119. )
  120. ]),
  121. overflow: TextOverflow.ellipsis,
  122. ),
  123. ),
  124. (model.unReadCount > 0 && !model.isDND)
  125. ? Container(
  126. width: 32,
  127. height: 32,
  128. alignment: Alignment.center,
  129. decoration: BoxDecoration(borderRadius: BorderRadius.circular(20)),
  130. child: Text(
  131. model.unReadCount.toString(),
  132. style: TextStyle(color: Colors.white, fontSize: 26),
  133. ))
  134. : Container(),
  135. model.isDND
  136. ? Row(
  137. children: <Widget>[
  138. Icon(Icons.visibility_off),
  139. model.unReadCount > 0
  140. ? Icon(
  141. Icons.chat_bubble,
  142. color: Colors.red,
  143. ) // TODO: 小红点
  144. : Container()
  145. ],
  146. )
  147. : Container()
  148. ],
  149. )
  150. ],
  151. ),
  152. )
  153. ],
  154. );
  155. }
  156. String _formatDate() {
  157. DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(model.lastMessage.time);
  158. return "${dateTime.hour}:${dateTime.minute}";
  159. }
  160. }