Files
flutterApp/lib/features/remote_control/presentation/widgets/left_joystick_area.dart

92 lines
2.6 KiB
Dart
Raw Normal View History

2026-04-16 10:48:50 +08:00
import 'dart:async';
2026-01-18 20:21:14 +08:00
import 'package:cc_ui_kit/cc_ui_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:vibration/vibration.dart';
2026-01-18 20:21:14 +08:00
import '../bloc/remote_control_cubit.dart';
2026-04-16 10:48:50 +08:00
class LeftJoystickArea extends StatefulWidget {
2026-01-18 20:21:14 +08:00
final bool isLocked;
2026-01-19 18:24:24 +08:00
final double width;
2026-01-18 20:21:14 +08:00
2026-04-16 10:48:50 +08:00
const LeftJoystickArea({
Key? key,
required this.isLocked,
required this.width,
}) : super(key: key);
@override
State<LeftJoystickArea> createState() => _LeftJoystickAreaState();
}
class _LeftJoystickAreaState extends State<LeftJoystickArea> {
int _lastSentY = 0;
bool _isTouching = false;
2026-01-18 20:21:14 +08:00
@override
Widget build(BuildContext context) {
return Column(
2026-04-16 10:48:50 +08:00
mainAxisSize: MainAxisSize.min,
2026-01-18 20:21:14 +08:00
children: [
IgnorePointer(
2026-04-16 10:48:50 +08:00
ignoring: widget.isLocked,
2026-01-18 20:21:14 +08:00
child: AnimatedOpacity(
duration: const Duration(milliseconds: 300),
2026-04-16 10:48:50 +08:00
opacity: widget.isLocked ? 0.3 : 1.0,
2026-01-18 20:21:14 +08:00
child: CCJoystick(
2026-04-16 10:48:50 +08:00
radius: widget.width,
2026-01-18 20:21:14 +08:00
axisHint: AxisHint.forwardBackward,
onValueChanged: (value) {
2026-04-16 10:48:50 +08:00
// 标记:正在触摸
_isTouching = true;
int currentY = value.y.toInt();
if (currentY != _lastSentY) {
_lastSentY = currentY;
debugPrint('左摇杆的数据- x: 0, y: $currentY');
context.read<RemoteControlCubit>().updateOriginY(currentY);
}
2026-01-18 20:21:14 +08:00
},
onPress: () {
2026-04-16 10:48:50 +08:00
_isTouching = true;
debugPrint('onPress');
_triggerVibration();
2026-01-18 20:21:14 +08:00
},
onPanEnd: () async {
2026-04-16 10:48:50 +08:00
// 🔥 松手 100% 归零
debugPrint('onPanEnd');
await _stopJoystick();
2026-04-16 10:48:50 +08:00
},
2026-01-18 20:21:14 +08:00
),
),
),
2026-01-19 18:24:24 +08:00
const SizedBox(height: 18),
Text(
2026-01-18 20:21:14 +08:00
"前后控制",
2026-04-16 10:48:50 +08:00
style: TextStyle(
color: Colors.white.withOpacity(0.5),
fontSize: 12,
fontWeight: FontWeight.w500,
),
2026-01-18 20:21:14 +08:00
),
],
);
}
2026-04-16 10:33:52 +08:00
2026-04-16 10:48:50 +08:00
// 统一停车方法(万能保险)
Future<void> _stopJoystick() async {
2026-04-16 10:48:50 +08:00
_isTouching = false;
_lastSentY = 0;
debugPrint('✅ 摇杆已停止 -> 强制 X=0, Y=0 停车');
// 🔥 使用安全方法,一次性清零双轴,确保只发送 (X=0, Y=0)
await context.read<RemoteControlCubit>().stopAllMovement();
2026-04-16 10:48:50 +08:00
}
void _triggerVibration() {
2026-04-16 10:48:50 +08:00
Vibration.hasVibrator().then((has) {
if (has ?? false) Vibration.vibrate(duration: 12);
});
}
2026-04-16 10:48:50 +08:00
}