Commit f2dace06 authored by Yacumima's avatar Yacumima

1.修正返回键逻辑,存在Route的情况下默认不关闭Activity

2.增加一个pushRoute的切面能力
parent 7fe33f73
......@@ -23,8 +23,7 @@ class _MyAppState extends State<MyApp> {
'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params),
///可以在native层通过 getContainerParams 来传递参数
'flutterPage': (pageName, params, _){
'flutterPage': (pageName, params, _) {
print("flutterPage params:$params");
return FlutterRouteWidget();
......@@ -38,7 +37,35 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Boost example',
builder: FlutterBoost.init(),
builder: FlutterBoost.init(postPush: _onRoutePushed),
home: Container());
}
void _onRoutePushed(
String pageName, String uniqueId, Map params, Route route, Future _) {
// List<OverlayEntry> newEntries = route.overlayEntries
// .map((OverlayEntry entry) => OverlayEntry(
// builder: (BuildContext context) {
// final pageWidget = entry.builder(context);
// return Stack(
// children: <Widget>[
// pageWidget,
// Positioned(
// child: Text(
// "pageName:$pageName\npageWidget:${pageWidget.toStringShort()}",
// style: TextStyle(fontSize: 12.0, color: Colors.red),
// ),
// left: 8.0,
// top: 8.0,
// )
// ],
// );
// },
// opaque: entry.opaque,
// maintainState: entry.maintainState))
// .toList(growable: true);
//
// route.overlayEntries.clear();
// route.overlayEntries.addAll(newEntries);
}
}
......@@ -240,15 +240,15 @@ class _PushWidgetState extends State<PushWidget> {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
if (_backPressedListenerUnsub == null) {
_backPressedListenerUnsub =
BoostContainer.of(context).addBackPressedListener(() {
if (BoostContainer.of(context).onstage &&
ModalRoute.of(context).isCurrent) {
Navigator.pop(context);
}
});
}
// if (_backPressedListenerUnsub == null) {
// _backPressedListenerUnsub =
// BoostContainer.of(context).addBackPressedListener(() {
// if (BoostContainer.of(context).onstage &&
// ModalRoute.of(context).isCurrent) {
// Navigator.pop(context);
// }
// });
// }
}
@override
......@@ -260,8 +260,6 @@ class _PushWidgetState extends State<PushWidget> {
@override
Widget build(BuildContext context) {
return FlutterRouteWidget(
message: "pushed Widget",
);
return FlutterRouteWidget(message:"Pushed Widget");
}
}
......@@ -22,6 +22,7 @@
* THE SOFTWARE.
*/
import 'package:flutter/material.dart';
import 'package:flutter_boost/container/container_coordinator.dart';
import 'package:flutter_boost/container/container_manager.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_boost/router/boost_page_route.dart';
......@@ -124,6 +125,8 @@ class BoostContainerState extends NavigatorState {
String get name => widget.settings.name;
Map get params => widget.settings.params;
BoostContainerSettings get settings => widget.settings;
bool get onstage =>
......@@ -164,8 +167,7 @@ class BoostContainerState extends NavigatorState {
Logger.log('performBackPressed');
if (_backPressedListeners.isEmpty) {
FlutterBoost.singleton
.closePage(name, uniqueId, settings.params, animated: false);
pop();
} else {
for (VoidCallback cb in _backPressedListeners) {
cb();
......@@ -190,15 +192,30 @@ class BoostContainerState extends NavigatorState {
if (canPop()) {
return super.pop(result);
} else {
if (BoostContainerManager.of(context).canPop()) {
BoostContainerManager.of(context).pop();
return true;
}
FlutterBoost.singleton.closePage(name, uniqueId, params);
}
return false;
}
@override
Future<T> push<T extends Object>(Route<T> route) {
Route<T> newRoute;
if (FlutterBoost.containerManager.prePushRoute != null) {
newRoute = FlutterBoost.containerManager
.prePushRoute(name, uniqueId, params, route);
}
Future<T> future = super.push<T>(newRoute ?? route);
if (FlutterBoost.containerManager.postPushRoute != null) {
FlutterBoost.containerManager
.postPushRoute(name, uniqueId, params, newRoute ?? route, future);
}
return future;
}
VoidCallback addBackPressedListener(VoidCallback listener) {
_backPressedListeners.add(listener);
......
......@@ -37,7 +37,11 @@ typedef BoostContainerObserver = void Function(
@immutable
class BoostContainerManager extends StatefulWidget {
final Navigator initNavigator;
const BoostContainerManager({Key key, this.initNavigator}) : super(key: key);
final PrePushRoute prePushRoute;
final PostPushRoute postPushRoute;
const BoostContainerManager(
{Key key, this.initNavigator, this.prePushRoute, this.postPushRoute})
: super(key: key);
@override
ContainerManagerState createState() => ContainerManagerState();
......@@ -69,6 +73,10 @@ class ContainerManagerState extends State<BoostContainerManager> {
String _lastShownContainer;
PrePushRoute get prePushRoute => widget.prePushRoute;
PostPushRoute get postPushRoute => widget.postPushRoute;
bool get foreground => _foreground;
ManagerNavigatorObserver get navigatorObserver => _navigatorObserver;
......
......@@ -40,6 +40,12 @@ export 'container/container_manager.dart';
typedef Widget PageBuilder(String pageName, Map params, String uniqueId);
typedef Route PrePushRoute(
String pageName, String uniqueId, Map params, Route route);
typedef void PostPushRoute(
String pageName, String uniqueId, Map params, Route route, Future result);
class FlutterBoost {
static final FlutterBoost _instance = FlutterBoost();
final GlobalKey<ContainerManagerState> containerManagerKey =
......@@ -58,14 +64,20 @@ class FlutterBoost {
static ContainerManagerState get containerManager =>
_instance.containerManagerKey.currentState;
static TransitionBuilder init([TransitionBuilder builder]) {
static TransitionBuilder init(
{TransitionBuilder builder,
PrePushRoute prePush,
PostPushRoute postPush}) {
return (BuildContext context, Widget child) {
assert(child is Navigator, 'child must be Navigator, what is wrong?');
//Logger.log('Running flutter boost opt!');
final BoostContainerManager manager = BoostContainerManager(
key: _instance.containerManagerKey, initNavigator: child);
key: _instance.containerManagerKey,
initNavigator: child,
prePushRoute: prePush,
postPushRoute: postPush);
if (builder != null) {
return builder(context, manager);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment