|
|
|
import 'dart:developer';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart' as material;
|
|
|
|
import 'package:flutter/src/widgets/navigator.dart';
|
|
|
|
import 'package:flutter/src/widgets/routes.dart';
|
|
|
|
import 'package:flutter/src/widgets/framework.dart';
|
|
|
|
|
|
|
|
class MyWillPopScope extends StatefulWidget {
|
|
|
|
const MyWillPopScope({
|
|
|
|
super.key,
|
|
|
|
required this.child,
|
|
|
|
required this.onWillPop,
|
|
|
|
});
|
|
|
|
|
|
|
|
final Widget child;
|
|
|
|
final WillPopCallback onWillPop;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyWillPopScope> createState() => _WillPopScopeState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _WillPopScopeState extends State<MyWillPopScope> {
|
|
|
|
double startDx = 0;
|
|
|
|
double endDx = 0;
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
return material.Listener(
|
|
|
|
onPointerUp: (details) async {
|
|
|
|
bool safe1 = startDx < 30;
|
|
|
|
bool safe2 = endDx < 30;
|
|
|
|
log(
|
|
|
|
"\n按下手指位置(${safe1 ? '安全区' : '非安全区'}) => $startDx"
|
|
|
|
"\n手指抬起位置(${safe2 ? '安全区' : '非安全区'}) => $endDx",
|
|
|
|
name: "WillPopScope",
|
|
|
|
);
|
|
|
|
// 左侧滑动、安全区
|
|
|
|
if ((endDx > startDx) && safe1 && !safe2) {
|
|
|
|
log("ios左侧滑动返回被拦截$endDx", name: "WillPopScope");
|
|
|
|
widget.onWillPop();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onPointerDown: (details) => startDx = details.localPosition.dx,
|
|
|
|
onPointerMove: (details) => endDx = details.localPosition.dx,
|
|
|
|
child: material.WillPopScope(
|
|
|
|
child: widget.child,
|
|
|
|
onWillPop: () async => false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
widget.onWillPop().then((value) {
|
|
|
|
log(value ? "android触发滑动返回" : "android滑动返回被拦截", name: "WillPopScope");
|
|
|
|
});
|
|
|
|
return material.WillPopScope(
|
|
|
|
onWillPop: widget.onWillPop,
|
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |