flutter中手机系统自带返回事件触发判断

发布时间 2023-10-11 11:58:29作者: 慕雪琳鸢

WillPopScope组件

用WillPopScope包裹根页面组件

WillPopScope(
  onWillPop: _requestPop,
  child: pageContainer()
)

其中onWillPop绑定的函数就是系统返回事件触发后的处理函数

Future<bool> _requestPop() async {
  // 调用原生返回,返回原生页面,根据自身场景自己定义处理函数
  const platform = MethodChannel("flutterBackNative");
  try {
    final int res = await platform.invokeMethod("flutterBackNative");
  } on PlatformException catch (e) {
    print(e);
  }
  return Future.value(false);
}