75 lines
2.1 KiB
Dart
75 lines
2.1 KiB
Dart
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);
|
||
});
|
||
}
|
||
} |