增加悬浮“众众”的定时图片切换效果

This commit is contained in:
2025-08-15 10:46:33 +08:00
parent 1302cd34a9
commit ae3289dc29
2 changed files with 19 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ import '../services/message_service.dart';
import '../screens/full_screen.dart';
import '../screens/part_screen.dart';
import 'floating_icon_with_wave.dart';
import 'dart:async'; // 添加此行
class FloatingIcon extends StatefulWidget {
const FloatingIcon({super.key});
@@ -17,22 +18,36 @@ class _FloatingIconState extends State<FloatingIcon>
Offset _position = const Offset(10, 120);
final iconSize = 80.0;
bool _isShowPartScreen = false;
late AnimationController _waveAnimationController;
// 新增:图片切换相关
int _imageIndex = 0;
late final List<String> _iconImages = [
'assets/images/ai1_hd.png',
'assets/images/ai0_hd.png',
];
Timer? _imageTimer; // 用于定时切换图片
@override
void initState() {
super.initState();
// VehicleStateService();
_waveAnimationController = AnimationController(
duration: const Duration(milliseconds: 1000),
vsync: this,
);
// 使用Timer.periodic定时切换图片
_imageTimer = Timer.periodic(const Duration(seconds: 3), (timer) {
setState(() {
_imageIndex = (_imageIndex + 1) % _iconImages.length;
});
});
}
@override
void dispose() {
_waveAnimationController.dispose();
_imageTimer?.cancel(); // 释放Timer
super.dispose();
}
@@ -80,7 +95,6 @@ class _FloatingIconState extends State<FloatingIcon>
right: _position.dx,
child: Consumer<MessageService>(
builder: (context, messageService, child) {
// 立即响应录音状态变化,控制动画
WidgetsBinding.instance.addPostFrameCallback((_) {
if (messageService.isRecording) {
if (!_waveAnimationController.isAnimating) {
@@ -92,17 +106,15 @@ class _FloatingIconState extends State<FloatingIcon>
}
}
});
return GestureDetector(
onTap: _showFullScreen,
onLongPress: () async {
_showPartScreen();
// 立即启动动画
_waveAnimationController.repeat();
await messageService.startVoiceInput();
},
onLongPressUp: () async {
// 立即停止动画
_waveAnimationController.stop();
await messageService.stopAndProcessVoiceInput();
final hasMessage = messageService.messages.isNotEmpty;
@@ -118,7 +130,7 @@ class _FloatingIconState extends State<FloatingIcon>
waveColor: Colors.white,
)
: Image.asset(
'assets/images/ai1_hd.png',
_iconImages[_imageIndex],
width: iconSize,
height: iconSize,
),