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

77 lines
2.2 KiB
Dart
Raw Normal View History

import 'package:cc_ui_kit/cc_ui_kit.dart';
2026-01-18 20:21:14 +08:00
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 RightJoystickArea 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
// 修复:删除重复的 key 定义
2026-01-19 18:24:24 +08:00
const RightJoystickArea({
2026-04-16 10:48:50 +08:00
super.key,
2026-01-19 18:24:24 +08:00
required this.isLocked,
required this.width,
2026-04-16 10:48:50 +08:00
});
@override
State<RightJoystickArea> createState() => _RightJoystickAreaState();
}
class _RightJoystickAreaState extends State<RightJoystickArea> {
int _lastX = 0;
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,
axisHint: AxisHint.leftRight,
2026-01-18 20:21:14 +08:00
onValueChanged: (value) {
2026-04-16 10:48:50 +08:00
int currentX = value.x.toInt();
if (currentX == _lastX) return;
_lastX = currentX;
int finalX = currentX.abs() < 1 ? 0 : currentX;
debugPrint('右摇杆的数据- x: $finalX, y: 0');
context.read<RemoteControlCubit>().updateOriginX(finalX);
},
onPress: _triggerVibration,
onPanEnd: () async {
debugPrint('🕹️ [右摇杆] 松手,强制 X=0, Y=0');
2026-04-16 10:48:50 +08:00
_lastX = 0;
// 🔥 使用安全方法,一次性清零双轴,确保只发送 (X=0, Y=0)
await context.read<RemoteControlCubit>().stopAllMovement();
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
"左右控制",
style: TextStyle(
2026-04-16 10:48:50 +08:00
color: Colors.white.withOpacity(0.5),
2026-01-18 20:21:14 +08:00
fontSize: 12,
fontWeight: FontWeight.w500,
),
),
],
);
}
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
}