From f9a73065087a3a7e2e0fd90b45fc0895a9d03f0b Mon Sep 17 00:00:00 2001 From: Joe Chan Date: Tue, 6 Aug 2024 15:19:02 +0800 Subject: [PATCH 1/2] fix #2106 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原先在initState中进行的初始化,改放到确保Overlay被mounted之后的时序中 --- lib/src/flutter_boost_app.dart | 91 ++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/lib/src/flutter_boost_app.dart b/lib/src/flutter_boost_app.dart index 48d76f44..be11a0cc 100644 --- a/lib/src/flutter_boost_app.dart +++ b/lib/src/flutter_boost_app.dart @@ -84,6 +84,8 @@ class FlutterBoostAppState extends State { late VoidCallback _lifecycleStateListenerRemover; + late BoostContainer _initialContainer; + @override void initState() { assert( @@ -94,11 +96,13 @@ class FlutterBoostAppState extends State { _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((_) { @@ -108,7 +112,7 @@ class FlutterBoostAppState extends State { return true; }()); - refreshOnPush(initialContainer); + refreshOnPush(initial); _boostFlutterRouterApi.isEnvReady = true; _addAppLifecycleStateEventListener(); BoostOperationQueue.instance.runPendingOperations(); @@ -148,23 +152,26 @@ class FlutterBoostAppState extends State { @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 [], - )))); + 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 [], + ))), + )); } void _handlePointerDown(PointerDownEvent event) { @@ -453,10 +460,8 @@ class FlutterBoostAppState extends State { } if (targetContainer.topPage != targetPage) { - Future.delayed( - const Duration(milliseconds: 50), - () => targetContainer?.navigator - ?.popUntil(_withPage(targetPage!))); + Future.delayed(const Duration(milliseconds: 50), + () => targetContainer?.navigator?.popUntil(_withPage(targetPage!))); } } else { topContainer?.navigator?.popUntil(_withPage(targetPage!)); @@ -465,8 +470,7 @@ class FlutterBoostAppState extends State { RoutePredicate _withPage(BoostPage targetPage) { return (Route route) { - return !route.willHandlePopInternally - && route == targetPage.route; + return !route.willHandlePopInternally && route == targetPage.route; }; } @@ -734,6 +738,40 @@ class FlutterBoostAppState extends State { } } +class FlutterBoostAppMountedWidget extends StatefulWidget { + final Widget child; + final BoostContainer initial; + + const FlutterBoostAppMountedWidget({ + super.key, + required this.child, + required this.initial, + }); + + @override + State createState() => + _FlutterBoostAppMountedWidgetState(); +} + +class _FlutterBoostAppMountedWidgetState + extends State { + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + FlutterBoostAppState? state = + context.findAncestorStateOfType(); + if (state != null) { + state.onMounted(widget.initial); + } + } + + @override + Widget build(BuildContext context) { + return widget.child; + } +} + // ignore: must_be_immutable class BoostPage extends Page { BoostPage._({LocalKey? key, required this.pageInfo}) @@ -744,6 +782,7 @@ class BoostPage extends Page { assert(_route != null, "Oops! Route name is not registered: '${pageInfo.pageName}'."); } + final PageInfo pageInfo; factory BoostPage.create(PageInfo pageInfo) { From 902155d29df1d2e86e33f0e2fb0b4003e8ea5437 Mon Sep 17 00:00:00 2001 From: Joe Chan Date: Tue, 6 Aug 2024 15:23:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=86=E6=94=BE=E5=9C=A8didChangeDepende?= =?UTF-8?q?ncies=E7=9A=84=E9=80=BB=E8=BE=91=E6=94=B9=E5=88=B0initState?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/flutter_boost_app.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/flutter_boost_app.dart b/lib/src/flutter_boost_app.dart index be11a0cc..625e1bc7 100644 --- a/lib/src/flutter_boost_app.dart +++ b/lib/src/flutter_boost_app.dart @@ -757,10 +757,10 @@ class _FlutterBoostAppMountedWidgetState extends State { @override - void didChangeDependencies() { - super.didChangeDependencies(); + void initState() { + super.initState(); FlutterBoostAppState? state = - context.findAncestorStateOfType(); + context.findAncestorStateOfType(); if (state != null) { state.onMounted(widget.initial); }