2025-08-12 13:36:42 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
import '../services/message_service.dart';
|
2025-09-22 17:15:44 +08:00
|
|
|
|
import '../pages/full_screen.dart';
|
2025-08-12 13:36:42 +08:00
|
|
|
|
import '../screens/part_screen.dart';
|
2025-08-12 17:40:39 +08:00
|
|
|
|
import 'floating_icon_with_wave.dart';
|
2025-09-17 18:07:38 +08:00
|
|
|
|
import 'dart:async';
|
2025-09-24 15:42:30 +08:00
|
|
|
|
import 'package:t_basic_intl/intl.dart';
|
2025-09-19 11:40:38 +08:00
|
|
|
|
import '../utils/assets_util.dart';
|
2025-08-12 13:36:42 +08:00
|
|
|
|
|
|
|
|
|
|
class FloatingIcon extends StatefulWidget {
|
|
|
|
|
|
const FloatingIcon({super.key});
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
State<FloatingIcon> createState() => _FloatingIconState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-17 18:07:38 +08:00
|
|
|
|
class _FloatingIconState extends State<FloatingIcon> with TickerProviderStateMixin {
|
2025-08-12 13:36:42 +08:00
|
|
|
|
Offset _position = const Offset(10, 120);
|
|
|
|
|
|
final iconSize = 80.0;
|
|
|
|
|
|
bool _isShowPartScreen = false;
|
2025-09-17 18:07:38 +08:00
|
|
|
|
bool _isDragging = false;
|
2025-08-15 10:46:33 +08:00
|
|
|
|
|
2025-09-17 18:07:38 +08:00
|
|
|
|
bool _isAnimating = false;
|
|
|
|
|
|
Offset _targetPosition = const Offset(10, 120);
|
|
|
|
|
|
|
|
|
|
|
|
// 水波纹动画控制器
|
2025-08-12 17:40:39 +08:00
|
|
|
|
late AnimationController _waveAnimationController;
|
2025-09-17 18:07:38 +08:00
|
|
|
|
// PartScreen 显示动画控制器
|
|
|
|
|
|
late AnimationController _partScreenAnimationController;
|
|
|
|
|
|
// 图片切换定时器
|
|
|
|
|
|
Timer? _imageTimer;
|
|
|
|
|
|
// 添加长按状态标记
|
|
|
|
|
|
bool _isLongPressing = false;
|
2025-08-12 13:36:42 +08:00
|
|
|
|
|
2025-09-17 18:07:38 +08:00
|
|
|
|
// 图片切换相关
|
2025-08-15 10:46:33 +08:00
|
|
|
|
int _imageIndex = 0;
|
|
|
|
|
|
late final List<String> _iconImages = [
|
2025-09-19 11:40:38 +08:00
|
|
|
|
'ai1_hd.png',
|
|
|
|
|
|
'ai0_hd.png',
|
2025-08-15 10:46:33 +08:00
|
|
|
|
];
|
2025-08-20 16:29:50 +08:00
|
|
|
|
|
|
|
|
|
|
late final List<String> _iconImagesEn = [
|
2025-09-19 11:40:38 +08:00
|
|
|
|
'ai1_hd_en.png',
|
|
|
|
|
|
'ai0_hd_en.png',
|
2025-08-20 16:29:50 +08:00
|
|
|
|
];
|
2025-08-15 10:46:33 +08:00
|
|
|
|
|
2025-08-12 13:36:42 +08:00
|
|
|
|
@override
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
super.initState();
|
2025-08-12 17:40:39 +08:00
|
|
|
|
_waveAnimationController = AnimationController(
|
|
|
|
|
|
duration: const Duration(milliseconds: 1000),
|
|
|
|
|
|
vsync: this,
|
|
|
|
|
|
);
|
2025-09-17 18:07:38 +08:00
|
|
|
|
|
|
|
|
|
|
// PartScreen动画控制器
|
|
|
|
|
|
_partScreenAnimationController = AnimationController(
|
|
|
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
|
|
vsync: this,
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 图片切换定时器
|
2025-08-15 10:46:33 +08:00
|
|
|
|
_imageTimer = Timer.periodic(const Duration(seconds: 3), (timer) {
|
2025-09-17 18:07:38 +08:00
|
|
|
|
if (mounted && !_isDragging) {
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_imageIndex = (_imageIndex + 1) % _iconImages.length;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-08-15 10:46:33 +08:00
|
|
|
|
});
|
2025-08-12 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
|
_waveAnimationController.dispose();
|
2025-09-17 18:07:38 +08:00
|
|
|
|
_partScreenAnimationController.dispose();
|
|
|
|
|
|
_imageTimer?.cancel();
|
2025-08-12 17:40:39 +08:00
|
|
|
|
super.dispose();
|
2025-08-12 13:36:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _showPartScreen() {
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_isShowPartScreen = true;
|
|
|
|
|
|
});
|
2025-09-17 18:07:38 +08:00
|
|
|
|
_partScreenAnimationController.forward();
|
2025-08-12 13:36:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _hidePartScreen() {
|
2025-09-17 18:07:38 +08:00
|
|
|
|
_partScreenAnimationController.reverse().then((_) {
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_isShowPartScreen = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-08-12 13:36:42 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-17 18:07:38 +08:00
|
|
|
|
// 显示全屏界面
|
2025-08-12 13:36:42 +08:00
|
|
|
|
void _showFullScreen() async {
|
2025-08-22 17:35:02 +08:00
|
|
|
|
_hidePartScreen();
|
2025-09-19 11:40:38 +08:00
|
|
|
|
final messageService = MessageService.instance;
|
2025-09-18 15:53:39 +08:00
|
|
|
|
|
2025-08-12 13:36:42 +08:00
|
|
|
|
await Navigator.of(context).push(
|
|
|
|
|
|
MaterialPageRoute(
|
2025-09-18 15:53:39 +08:00
|
|
|
|
builder: (context) => ChangeNotifierProvider.value(
|
|
|
|
|
|
value: messageService, // 传递同一个单例实例
|
2025-09-22 17:15:44 +08:00
|
|
|
|
child: const FullScreenPage(),
|
2025-09-18 15:53:39 +08:00
|
|
|
|
),
|
2025-08-12 13:36:42 +08:00
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-17 18:07:38 +08:00
|
|
|
|
// 更新位置
|
2025-08-12 13:36:42 +08:00
|
|
|
|
void _updatePosition(Offset delta) {
|
2025-09-17 18:07:38 +08:00
|
|
|
|
if (!_isDragging) return;
|
|
|
|
|
|
|
2025-08-12 13:36:42 +08:00
|
|
|
|
setState(() {
|
|
|
|
|
|
final screenSize = MediaQuery.of(context).size;
|
2025-09-17 18:07:38 +08:00
|
|
|
|
double newX = (_position.dx - delta.dx).clamp(0.0, screenSize.width - iconSize);
|
|
|
|
|
|
double newY = (_position.dy - delta.dy).clamp(0.0, screenSize.height - iconSize);
|
2025-08-12 13:36:42 +08:00
|
|
|
|
_position = Offset(newX, newY);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-17 18:07:38 +08:00
|
|
|
|
// 吸附到屏幕边缘
|
|
|
|
|
|
void _snapToEdge() {
|
|
|
|
|
|
final screenSize = MediaQuery.of(context).size;
|
|
|
|
|
|
final centerX = screenSize.width / 2;
|
|
|
|
|
|
|
|
|
|
|
|
// 判断应该吸到左边还是右边
|
|
|
|
|
|
final shouldSnapToLeft = _position.dx < centerX - iconSize / 2;
|
|
|
|
|
|
final targetX = shouldSnapToLeft ? 0.0 : screenSize.width - iconSize;
|
|
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_targetPosition = Offset(targetX, _position.dy);
|
|
|
|
|
|
_isAnimating = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 使用定时器在动画完成后更新状态
|
|
|
|
|
|
Timer(const Duration(milliseconds: 100), () {
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_position = _targetPosition;
|
|
|
|
|
|
_isAnimating = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _onPanStart(DragStartDetails details) {
|
|
|
|
|
|
_isDragging = true;
|
|
|
|
|
|
_waveAnimationController.stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void _onPanEnd(DragEndDetails details) {
|
|
|
|
|
|
_isDragging = false;
|
|
|
|
|
|
_snapToEdge();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 13:36:42 +08:00
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-17 18:07:38 +08:00
|
|
|
|
// 奇怪的代码,不应该放在build里面
|
|
|
|
|
|
// WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
|
// if (messageService.isRecording) {
|
|
|
|
|
|
// if (!_waveAnimationController.isAnimating) {
|
|
|
|
|
|
// _waveAnimationController.repeat();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// if (_waveAnimationController.isAnimating) {
|
|
|
|
|
|
// _waveAnimationController.stop();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
2025-09-18 15:53:39 +08:00
|
|
|
|
|
2025-09-19 11:40:38 +08:00
|
|
|
|
return ChangeNotifierProvider.value(
|
|
|
|
|
|
value: MessageService.instance,
|
|
|
|
|
|
child: _buildFloatingIcon(),
|
|
|
|
|
|
);
|
2025-09-18 15:53:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Widget _buildFloatingIcon() {
|
2025-08-12 13:36:42 +08:00
|
|
|
|
return Stack(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
if (_isShowPartScreen)
|
2025-09-17 18:07:38 +08:00
|
|
|
|
FadeTransition(
|
|
|
|
|
|
opacity: _partScreenAnimationController,
|
|
|
|
|
|
child: SlideTransition(
|
|
|
|
|
|
position: Tween<Offset>(
|
|
|
|
|
|
begin: const Offset(0, 0.3),
|
|
|
|
|
|
end: Offset.zero,
|
|
|
|
|
|
).animate(CurvedAnimation(
|
|
|
|
|
|
parent: _partScreenAnimationController,
|
|
|
|
|
|
curve: Curves.easeOutQuart,
|
|
|
|
|
|
)),
|
|
|
|
|
|
child: PartScreen(
|
|
|
|
|
|
onHide: _hidePartScreen,
|
|
|
|
|
|
floatingIconPosition: _position,
|
|
|
|
|
|
iconSize: iconSize,
|
|
|
|
|
|
)),
|
2025-08-12 13:36:42 +08:00
|
|
|
|
),
|
2025-09-17 18:07:38 +08:00
|
|
|
|
AnimatedPositioned(
|
|
|
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
|
|
curve: Curves.easeOutBack,
|
|
|
|
|
|
bottom: _isAnimating ? _targetPosition.dy : _position.dy,
|
|
|
|
|
|
right: _isAnimating ? _targetPosition.dx : _position.dx,
|
2025-08-12 13:36:42 +08:00
|
|
|
|
child: Consumer<MessageService>(
|
|
|
|
|
|
builder: (context, messageService, child) {
|
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
|
onTap: _showFullScreen,
|
|
|
|
|
|
onLongPress: () async {
|
2025-09-17 18:07:38 +08:00
|
|
|
|
debugPrint('Long press');
|
|
|
|
|
|
_isLongPressing = true; // 标记开始长按
|
2025-08-12 13:36:42 +08:00
|
|
|
|
_showPartScreen();
|
2025-08-12 17:40:39 +08:00
|
|
|
|
_waveAnimationController.repeat();
|
2025-08-12 13:36:42 +08:00
|
|
|
|
await messageService.startVoiceInput();
|
|
|
|
|
|
},
|
|
|
|
|
|
onLongPressUp: () async {
|
2025-09-17 18:07:38 +08:00
|
|
|
|
debugPrint('Long press up');
|
|
|
|
|
|
_isLongPressing = false; // 清除长按标记
|
2025-08-12 17:40:39 +08:00
|
|
|
|
_waveAnimationController.stop();
|
2025-08-12 13:36:42 +08:00
|
|
|
|
await messageService.stopAndProcessVoiceInput();
|
|
|
|
|
|
final hasMessage = messageService.messages.isNotEmpty;
|
|
|
|
|
|
if (!hasMessage) {
|
|
|
|
|
|
_hidePartScreen();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-09-17 18:07:38 +08:00
|
|
|
|
onLongPressEnd: (d) {
|
|
|
|
|
|
debugPrint('Long press end $d');
|
|
|
|
|
|
_isLongPressing = false; // 清除长按标记
|
|
|
|
|
|
},
|
|
|
|
|
|
onPanStart: (details) {
|
|
|
|
|
|
// 只有在非长按状态下才允许拖拽
|
|
|
|
|
|
if (!_isLongPressing) {
|
|
|
|
|
|
_onPanStart(details);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
onPanUpdate: (details) {
|
|
|
|
|
|
// 只有在拖拽状态下才更新位置
|
|
|
|
|
|
if (_isDragging && !_isLongPressing) {
|
|
|
|
|
|
_updatePosition(details.delta);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
onPanEnd: (details) {
|
|
|
|
|
|
if (!_isLongPressing) {
|
|
|
|
|
|
_onPanEnd(details);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
child: AnimatedScale(
|
|
|
|
|
|
scale: _isDragging ? 1.1 : 1.0,
|
|
|
|
|
|
duration: const Duration(milliseconds: 150),
|
|
|
|
|
|
child: messageService.isRecording
|
|
|
|
|
|
? FloatingIconWithWave(
|
|
|
|
|
|
animationController: _waveAnimationController,
|
|
|
|
|
|
iconSize: iconSize,
|
|
|
|
|
|
waveColor: Colors.white,
|
|
|
|
|
|
)
|
2025-09-19 11:40:38 +08:00
|
|
|
|
: AssetsUtil.getImageWidget(
|
2025-09-17 18:07:38 +08:00
|
|
|
|
Intl.getCurrentLocale().startsWith('zh')
|
|
|
|
|
|
? _iconImages[_imageIndex]
|
|
|
|
|
|
: _iconImagesEn[_imageIndex],
|
|
|
|
|
|
width: iconSize,
|
|
|
|
|
|
height: iconSize,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2025-08-12 13:36:42 +08:00
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
2025-09-17 18:07:38 +08:00
|
|
|
|
)
|
2025-08-12 13:36:42 +08:00
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|