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 createState() => _ChatResponsePendingState(); } class _ChatResponsePendingState extends State 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) => Semantics( label: 'Assistant is responding', liveRegion: true, excludeSemantics: true, child: AnimatedBuilder( animation: _animation, builder: (context, _) => Row( mainAxisSize: MainAxisSize.min, children: [ for (var index = 0; index < 3; index++) Opacity( opacity: MediaQuery.disableAnimationsOf(context) ? 1 : 0.35 + 0.65 * (0.5 + 0.5 * math.sin( 2 * math.pi * (_animation.value - index / 3), )), child: const Text('.'), ), ], ), ), ); }