Files
flutterApp/lib/features/remote_control/presentation/widgets/right_joystick_area.dart
2026-04-16 10:48:50 +08:00

75 lines
2.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 RightJoystickArea extends StatefulWidget {
final bool isLocked;
final double width;
// 修复:删除重复的 key 定义
const RightJoystickArea({
super.key,
required this.isLocked,
required this.width,
});
@override
State<RightJoystickArea> createState() => _RightJoystickAreaState();
}
class _RightJoystickAreaState extends State<RightJoystickArea> {
int _lastX = 0;
@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.leftRight,
onValueChanged: (value) {
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: () {
debugPrint('🕹️ [右摇杆] 松手,强制X=0');
_lastX = 0;
context.read<RemoteControlCubit>().updateOriginX(0);
},
),
),
),
const SizedBox(height: 18),
Text(
"左右控制",
style: TextStyle(
color: Colors.white.withOpacity(0.5),
fontSize: 12,
fontWeight: FontWeight.w500,
),
),
],
);
}
void _triggerVibration() {
Vibration.hasVibrator().then((has) {
if (has ?? false) Vibration.vibrate(duration: 12);
});
}
}