修复emit异步导致的竞态+tcp缓冲取挤压+网络堵塞+定时器恢复的造成归0被吞的情况

完成APP端释放权限的接口的集成和数据返回获取
This commit is contained in:
2026-04-16 17:39:54 +08:00
parent 407bcf026c
commit 976c5fca2f
8 changed files with 260 additions and 108 deletions

View File

@@ -14,7 +14,7 @@ class CCJoystick extends StatefulWidget {
final AxisHint axisHint;
final Function(JoystickValue) onValueChanged;
final VoidCallback? onPress;
final VoidCallback? onPanEnd; // 🔥 新增:松手回调
final Future<void> Function()? onPanEnd; // 🔥 修复:改为异步回调
final double radius;
const CCJoystick({
@@ -34,6 +34,7 @@ class _CustomJoystickState extends State<CCJoystick>
with SingleTickerProviderStateMixin {
Offset _offset = Offset.zero;
late AnimationController _floatController;
bool _isPanning = false; // 🔥 跟踪是否正在拖拽
@override
void initState() {
@@ -71,47 +72,73 @@ class _CustomJoystickState extends State<CCJoystick>
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque, // 🔥 改为 opaque,确保所有触摸事件都被捕获
onPanStart: (_) => widget.onPress?.call(),
onPanUpdate: (details) {
setState(() {
Offset newOffset = _offset + details.delta;
double distance = newOffset.distance;
// 限制在圆圈内
if (distance <= widget.radius) {
_offset = newOffset;
} else {
double angle = atan2(newOffset.dy, newOffset.dx);
_offset = Offset(
cos(angle) * widget.radius,
sin(angle) * widget.radius,
);
}
});
_updateValue();
return Listener(
// 🔥 兜底:监听全局指针抬起事件,防止 onPanEnd 丢失
onPointerUp: (_) async {
if (_isPanning) {
debugPrint('🛡️ [兜底] 全局指针抬起,强制归零');
await _handlePanEnd();
}
},
onPanEnd: (_) {
setState(() => _offset = Offset.zero); // 归位
_updateValue();
widget.onPanEnd?.call(); // 🔥 调用松手回调
},
child: AnimatedBuilder(
animation: _floatController,
builder: (context, child) {
return CustomPaint(
size: Size(widget.radius * 2, widget.radius * 2),
painter: JoystickPainter(
offset: _offset,
radius: widget.radius,
axisHint: widget.axisHint,
floatValue: _floatController.value * 10 - 5, // -5f 到 5f
),
);
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onPanStart: (_) {
_isPanning = true;
widget.onPress?.call();
},
onPanUpdate: (details) {
setState(() {
Offset newOffset = _offset + details.delta;
double distance = newOffset.distance;
// 限制在圆圈内
if (distance <= widget.radius) {
_offset = newOffset;
} else {
double angle = atan2(newOffset.dy, newOffset.dx);
_offset = Offset(
cos(angle) * widget.radius,
sin(angle) * widget.radius,
);
}
});
_updateValue();
},
onPanEnd: (_) async {
debugPrint('✋ [正常] onPanEnd 触发');
await _handlePanEnd();
},
onPanCancel: () async {
// 🔥 新增:处理手势取消(如来电中断)
debugPrint('⚠️ [异常] onPanCancel 触发');
await _handlePanEnd();
},
child: AnimatedBuilder(
animation: _floatController,
builder: (context, child) {
return CustomPaint(
size: Size(widget.radius * 2, widget.radius * 2),
painter: JoystickPainter(
offset: _offset,
radius: widget.radius,
axisHint: widget.axisHint,
floatValue: _floatController.value * 10 - 5, // -5f 到 5f
),
);
},
),
),
);
}
// 🔥 统一的松手处理逻辑
Future<void> _handlePanEnd() async {
if (!_isPanning) return;
_isPanning = false;
setState(() => _offset = Offset.zero); // 归位
// 🔥 关键修复:不调用 _updateValue(),避免触发 onValueChanged 导致重复发送
// _updateValue(); // ❌ 注释掉,防止发送错误的停止指令
await widget.onPanEnd?.call(); // 🔥 等待异步回调完成
}
}
class JoystickPainter extends CustomPainter {