Commit f2dace06 authored by Yacumima's avatar Yacumima

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

2.增加一个pushRoute的切面能力
parent 7fe33f73
...@@ -23,8 +23,7 @@ class _MyAppState extends State<MyApp> { ...@@ -23,8 +23,7 @@ class _MyAppState extends State<MyApp> {
'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params), 'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params),
///可以在native层通过 getContainerParams 来传递参数 ///可以在native层通过 getContainerParams 来传递参数
'flutterPage': (pageName, params, _){ 'flutterPage': (pageName, params, _) {
print("flutterPage params:$params"); print("flutterPage params:$params");
return FlutterRouteWidget(); return FlutterRouteWidget();
...@@ -38,7 +37,35 @@ class _MyAppState extends State<MyApp> { ...@@ -38,7 +37,35 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Boost example', title: 'Flutter Boost example',
builder: FlutterBoost.init(), builder: FlutterBoost.init(postPush: _onRoutePushed),
home: Container()); 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> { ...@@ -240,15 +240,15 @@ class _PushWidgetState extends State<PushWidget> {
// TODO: implement didChangeDependencies // TODO: implement didChangeDependencies
super.didChangeDependencies(); super.didChangeDependencies();
if (_backPressedListenerUnsub == null) { // if (_backPressedListenerUnsub == null) {
_backPressedListenerUnsub = // _backPressedListenerUnsub =
BoostContainer.of(context).addBackPressedListener(() { // BoostContainer.of(context).addBackPressedListener(() {
if (BoostContainer.of(context).onstage && // if (BoostContainer.of(context).onstage &&
ModalRoute.of(context).isCurrent) { // ModalRoute.of(context).isCurrent) {
Navigator.pop(context); // Navigator.pop(context);
} // }
}); // });
} // }
} }
@override @override
...@@ -260,8 +260,6 @@ class _PushWidgetState extends State<PushWidget> { ...@@ -260,8 +260,6 @@ class _PushWidgetState extends State<PushWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FlutterRouteWidget( return FlutterRouteWidget(message:"Pushed Widget");
message: "pushed Widget",
);
} }
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
import 'package:flutter/material.dart'; 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/container/container_manager.dart';
import 'package:flutter_boost/flutter_boost.dart'; import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_boost/router/boost_page_route.dart'; import 'package:flutter_boost/router/boost_page_route.dart';
...@@ -124,6 +125,8 @@ class BoostContainerState extends NavigatorState { ...@@ -124,6 +125,8 @@ class BoostContainerState extends NavigatorState {
String get name => widget.settings.name; String get name => widget.settings.name;
Map get params => widget.settings.params;
BoostContainerSettings get settings => widget.settings; BoostContainerSettings get settings => widget.settings;
bool get onstage => bool get onstage =>
...@@ -164,8 +167,7 @@ class BoostContainerState extends NavigatorState { ...@@ -164,8 +167,7 @@ class BoostContainerState extends NavigatorState {
Logger.log('performBackPressed'); Logger.log('performBackPressed');
if (_backPressedListeners.isEmpty) { if (_backPressedListeners.isEmpty) {
FlutterBoost.singleton pop();
.closePage(name, uniqueId, settings.params, animated: false);
} else { } else {
for (VoidCallback cb in _backPressedListeners) { for (VoidCallback cb in _backPressedListeners) {
cb(); cb();
...@@ -190,15 +192,30 @@ class BoostContainerState extends NavigatorState { ...@@ -190,15 +192,30 @@ class BoostContainerState extends NavigatorState {
if (canPop()) { if (canPop()) {
return super.pop(result); return super.pop(result);
} else { } else {
if (BoostContainerManager.of(context).canPop()) { FlutterBoost.singleton.closePage(name, uniqueId, params);
BoostContainerManager.of(context).pop();
return true;
}
} }
return false; 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) { VoidCallback addBackPressedListener(VoidCallback listener) {
_backPressedListeners.add(listener); _backPressedListeners.add(listener);
......
...@@ -37,7 +37,11 @@ typedef BoostContainerObserver = void Function( ...@@ -37,7 +37,11 @@ typedef BoostContainerObserver = void Function(
@immutable @immutable
class BoostContainerManager extends StatefulWidget { class BoostContainerManager extends StatefulWidget {
final Navigator initNavigator; 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 @override
ContainerManagerState createState() => ContainerManagerState(); ContainerManagerState createState() => ContainerManagerState();
...@@ -69,6 +73,10 @@ class ContainerManagerState extends State<BoostContainerManager> { ...@@ -69,6 +73,10 @@ class ContainerManagerState extends State<BoostContainerManager> {
String _lastShownContainer; String _lastShownContainer;
PrePushRoute get prePushRoute => widget.prePushRoute;
PostPushRoute get postPushRoute => widget.postPushRoute;
bool get foreground => _foreground; bool get foreground => _foreground;
ManagerNavigatorObserver get navigatorObserver => _navigatorObserver; ManagerNavigatorObserver get navigatorObserver => _navigatorObserver;
......
...@@ -40,6 +40,12 @@ export 'container/container_manager.dart'; ...@@ -40,6 +40,12 @@ export 'container/container_manager.dart';
typedef Widget PageBuilder(String pageName, Map params, String uniqueId); 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 { class FlutterBoost {
static final FlutterBoost _instance = FlutterBoost(); static final FlutterBoost _instance = FlutterBoost();
final GlobalKey<ContainerManagerState> containerManagerKey = final GlobalKey<ContainerManagerState> containerManagerKey =
...@@ -58,14 +64,20 @@ class FlutterBoost { ...@@ -58,14 +64,20 @@ class FlutterBoost {
static ContainerManagerState get containerManager => static ContainerManagerState get containerManager =>
_instance.containerManagerKey.currentState; _instance.containerManagerKey.currentState;
static TransitionBuilder init([TransitionBuilder builder]) { static TransitionBuilder init(
{TransitionBuilder builder,
PrePushRoute prePush,
PostPushRoute postPush}) {
return (BuildContext context, Widget child) { return (BuildContext context, Widget child) {
assert(child is Navigator, 'child must be Navigator, what is wrong?'); assert(child is Navigator, 'child must be Navigator, what is wrong?');
//Logger.log('Running flutter boost opt!'); //Logger.log('Running flutter boost opt!');
final BoostContainerManager manager = BoostContainerManager( final BoostContainerManager manager = BoostContainerManager(
key: _instance.containerManagerKey, initNavigator: child); key: _instance.containerManagerKey,
initNavigator: child,
prePushRoute: prePush,
postPushRoute: postPush);
if (builder != null) { if (builder != null) {
return builder(context, manager); 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