Files
flutterApp/lib/features/remote_control/presentation/widgets/left_joystick_area.dart
2026-04-16 17:39:54 +08:00

92 lines
2.6 KiB
Dart

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