diff --git a/example/lib/main_get.dart b/example/lib/main_get.dart index e9389b4..4f54c82 100644 --- a/example/lib/main_get.dart +++ b/example/lib/main_get.dart @@ -1,7 +1,8 @@ // import 'package:flutter/cupertino.dart'; // import 'package:flutter/material.dart'; +// import 'package:flutter_screenutil/flutter_screenutil.dart'; // import 'package:g_faraday/g_faraday.dart'; -// import 'package:get/route_manager.dart'; +// import 'package:get/get.dart'; // import 'src/pages/features/basic/pages/flutter_to_flutter.dart'; // import 'src/pages/features/basic/pages/native_to_flutter.dart'; @@ -37,12 +38,31 @@ // ) // }; +// List getPages = [ +// // GetPage(name: '/', page: () => HomePage({})), +// GetPage(name: '/Home', page: () => HomePage({})), +// GetPage(name: '/Flutter2Flutter', page: () => Flutter2Flutter()), +// ]; + +// @override +// void initState() { +// super.initState(); + +// Get.addPages(getPages); +// } + // @override // Widget build(BuildContext context) { // final color = Color.fromARGB(255, 6, 210, 116); // final route = faraday.wrapper( // (settings) { +// if (getPages.indexWhere((element) => element.name == settings.name) > +// -1) { +// print("TAG GetX Route settings ${settings.toString()}"); +// return PageRedirect(settings, null).page(); +// } + // final f = routes[settings.name!]; // if (f == null) return null; // return f(settings); @@ -63,6 +83,20 @@ // // flutter 自定义过渡页背景 // nativeContainerBackgroundColorProvider: (context, {route}) => // CupertinoColors.secondarySystemBackground, +// navigatorKeyCallback: (key, id) { +// print("TAG navigatorKeyCallback key $key id $id"); + +// /// 添加进去 GetX 的 GlobalKey 组合,方便根据 id 切换 Navigator +// Get.keys.putIfAbsent( +// id, +// () => key as GlobalKey, +// ); + +// /// 添加最顶层 Navigator 作为 GetX 默认的 Navigator +// Get.addKey(key as GlobalKey); +// }, +// faradayNavigatorParentBuilder: (child) => +// ScreenUtilInit(designSize: Size(750, 1334), builder: () => child), // ); // final cupertinoApp = GetCupertinoApp( diff --git a/example/lib/src/pages/features/basic/basic.dart b/example/lib/src/pages/features/basic/basic.dart index fce002e..7ca3cec 100644 --- a/example/lib/src/pages/features/basic/basic.dart +++ b/example/lib/src/pages/features/basic/basic.dart @@ -87,9 +87,13 @@ List _buildActions(BuildContext context, {bool onlyBase = true}) { subTitle: S.of(context).basicFlutter2FlutterDescription, begin: _Action.flutter, end: _Action.flutter, - onTap: () => Navigator.of(context).push( - CupertinoPageRoute(builder: (_) => Flutter2Flutter()), - ), + onTap: () { + // Get.toNamed("/Flutter2Flutter"); + + Navigator.of(context).push( + CupertinoPageRoute(builder: (_) => Flutter2Flutter()), + ); + }, ), ]; if (onlyBase) return base; diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 3b99918..0fcb531 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -21,7 +21,8 @@ dependencies: # the parent directory to use the current plugin's version. path: ../ # fluro: ^1.7.8 - # get: ^3.24.0 + # get: ^4.1.4 + # flutter_screenutil: ^5.0.0+2 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/ios/Classes/Faraday.swift b/ios/Classes/Faraday.swift index 4ad3eb8..5cb6135 100644 --- a/ios/Classes/Faraday.swift +++ b/ios/Classes/Faraday.swift @@ -194,7 +194,7 @@ public class Faraday { viewController.viewDidAppear(false) } - static func callback(_ token: CallbackToken?, result: Any?) { + public static func callback(_ token: CallbackToken?, result: Any?) { if let t = token { if let cb = Faraday.default.callbackCache.removeValue(forKey: t) { cb(result) diff --git a/lib/src/faraday.dart b/lib/src/faraday.dart index c0c2179..e57a079 100644 --- a/lib/src/faraday.dart +++ b/lib/src/faraday.dart @@ -56,6 +56,8 @@ class Faraday { TransitionBuilderProvider? switchPageAnimation, List? observers, WidgetBuilder? errorPage, + NavigatorKeyCallback? navigatorKeyCallback, + FaradayNavigatorParentBuilder? faradayNavigatorParentBuilder, }) { return FaradayPageRouteBuilder( pageBuilder: (context) { @@ -67,6 +69,8 @@ class Faraday { observers: observers, errorPage: errorPage, key: _key, + navigatorKeyCallback: navigatorKeyCallback, + faradayNavigatorParentBuilder: faradayNavigatorParentBuilder, ); return page; }, diff --git a/lib/src/route/native_bridge.dart b/lib/src/route/native_bridge.dart index 13f41df..a946a7b 100644 --- a/lib/src/route/native_bridge.dart +++ b/lib/src/route/native_bridge.dart @@ -22,6 +22,12 @@ typedef TransitionBuilderProvider = TransitionBuilder? Function( typedef ColorProvider = Color Function(BuildContext context, {JSON? route}); +// FaradayNavigatorKey 回调 +typedef NavigatorKeyCallback = Function(GlobalKey key, int id); + +// FaradayNavigator 外部 Weiget Builder +typedef FaradayNavigatorParentBuilder = Widget Function(Widget child); + Color _defaultBackgroundColor(BuildContext context, {JSON? route}) { return MediaQuery.of(context).platformBrightness == Brightness.light ? CupertinoColors.white @@ -60,6 +66,10 @@ class FaradayNativeBridge extends StatefulWidget { // 路由未找到时展示错误页面 final WidgetBuilder? errorPage; + final NavigatorKeyCallback? navigatorKeyCallback; + + final FaradayNavigatorParentBuilder? faradayNavigatorParentBuilder; + FaradayNativeBridge( this.onGenerateRoute, { Key? key, @@ -67,6 +77,8 @@ class FaradayNativeBridge extends StatefulWidget { this.transitionBuilderProvider, this.observers, this.errorPage, + this.navigatorKeyCallback, + this.faradayNavigatorParentBuilder, }) : super(key: key); static FaradayNativeBridgeState? of(BuildContext context) { @@ -223,9 +235,12 @@ class FaradayNativeBridgeState extends State { ), ); + final contentParent = + widget.faradayNavigatorParentBuilder?.call(content) ?? content; + final builder = widget.transitionBuilderProvider?.call(current.info); - if (builder == null) return content; - return builder(context, content); + if (builder == null) return contentParent; + return builder(context, contentParent); } Widget _defaultErrorPage(BuildContext context) { @@ -290,6 +305,7 @@ class FaradayNativeBridgeState extends State { // 此时`native`容器已经`dealloc`了,不会再触发渲染会导致`flutter`侧widget延迟释放 // 因此在这里手动触发一次渲染 WidgetsBinding.instance?.drawFrame(); + WidgetsBinding.instance?.scheduleWarmUpFrame(); log(''' TRIGGER `drawFrame` by hand. if you find any bugs please contact me. Email: aoxianglele@icloud.com @@ -326,6 +342,10 @@ Github Issue: https://github.com/gfaraday/g_faraday/issues } Widget _buildPage(BuildContext context, FaradayArguments arg) { + if (widget.navigatorKeyCallback != null) { + widget.navigatorKeyCallback!(arg.key, arg.id); + } + return FaradayNavigator( key: arg.key, arg: arg,