ios_edge_swipe_detector.dart
996 Bytes
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
class IosEdgeSwipeDetector {
static const MethodChannel _channel = MethodChannel('ios_edge_swipe');
/// 初始化边缘滑动检测器
/// 调用原生iOS代码注册边缘滑动监听
static Future<void> init() async {
try {
await _channel.invokeMethod('initSwipeListener');
} catch (e) {
debugPrint('初始化边缘滑动监听失败: $e');
}
}
/// 注册边缘滑动事件回调
/// [onSwipeDetected] 当检测到边缘滑动时的回调函数
static void onEdgeSwipe(VoidCallback onSwipeDetected) {
_channel.setMethodCallHandler((call) async {
if (call.method == 'onEdgeSwipe') {
onSwipeDetected();
} else {
debugPrint('方法 ${call.method} 未实现');
}
});
}
/// 清理边缘滑动监听器
/// 在组件销毁时调用,防止内存泄漏
static void dispose() {
_channel.setMethodCallHandler(null);
}
}