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

支持外部传入 NavigatorKeyBuilder #14

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
36 changes: 35 additions & 1 deletion example/lib/main_get.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -37,12 +38,31 @@
// )
// };

// List<GetPage> 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);
Expand All @@ -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<NavigatorState>,
// );

// /// 添加最顶层 Navigator 作为 GetX 默认的 Navigator
// Get.addKey(key as GlobalKey<NavigatorState>);
// },
// faradayNavigatorParentBuilder: (child) =>
// ScreenUtilInit(designSize: Size(750, 1334), builder: () => child),
// );

// final cupertinoApp = GetCupertinoApp(
Expand Down
10 changes: 7 additions & 3 deletions example/lib/src/pages/features/basic/basic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ List<Widget> _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;
Expand Down
3 changes: 2 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/Faraday.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/src/faraday.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Faraday {
TransitionBuilderProvider? switchPageAnimation,
List<NavigatorObserver>? observers,
WidgetBuilder? errorPage,
NavigatorKeyCallback? navigatorKeyCallback,
FaradayNavigatorParentBuilder? faradayNavigatorParentBuilder,
}) {
return FaradayPageRouteBuilder(
pageBuilder: (context) {
Expand All @@ -67,6 +69,8 @@ class Faraday {
observers: observers,
errorPage: errorPage,
key: _key,
navigatorKeyCallback: navigatorKeyCallback,
faradayNavigatorParentBuilder: faradayNavigatorParentBuilder,
);
return page;
},
Expand Down
24 changes: 22 additions & 2 deletions lib/src/route/native_bridge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,13 +66,19 @@ class FaradayNativeBridge extends StatefulWidget {
// 路由未找到时展示错误页面
final WidgetBuilder? errorPage;

final NavigatorKeyCallback? navigatorKeyCallback;

final FaradayNavigatorParentBuilder? faradayNavigatorParentBuilder;

FaradayNativeBridge(
this.onGenerateRoute, {
Key? key,
this.backgroundColorProvider,
this.transitionBuilderProvider,
this.observers,
this.errorPage,
this.navigatorKeyCallback,
this.faradayNavigatorParentBuilder,
}) : super(key: key);

static FaradayNativeBridgeState? of(BuildContext context) {
Expand Down Expand Up @@ -223,9 +235,12 @@ class FaradayNativeBridgeState extends State<FaradayNativeBridge> {
),
);

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) {
Expand Down Expand Up @@ -290,6 +305,7 @@ class FaradayNativeBridgeState extends State<FaradayNativeBridge> {
// 此时`native`容器已经`dealloc`了,不会再触发渲染会导致`flutter`侧widget延迟释放
// 因此在这里手动触发一次渲染
WidgetsBinding.instance?.drawFrame();
WidgetsBinding.instance?.scheduleWarmUpFrame();
log('''
TRIGGER `drawFrame` by hand. if you find any bugs please contact me.
Email: [email protected]
Expand Down Expand Up @@ -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,
Expand Down