session.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 SessionList extends StatefulWidget {
  7. @override
  8. _SessionListState createState() => _SessionListState();
  9. }
  10. class _SessionListState extends State<SessionList> {
  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: SessionItem(_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 SessionItem extends StatelessWidget {
  67. final SessionModel model;
  68. SessionItem(this.model);
  69. @override
  70. Widget build(BuildContext context) {
  71. final message = model.lastMessage;
  72. return Row(
  73. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  74. children: <Widget>[
  75. model.avatar ?? Global.defaultAvatar,
  76. Expanded(
  77. child: Column(
  78. children: <Widget>[
  79. Padding(
  80. padding: EdgeInsets.symmetric(vertical: 8),
  81. child: Row(
  82. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  83. children: <Widget>[
  84. Expanded(
  85. child: Text(
  86. model.chatName,
  87. style: TextStyle(fontSize: 34),
  88. overflow: TextOverflow.ellipsis,
  89. ),
  90. ),
  91. Text(
  92. model.lastMessage != null ? _formatDate(model.lastMessage!.time) : "",
  93. style: TextStyle(fontSize: 26),
  94. overflow: TextOverflow.ellipsis,
  95. ),
  96. ],
  97. ),
  98. ),
  99. Row(
  100. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  101. children: <Widget>[
  102. Expanded(
  103. child: RichText(
  104. text: TextSpan(children: [
  105. TextSpan(
  106. text: model.isAtYou ? "[@你]" : "",
  107. style: TextStyle(fontSize: 28),
  108. ),
  109. TextSpan(
  110. text: model.isSpecialAttention ? "[特别关注]" : "",
  111. style: TextStyle(fontSize: 28),
  112. ),
  113. TextSpan(
  114. text: model.isAtAll ? "[@所有人]" : "",
  115. style: TextStyle(fontSize: 28),
  116. ),
  117. TextSpan(
  118. text: model.lastMessage?.content.target?.plaintext ?? "",
  119. style: TextStyle(fontSize: 28),
  120. )
  121. ]),
  122. overflow: TextOverflow.ellipsis,
  123. ),
  124. ),
  125. (model.unReadCount > 0 && !model.isDND)
  126. ? Container(
  127. width: 32,
  128. height: 32,
  129. alignment: Alignment.center,
  130. decoration: BoxDecoration(borderRadius: BorderRadius.circular(20)),
  131. child: Text(
  132. model.unReadCount.toString(),
  133. style: TextStyle(color: Colors.white, fontSize: 26),
  134. ))
  135. : Container(),
  136. model.isDND
  137. ? Row(
  138. children: <Widget>[
  139. Icon(Icons.visibility_off),
  140. model.unReadCount > 0
  141. ? Icon(
  142. Icons.chat_bubble,
  143. color: Colors.red,
  144. ) // TODO: 小红点
  145. : Container()
  146. ],
  147. )
  148. : Container()
  149. ],
  150. )
  151. ],
  152. ),
  153. )
  154. ],
  155. );
  156. }
  157. String _formatDate(int time) {
  158. DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(time);
  159. return "${dateTime.hour}:${dateTime.minute}";
  160. }
  161. }