Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2106 #2108

Open
wants to merge 2 commits into
base: dev/exp
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 65 additions & 26 deletions lib/src/flutter_boost_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {

late VoidCallback _lifecycleStateListenerRemover;

late BoostContainer _initialContainer;

@override
void initState() {
assert(
Expand All @@ -94,11 +96,13 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {
_boostFlutterRouterApi = BoostFlutterRouterApi(this);

/// create the container matching the initial route
final BoostContainer initialContainer =
_initialContainer =
_createContainer(PageInfo(pageName: widget.initialRoute));
_containers.add(initialContainer);
_containers.add(_initialContainer);
super.initState();
}

void onMounted(BoostContainer initial) {
// Make sure that the widget in the tree that matches [overlayKey]
// is already mounted, or [refreshOnPush] will fail.
WidgetsBinding.instance.addPostFrameCallback((_) {
Expand All @@ -108,7 +112,7 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {
return true;
}());

refreshOnPush(initialContainer);
refreshOnPush(initial);
_boostFlutterRouterApi.isEnvReady = true;
_addAppLifecycleStateEventListener();
BoostOperationQueue.instance.runPendingOperations();
Expand Down Expand Up @@ -148,23 +152,26 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {

@override
Widget build(BuildContext context) {
return widget.appBuilder(WillPopScope(
onWillPop: () async {
final canPop = topContainer!.navigator!.canPop();
if (canPop) {
topContainer!.navigator!.pop();
return true;
}
return false;
},
child: Listener(
onPointerDown: _handlePointerDown,
onPointerUp: _handlePointerUpOrCancel,
onPointerCancel: _handlePointerUpOrCancel,
child: Overlay(
key: overlayKey,
initialEntries: const <OverlayEntry>[],
))));
return widget.appBuilder(FlutterBoostAppMountedWidget(
initial: _initialContainer,
child: WillPopScope(
onWillPop: () async {
final canPop = topContainer!.navigator!.canPop();
if (canPop) {
topContainer!.navigator!.pop();
return true;
}
return false;
},
child: Listener(
onPointerDown: _handlePointerDown,
onPointerUp: _handlePointerUpOrCancel,
onPointerCancel: _handlePointerUpOrCancel,
child: Overlay(
key: overlayKey,
initialEntries: const <OverlayEntry>[],
))),
));
}

void _handlePointerDown(PointerDownEvent event) {
Expand Down Expand Up @@ -453,10 +460,8 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {
}

if (targetContainer.topPage != targetPage) {
Future<void>.delayed(
const Duration(milliseconds: 50),
() => targetContainer?.navigator
?.popUntil(_withPage(targetPage!)));
Future<void>.delayed(const Duration(milliseconds: 50),
() => targetContainer?.navigator?.popUntil(_withPage(targetPage!)));
}
} else {
topContainer?.navigator?.popUntil(_withPage(targetPage!));
Expand All @@ -465,8 +470,7 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {

RoutePredicate _withPage(BoostPage targetPage) {
return (Route<dynamic> route) {
return !route.willHandlePopInternally
&& route == targetPage.route;
return !route.willHandlePopInternally && route == targetPage.route;
};
}

Expand Down Expand Up @@ -734,6 +738,40 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {
}
}

class FlutterBoostAppMountedWidget extends StatefulWidget {
final Widget child;
final BoostContainer initial;

const FlutterBoostAppMountedWidget({
super.key,
required this.child,
required this.initial,
});

@override
State<FlutterBoostAppMountedWidget> createState() =>
_FlutterBoostAppMountedWidgetState();
}

class _FlutterBoostAppMountedWidgetState
extends State<FlutterBoostAppMountedWidget> {

@override
void initState() {
super.initState();
FlutterBoostAppState? state =
context.findAncestorStateOfType<FlutterBoostAppState>();
if (state != null) {
state.onMounted(widget.initial);
}
}

@override
Widget build(BuildContext context) {
return widget.child;
}
}

// ignore: must_be_immutable
class BoostPage<T> extends Page<T> {
BoostPage._({LocalKey? key, required this.pageInfo})
Expand All @@ -744,6 +782,7 @@ class BoostPage<T> extends Page<T> {
assert(_route != null,
"Oops! Route name is not registered: '${pageInfo.pageName}'.");
}

final PageInfo pageInfo;

factory BoostPage.create(PageInfo pageInfo) {
Expand Down