0812
This commit is contained in:
98
lib/widgets/floating_icon.dart
Normal file
98
lib/widgets/floating_icon.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
import 'package:ai_chat_assistant/services/vehicle_state_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../services/message_service.dart';
|
||||
import '../screens/full_screen.dart';
|
||||
import '../screens/part_screen.dart';
|
||||
|
||||
class FloatingIcon extends StatefulWidget {
|
||||
const FloatingIcon({super.key});
|
||||
|
||||
@override
|
||||
State<FloatingIcon> createState() => _FloatingIconState();
|
||||
}
|
||||
|
||||
class _FloatingIconState extends State<FloatingIcon> {
|
||||
Offset _position = const Offset(10, 120);
|
||||
final iconSize = 80.0;
|
||||
bool _isShowPartScreen = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// VehicleStateService();
|
||||
}
|
||||
|
||||
void _showPartScreen() {
|
||||
setState(() {
|
||||
_isShowPartScreen = true;
|
||||
});
|
||||
}
|
||||
|
||||
void _hidePartScreen() {
|
||||
setState(() {
|
||||
_isShowPartScreen = false;
|
||||
});
|
||||
}
|
||||
|
||||
void _showFullScreen() async {
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const FullScreen(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _updatePosition(Offset delta) {
|
||||
setState(() {
|
||||
final screenSize = MediaQuery.of(context).size;
|
||||
double newX =
|
||||
(_position.dx - delta.dx).clamp(0.0, screenSize.width - iconSize);
|
||||
double newY =
|
||||
(_position.dy - delta.dy).clamp(0.0, screenSize.height - iconSize);
|
||||
_position = Offset(newX, newY);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Stack(
|
||||
children: [
|
||||
if (_isShowPartScreen)
|
||||
PartScreen(
|
||||
onHide: _hidePartScreen,
|
||||
),
|
||||
Positioned(
|
||||
bottom: _position.dy,
|
||||
right: _position.dx,
|
||||
child: Consumer<MessageService>(
|
||||
builder: (context, messageService, child) {
|
||||
return GestureDetector(
|
||||
onTap: _showFullScreen,
|
||||
onLongPress: () async {
|
||||
_showPartScreen();
|
||||
await messageService.startVoiceInput();
|
||||
},
|
||||
onLongPressUp: () async {
|
||||
await messageService.stopAndProcessVoiceInput();
|
||||
final hasMessage = messageService.messages.isNotEmpty;
|
||||
if (!hasMessage) {
|
||||
_hidePartScreen();
|
||||
}
|
||||
},
|
||||
onPanUpdate: (details) => _updatePosition(details.delta),
|
||||
child: Image.asset(
|
||||
messageService.isRecording
|
||||
? 'assets/images/ai2.png'
|
||||
: 'assets/images/ai1.png',
|
||||
width: iconSize,
|
||||
height: iconSize,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user