Wash the operator's bubbles softly in the chat colour

A coloured chat's own messages blend 22% of its shade over the host's
neutral bubble fill; text keeps the host colours and uncoloured chats
are unchanged.
This commit is contained in:
2026-11-10 11:27:29 +00:00
parent 4ae2de444e
commit 19aa726e7f
3 changed files with 500 additions and 335 deletions
@@ -0,0 +1,125 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_bubble.dart';
void main() {
Future<void> pump(
WidgetTester tester, {
required bool cupertino,
String? color,
Brightness brightness = Brightness.light,
}) async {
final bubble = ChatBubbleColor(
name: color,
child: ChatUserBubble(cupertino: cupertino, child: const Text('Hello')),
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
theme: CupertinoThemeData(brightness: brightness),
home: Center(child: bubble),
)
: MaterialApp(
theme: ThemeData(brightness: brightness),
home: Scaffold(body: Center(child: bubble)),
),
);
}
Color materialFill(WidgetTester tester) => tester
.widget<Material>(
find
.descendant(
of: find.byType(ChatUserBubble),
matching: find.byType(Material),
)
.first,
)
.color!;
Color cupertinoFill(WidgetTester tester) =>
(tester
.widget<DecoratedBox>(
find
.descendant(
of: find.byType(ChatUserBubble),
matching: find.byType(DecoratedBox),
)
.first,
)
.decoration
as BoxDecoration)
.color!;
for (final brightness in Brightness.values) {
testWidgets('Material ${brightness.name}: a coloured chat washes the '
'bubble softly; an uncoloured chat keeps the host fill', (
tester,
) async {
await pump(tester, cupertino: false, brightness: brightness);
final neutral = materialFill(tester);
final context = tester.element(find.byType(ChatUserBubble));
expect(neutral, Theme.of(context).colorScheme.surfaceContainerHighest);
await pump(
tester,
cupertino: false,
color: 'teal',
brightness: brightness,
);
final tinted = materialFill(tester);
expect(tinted, isNot(neutral));
expect(tinted.a, 1);
// Soft: every channel stays within the tint's share of the neutral fill.
for (final (a, b) in [
(tinted.r, neutral.r),
(tinted.g, neutral.g),
(tinted.b, neutral.b),
]) {
expect((a - b).abs(), lessThanOrEqualTo(chatBubbleTint + 0.01));
}
// The text keeps the host's colour.
final style = DefaultTextStyle.of(
tester.element(find.text('Hello')),
).style;
await pump(tester, cupertino: false, brightness: brightness);
expect(
DefaultTextStyle.of(tester.element(find.text('Hello'))).style.color,
style.color,
);
});
}
testWidgets('Cupertino: a coloured chat washes the bubble; none keeps grey', (
tester,
) async {
await pump(tester, cupertino: true);
final neutral = cupertinoFill(tester);
expect(
neutral,
CupertinoColors.systemGrey5.resolveFrom(
tester.element(find.byType(ChatUserBubble)),
),
);
await pump(tester, cupertino: true, color: 'blue');
expect(cupertinoFill(tester), isNot(neutral));
await pump(tester, cupertino: true, color: 'not-a-colour');
expect(cupertinoFill(tester), neutral);
});
testWidgets('a bubble with no colour scope stays neutral', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ChatUserBubble(cupertino: false, child: Text('Hello')),
),
),
);
final context = tester.element(find.byType(ChatUserBubble));
expect(
materialFill(tester),
Theme.of(context).colorScheme.surfaceContainerHighest,
);
});
}