|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:stream_chat/stream_chat.dart'; |
| 5 | +import 'package:stream_chat_flutter/src/reaction_bubble.dart'; |
| 6 | +import 'package:stream_chat_flutter/src/reaction_picker.dart'; |
| 7 | +import 'package:stream_chat_flutter/src/stream_chat.dart'; |
| 8 | +import 'package:stream_chat_flutter/src/user_avatar.dart'; |
| 9 | + |
| 10 | +import 'message_widget.dart'; |
| 11 | +import 'stream_chat_theme.dart'; |
| 12 | + |
| 13 | +class MessageReactionsModal extends StatelessWidget { |
| 14 | + final Widget Function(BuildContext, Message) editMessageInputBuilder; |
| 15 | + final void Function(Message) onThreadTap; |
| 16 | + final Message message; |
| 17 | + final MessageTheme messageTheme; |
| 18 | + final bool reverse; |
| 19 | + final bool showReactions; |
| 20 | + final ShapeBorder messageShape; |
| 21 | + final void Function(User) onUserAvatarTap; |
| 22 | + |
| 23 | + const MessageReactionsModal({ |
| 24 | + Key key, |
| 25 | + @required this.message, |
| 26 | + @required this.messageTheme, |
| 27 | + this.showReactions = true, |
| 28 | + this.onThreadTap, |
| 29 | + this.editMessageInputBuilder, |
| 30 | + this.messageShape, |
| 31 | + this.reverse = false, |
| 32 | + this.onUserAvatarTap, |
| 33 | + }) : super(key: key); |
| 34 | + |
| 35 | + @override |
| 36 | + Widget build(BuildContext context) { |
| 37 | + return Stack( |
| 38 | + children: [ |
| 39 | + Positioned.fill( |
| 40 | + child: GestureDetector( |
| 41 | + behavior: HitTestBehavior.translucent, |
| 42 | + onTap: () { |
| 43 | + Navigator.pop(context); |
| 44 | + }, |
| 45 | + child: BackdropFilter( |
| 46 | + filter: ImageFilter.blur( |
| 47 | + sigmaX: 10, |
| 48 | + sigmaY: 10, |
| 49 | + ), |
| 50 | + child: Container( |
| 51 | + color: Colors.transparent, |
| 52 | + ), |
| 53 | + ), |
| 54 | + ), |
| 55 | + ), |
| 56 | + Column( |
| 57 | + mainAxisAlignment: MainAxisAlignment.center, |
| 58 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 59 | + children: <Widget>[ |
| 60 | + if (showReactions && |
| 61 | + (message.status == MessageSendingStatus.SENT || |
| 62 | + message.status == null)) |
| 63 | + Center( |
| 64 | + child: ReactionPicker( |
| 65 | + message: message, |
| 66 | + messageTheme: messageTheme, |
| 67 | + ), |
| 68 | + ), |
| 69 | + AbsorbPointer( |
| 70 | + child: MessageWidget( |
| 71 | + key: Key('MessageWidget'), |
| 72 | + reverse: reverse, |
| 73 | + message: message, |
| 74 | + messageTheme: messageTheme, |
| 75 | + showReactions: false, |
| 76 | + showUsername: false, |
| 77 | + showReplyIndicator: false, |
| 78 | + showTimestamp: false, |
| 79 | + showSendingIndicator: DisplayWidget.gone, |
| 80 | + shape: messageShape, |
| 81 | + ), |
| 82 | + ), |
| 83 | + SizedBox( |
| 84 | + height: 16, |
| 85 | + ), |
| 86 | + if (message.latestReactions?.isNotEmpty == true) |
| 87 | + Container( |
| 88 | + constraints: BoxConstraints.loose(Size.fromHeight(400)), |
| 89 | + child: _buildReactionCard(context), |
| 90 | + ), |
| 91 | + ], |
| 92 | + ), |
| 93 | + ], |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + Padding _buildReactionCard(BuildContext context) { |
| 98 | + final currentUser = StreamChat.of(context).user; |
| 99 | + return Padding( |
| 100 | + padding: const EdgeInsets.symmetric( |
| 101 | + horizontal: 8.0, |
| 102 | + ), |
| 103 | + child: Card( |
| 104 | + clipBehavior: Clip.hardEdge, |
| 105 | + shape: RoundedRectangleBorder( |
| 106 | + borderRadius: BorderRadius.circular(16), |
| 107 | + ), |
| 108 | + child: Column( |
| 109 | + mainAxisSize: MainAxisSize.min, |
| 110 | + children: [ |
| 111 | + Padding( |
| 112 | + padding: const EdgeInsets.all(16.0), |
| 113 | + child: Text( |
| 114 | + 'Message Reactions', |
| 115 | + style: Theme.of(context).textTheme.headline6, |
| 116 | + ), |
| 117 | + ), |
| 118 | + Flexible( |
| 119 | + child: Padding( |
| 120 | + padding: const EdgeInsets.only( |
| 121 | + left: 16.0, |
| 122 | + right: 16, |
| 123 | + bottom: 16, |
| 124 | + ), |
| 125 | + child: GridView.builder( |
| 126 | + shrinkWrap: true, |
| 127 | + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( |
| 128 | + crossAxisCount: 4, |
| 129 | + crossAxisSpacing: 16, |
| 130 | + childAspectRatio: 0.75, |
| 131 | + mainAxisSpacing: 22, |
| 132 | + ), |
| 133 | + itemCount: message.latestReactions.length, |
| 134 | + itemBuilder: (context, i) { |
| 135 | + final reaction = message.latestReactions[i]; |
| 136 | + |
| 137 | + return _buildReaction( |
| 138 | + reaction, |
| 139 | + currentUser, |
| 140 | + context, |
| 141 | + ); |
| 142 | + }, |
| 143 | + ), |
| 144 | + ), |
| 145 | + ), |
| 146 | + ], |
| 147 | + ), |
| 148 | + ), |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + Column _buildReaction( |
| 153 | + Reaction reaction, |
| 154 | + User currentUser, |
| 155 | + BuildContext context, |
| 156 | + ) { |
| 157 | + final isCurrentUser = reaction.user.id == currentUser.id; |
| 158 | + return Column( |
| 159 | + mainAxisSize: MainAxisSize.min, |
| 160 | + mainAxisAlignment: MainAxisAlignment.start, |
| 161 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 162 | + children: [ |
| 163 | + Stack( |
| 164 | + children: [ |
| 165 | + UserAvatar( |
| 166 | + onTap: onUserAvatarTap, |
| 167 | + user: reaction.user, |
| 168 | + constraints: BoxConstraints.tightFor( |
| 169 | + height: 64, |
| 170 | + width: 64, |
| 171 | + ), |
| 172 | + borderRadius: BorderRadius.circular(32), |
| 173 | + ), |
| 174 | + Positioned( |
| 175 | + child: ReactionBubble( |
| 176 | + reactions: [reaction], |
| 177 | + borderColor: isCurrentUser |
| 178 | + ? messageTheme.ownReactionsBorderColor |
| 179 | + : messageTheme.otherReactionsBorderColor, |
| 180 | + backgroundColor: isCurrentUser |
| 181 | + ? messageTheme.ownReactionsBackgroundColor |
| 182 | + : messageTheme.otherReactionsBackgroundColor, |
| 183 | + flipTail: !isCurrentUser, |
| 184 | + ), |
| 185 | + bottom: 0, |
| 186 | + left: isCurrentUser ? 0 : null, |
| 187 | + right: isCurrentUser ? 0 : null, |
| 188 | + ), |
| 189 | + ], |
| 190 | + ), |
| 191 | + Text( |
| 192 | + reaction.user.name, |
| 193 | + style: Theme.of(context).textTheme.subtitle2, |
| 194 | + textAlign: TextAlign.center, |
| 195 | + ), |
| 196 | + ], |
| 197 | + ); |
| 198 | + } |
| 199 | +} |
0 commit comments