f3a469c356
Replace the running Stop button with an animated composer border and task spinner. Add Escape, slash-command, and touch-menu stopping paths with focused widget coverage, including reduced-motion behavior.
72 lines
1.6 KiB
Dart
72 lines
1.6 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
/// Turns [child] once every [period] while [spinning]; static otherwise. With
|
|
/// reduced motion it never turns. Hidden UI is silenced by TickerMode.
|
|
class ChatTaskSpinner extends StatefulWidget {
|
|
const ChatTaskSpinner({
|
|
super.key,
|
|
required this.spinning,
|
|
required this.child,
|
|
});
|
|
|
|
final bool spinning;
|
|
final Widget child;
|
|
|
|
/// One full turn while tasks run.
|
|
static const Duration period = Duration(milliseconds: 2500);
|
|
|
|
@override
|
|
State<ChatTaskSpinner> createState() => _ChatTaskSpinnerState();
|
|
}
|
|
|
|
class _ChatTaskSpinnerState extends State<ChatTaskSpinner>
|
|
with SingleTickerProviderStateMixin {
|
|
late final AnimationController _turns = AnimationController(
|
|
vsync: this,
|
|
duration: ChatTaskSpinner.period,
|
|
);
|
|
bool _reduced = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_sync();
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
_reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false;
|
|
_sync();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatTaskSpinner oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
_sync();
|
|
}
|
|
|
|
void _sync() {
|
|
if (widget.spinning && !_reduced) {
|
|
if (!_turns.isAnimating) _turns.repeat();
|
|
} else {
|
|
_turns
|
|
..stop()
|
|
..value = 0;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_turns.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => RotationTransition(
|
|
key: const Key('chat-background-tasks-spinner'),
|
|
turns: _turns,
|
|
child: widget.child,
|
|
);
|
|
}
|