本文最后更新于 546 天前
1. FlutterBoost源码
- FlutterBoostApp->pop方法
Future<bool> pop(
{String? uniqueId, Object? result, bool onBackPressed = false}) async {
BoostContainer? container;
if (uniqueId != null) {
container = _findContainerByUniqueId(uniqueId);
if (container == null) {
Logger.error('uniqueId=uniqueId not found');
return false;
}
if (container != topContainer) {
await _removeContainer(container);
return true;
}
} else {
container = topContainer;
}
// ---------------------追加部分-----------------------------------
// 返回键再处理
if(onBackPressed) {
List<PopCallback> callbackList = Binding().observers();
for(PopCallback callback in callbackList) {
var params = await callback.call();
if(result == null) {
result = params;
} else if(result is Map) {
result = {...result,...params};
}
break;
// print(">>>>>>>>>> callBackparams");
// if(params is Map && params.containsKey("key") && params["key"] == container?.pageInfo?.pageName) {
// }
}
}
// --------------------------------------------------------
final currentPage = topContainer?.topPage.pageInfo.uniqueId!;
assert(currentPage != null);
// .........
return true;
}
2. 额外新增类
typedef PopCallback = Future<Map<String,dynamic>> Function();
class Binding {
final List<PopCallback> _observers = <PopCallback>[];
void addObserver(PopCallback observer) => _observers.add(observer);
bool removeObserver(PopCallback observer) => _observers.remove(observer);
static final Binding _instance = Binding._internal();
factory Binding() {
return _instance;
}
Binding._internal();
List<PopCallback> observers() => _observers;
}
import 'package:flutter/material.dart';
import 'binding.dart';
class BoostWillPopeScope extends StatefulWidget {
final Widget child;
final PopCallback? onWillPop;
BoostWillPopeScope({required this.child,this.onWillPop,Key? key}) : super(key: key);
@override
State<BoostWillPopeScope> createState() => _BoostWillPopeScopeState();
}
class _BoostWillPopeScopeState extends State<BoostWillPopeScope>{
@override
void initState() {
super.initState();
if(widget.onWillPop != null) {
Binding().addObserver(widget.onWillPop!);
}
}
@override
void dispose() {
super.dispose();
if(widget.onWillPop != null) {
Binding().removeObserver(widget.onWillPop!);
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (widget.onWillPop != null)
Binding().removeObserver(widget.onWillPop!);
if (widget.onWillPop != null)
Binding().addObserver(widget.onWillPop!);
}
@override
void didUpdateWidget(BoostWillPopeScope oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.onWillPop != oldWidget.onWillPop) {
if (oldWidget.onWillPop != null)
Binding().removeObserver(oldWidget.onWillPop!);
if (widget.onWillPop != null)
Binding().addObserver(widget.onWillPop!);
}
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}
3. export将BoostWillPopeScope暴露
flutter_boost.dart追加导包
4. 使用
同flutter sdk内的WillPopeScope
return BoostWillPopeScope(
// 添加方法回调并返回需要的参数即可
onWillPop: () async => {
"key":RouterKeys.feedback_history
},
child: Scaffold(
backgroundColor: CommonColors.backgroundColor,
appBar: GpAppBar(
elevation: 0,
leadingColors: CommonColors.appbarIconBlackColor,
title: Text("历史记录"),
),
body: Text("")
),
);