a2a9a7e3c1
Use an outlined settings gear with a running-count badge and slow linear rotation while background tasks are active. Preserve reduced-motion behavior, composer sizing, and tooltip coverage with updated focused tests and notes.
88 lines
2.6 KiB
Dart
88 lines
2.6 KiB
Dart
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]) and a small Material [Badge] shows the count.
|
|
/// With no running task the gear is static and carries no badge. With reduced
|
|
/// motion the gear never rotates but the badge still shows. 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<ChatTaskGear> createState() => _ChatTaskGearState();
|
|
}
|
|
|
|
class _ChatTaskGearState extends State<ChatTaskGear>
|
|
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) {
|
|
final theme = Theme.of(context);
|
|
final running = widget.running;
|
|
return SizedBox.square(
|
|
key: const Key('chat-background-tasks-gear'),
|
|
dimension: widget.size,
|
|
child: Badge(
|
|
key: const Key('chat-background-tasks-badge'),
|
|
isLabelVisible: running > 0,
|
|
backgroundColor: theme.colorScheme.primary,
|
|
textColor: theme.colorScheme.onPrimary,
|
|
label: Text(
|
|
'$running',
|
|
key: const Key('chat-background-tasks-count'),
|
|
),
|
|
child: RotationTransition(
|
|
key: const Key('chat-background-tasks-rotation'),
|
|
turns: _spin,
|
|
child: Icon(Icons.settings_outlined, size: widget.size),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|