message.dart 5.6 KB

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