my_will_pop_scope.dart
1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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,
);
}
}
}