Files
meshcore-open/lib/widgets/message_bubble_frame.dart

152 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import '../theme/mesh_theme.dart';
/// Colours shared by the direct-message and channel chat bubbles.
@immutable
class MessageBubbleColors {
final Color background;
final Color border;
final Color text;
final Color meta;
const MessageBubbleColors({
required this.background,
required this.border,
required this.text,
required this.meta,
});
/// Outgoing bubbles use the theme's [MeshBubbleTheme]; a failed send
/// switches to the error container.
factory MessageBubbleColors.of(
BuildContext context, {
required bool isOutgoing,
bool isFailed = false,
}) {
final scheme = Theme.of(context).colorScheme;
final bubble = MeshBubbleTheme.of(context);
final text = isFailed
? scheme.onErrorContainer
: isOutgoing
? bubble.meInk
: scheme.onSurface;
return MessageBubbleColors(
background: isFailed
? scheme.errorContainer
: isOutgoing
? bubble.me
: scheme.surfaceContainerLow,
border: isFailed
? scheme.error
: isOutgoing
? bubble.meBorder
: scheme.outlineVariant,
text: text,
meta: text.withValues(alpha: 0.65),
);
}
}
/// Asymmetric bubble radius: the corner nearest the sender is tight.
BorderRadius messageBubbleRadius({required bool isOutgoing}) {
return isOutgoing
? const BorderRadius.only(
topLeft: Radius.circular(MeshRadii.lg),
topRight: Radius.circular(MeshRadii.lg),
bottomLeft: Radius.circular(MeshRadii.lg),
bottomRight: Radius.circular(MeshRadii.xs),
)
: const BorderRadius.only(
topLeft: Radius.circular(MeshRadii.xs),
topRight: Radius.circular(MeshRadii.lg),
bottomLeft: Radius.circular(MeshRadii.lg),
bottomRight: Radius.circular(MeshRadii.lg),
);
}
/// Extra sender-name hues beyond the [MeshPalette] accents. They match the
/// two extra hues [AvatarCircle] uses, so a name and its avatar agree.
const Color kSenderHuePeriwinkle = Color(0xFF8FA8F0);
const Color kSenderHueAqua = Color(0xFF6FD9CE);
const List<Color> _senderHues = [
MeshPalette.blue,
MeshPalette.magenta,
MeshPalette.signal,
MeshPalette.warn,
kSenderHuePeriwinkle,
kSenderHueAqua,
];
/// Deterministic name-to-hue mapping consistent with [AvatarCircle].
Color senderNameColor(String name) {
var h = 0;
for (final c in name.codeUnits) {
h = (h * 31 + c) & 0x7fffffff;
}
return _senderHues[h % _senderHues.length];
}
/// The bubble container shared by both chat screens: colour, border,
/// asymmetric radius, padding and the 72% max width.
class MessageBubbleFrame extends StatelessWidget {
final bool isOutgoing;
final bool isFailed;
final bool isBookmarked;
/// Tighter padding for media (GIF) content.
final bool compact;
final double maxWidth;
final Widget child;
const MessageBubbleFrame({
super.key,
required this.isOutgoing,
required this.maxWidth,
required this.child,
this.isFailed = false,
this.isBookmarked = false,
this.compact = false,
});
@override
Widget build(BuildContext context) {
final colors = MessageBubbleColors.of(
context,
isOutgoing: isOutgoing,
isFailed: isFailed,
);
final contentPadding = compact
? const EdgeInsets.all(4)
: const EdgeInsets.symmetric(horizontal: 12, vertical: 8);
return Stack(
clipBehavior: Clip.none,
children: [
Container(
constraints: BoxConstraints(maxWidth: maxWidth * 0.72),
decoration: BoxDecoration(
color: colors.background,
borderRadius: messageBubbleRadius(isOutgoing: isOutgoing),
border: Border.all(color: colors.border, width: 1),
),
child: Padding(
padding: isBookmarked
? contentPadding.add(
EdgeInsets.only(top: compact ? 18 : 14, right: 14),
)
: contentPadding,
child: child,
),
),
if (isBookmarked)
const Positioned(
top: -2,
right: 12,
child: Icon(Icons.bookmark, size: 22, color: Color(0xFFFFE54A)),
),
],
);
}
}