import 'package:flutter/material.dart'; /// Background-task glyph for the composer: an outlined settings gear. While /// [running] is above zero the gear turns slowly and steadily (linear, one /// turn per [turnDuration]). The running count is already written beside it /// ("1 task"), so the gear carries no count of its own. With no running task, /// or with reduced motion, the gear is static. Always [size] square so the /// button keeps the same dimensions in every state. class ChatTaskGear extends StatefulWidget { const ChatTaskGear({super.key, required this.running, this.size = 16}); final int running; final double size; /// Time for one full turn while tasks run. static const Duration turnDuration = Duration(seconds: 4); @override State createState() => _ChatTaskGearState(); } class _ChatTaskGearState extends State with SingleTickerProviderStateMixin { late final AnimationController _spin = AnimationController( vsync: this, duration: ChatTaskGear.turnDuration, ); bool? _spinning; @override void didChangeDependencies() { super.didChangeDependencies(); _sync(); } @override void didUpdateWidget(covariant ChatTaskGear oldWidget) { super.didUpdateWidget(oldWidget); _sync(); } /// Turns the gear only while tasks run and motion is allowed. Stopping /// resets to the upright position, so an idle gear is always the same glyph. void _sync() { final reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false; final spinning = widget.running > 0 && !reduced; if (spinning == _spinning) return; _spinning = spinning; if (spinning) { _spin.repeat(); } else { _spin.stop(); _spin.value = 0; } } @override void dispose() { _spin.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return SizedBox.square( key: const Key('chat-background-tasks-gear'), dimension: widget.size, child: RotationTransition( key: const Key('chat-background-tasks-rotation'), turns: _spin, child: Icon(Icons.settings_outlined, size: widget.size), ), ); } }