Files
pipelines/app/lib/features/chat/ui/transcript/chat_response_pending.dart
T

100 lines
3.3 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/widgets.dart';
/// A local animation inside the normal assistant bubble, before answer text.
/// It never rebuilds the transcript or changes its scroll position on a tick.
class ChatResponsePending extends StatefulWidget {
const ChatResponsePending({super.key});
@override
State<ChatResponsePending> createState() => _ChatResponsePendingState();
}
class _ChatResponsePendingState extends State<ChatResponsePending>
with SingleTickerProviderStateMixin {
late final AnimationController _animation = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1200),
);
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (MediaQuery.disableAnimationsOf(context)) {
_animation.stop();
} else {
_animation.repeat();
}
}
@override
void dispose() {
_animation.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Round dots sized from the surrounding text, so they sit on the text's
// middle (a period glyph sits on the baseline and reads low) and follow
// text scaling and colour.
final style = DefaultTextStyle.of(context).style;
final fontSize = MediaQuery.textScalerOf(
context,
).scale(style.fontSize ?? 14);
final dot = fontSize * 0.3;
final color = style.color ?? const Color(0xFF808080);
return Semantics(
label: 'Assistant is responding',
liveRegion: true,
excludeSemantics: true,
// A fixed box (the three dots and their two gaps) with its own
// builder: each tick rebuilds the dots and lays out only this box, not
// the transcript row's builder and the list around it.
child: SizedBox(
key: const Key('chat-response-pending-dots'),
height: fontSize,
width: dot * 4.2,
child: LayoutBuilder(
builder: (context, _) => AnimatedBuilder(
animation: _animation,
builder: (context, _) => Row(
mainAxisSize: MainAxisSize.min,
children: [
for (var index = 0; index < 3; index++)
Padding(
padding: EdgeInsetsDirectional.only(
start: index == 0 ? 0 : dot * 0.6,
),
child: Opacity(
opacity: MediaQuery.disableAnimationsOf(context)
? 1
: 0.35 +
0.65 *
(0.5 +
0.5 *
math.sin(
2 *
math.pi *
(_animation.value -
index / 3),
)),
child: DecoratedBox(
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
),
child: SizedBox.square(dimension: dot),
),
),
),
],
),
),
),
),
);
}
}