jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.41k stars 1.63k forks source link

why github and https://pub.flutter-io.cn/ codes are inconsistent #2386

Open wxuonline opened 2 years ago

wxuonline commented 2 years ago

Github code is inconsistent with get: ^4.6.5 but the example_nav2 using this code generates an error such as router_outlet.dart diff:

--- F:\repository\test2\getx\lib\get_navigation\src\routes\router_outlet.dart
+++ C:\Users\pc\AppData\Local\Pub\Cache\hosted\pub.flutter-io.cn\get-4.6.5\lib\get_navigation\src\nav2\router_outlet.dart

@@ -5,16 +5,22 @@
 class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object>
     extends StatefulWidget {
   final TDelegate routerDelegate;
-  final Widget Function(BuildContext context) builder;
+  final Widget Function(
+    BuildContext context,
+    TDelegate delegate,
+    T? currentRoute,
+  ) builder;

   //keys
   RouterOutlet.builder({
+    Key? key,
     TDelegate? delegate,
     required this.builder,
   })  : routerDelegate = delegate ?? Get.delegate<TDelegate, T>()!,
-        super();
+        super(key: key);

   RouterOutlet({
+    Key? key,
     TDelegate? delegate,
     required Iterable<GetPage> Function(T currentNavStack) pickPages,
     required Widget Function(
@@ -24,12 +30,10 @@
     )
         pageBuilder,
   }) : this.builder(
-          builder: (context) {
-            final currentConfig = context.delegate.currentConfiguration as T?;
-            final rDelegate = context.delegate as TDelegate;
+          builder: (context, rDelegate, currentConfig) {
             var picked =
                 currentConfig == null ? null : pickPages(currentConfig);
-            if (picked?.length == 0) {
+            if (picked?.isEmpty ?? false) {
               picked = null;
             }
             return pageBuilder(context, rDelegate, picked);
@@ -37,95 +41,47 @@
           delegate: delegate,
         );
   @override
-  _RouterOutletState<TDelegate, T> createState() =>
-      _RouterOutletState<TDelegate, T>();
+  RouterOutletState<TDelegate, T> createState() =>
+      RouterOutletState<TDelegate, T>();
 }

-class _RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object>
+class RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object>
     extends State<RouterOutlet<TDelegate, T>> {
-  RouterDelegate? delegate;
-  late ChildBackButtonDispatcher _backButtonDispatcher;
-
-  void _listener() {
-    setState(() {});
-  }
-
-  VoidCallback? disposer;
-
+  TDelegate get delegate => widget.routerDelegate;
   @override
-  void didChangeDependencies() {
-    super.didChangeDependencies();
-    disposer?.call();
-    final router = Router.of(context);
-    delegate ??= router.routerDelegate;
-    delegate?.addListener(_listener);
-    disposer = () => delegate?.removeListener(_listener);
-
-    _backButtonDispatcher =
-        router.backButtonDispatcher!.createChildBackButtonDispatcher();
+  void initState() {
+    super.initState();
+    _getCurrentRoute();
+    delegate.addListener(onRouterDelegateChanged);
   }

   @override
   void dispose() {
+    delegate.removeListener(onRouterDelegateChanged);
     super.dispose();
-    Get.routerDelegate?.removeListener(_listener);
-    //_backButtonDispatcher.forget(_backButtonDispatcher)
+  }
+
+  T? currentRoute;
+  void _getCurrentRoute() {
+    currentRoute = delegate.currentConfiguration;
+  }
+
+  void onRouterDelegateChanged() {
+    setState(_getCurrentRoute);
   }

   @override
   Widget build(BuildContext context) {
-    _backButtonDispatcher.takePriority();
-    return widget.builder(context);
+    return widget.builder(context, delegate, currentRoute);
   }
 }

-// class _RouterOutletState<TDelegate extends RouterDelegate<T>,
-//T extends Object>
-//     extends State<RouterOutlet<TDelegate, T>> {
-//   TDelegate get delegate => context.delegate as TDelegate;
-//   @override
-//   void initState() {
-//     super.initState();
-//   }
-
-//   VoidCallback? disposer;
-
-//   @override
-//   void didChangeDependencies() {
-//     disposer?.call();
-//     delegate.addListener(onRouterDelegateChanged);
-//     disposer = () => delegate.removeListener(onRouterDelegateChanged);
-//     _getCurrentRoute();
-//     super.didChangeDependencies();
-//   }
-
-//   @override
-//   void dispose() {
-//     disposer?.call();
-//     super.dispose();
-//   }
-
-//   T? currentRoute;
-//   void _getCurrentRoute() {
-//     currentRoute = delegate.currentConfiguration;
-//   }
-
-//   void onRouterDelegateChanged() {
-//     setState(_getCurrentRoute);
-//   }
-
-//   @override
-//   Widget build(BuildContext context) {
-//     return widget.builder(context);
-//   }
-// }
-
-class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> {
+class GetRouterOutlet extends RouterOutlet<GetDelegate, GetNavConfig> {
   GetRouterOutlet({
     String? anchorRoute,
     required String initialRoute,
     Iterable<GetPage> Function(Iterable<GetPage> afterAnchor)? filterPages,
-    // GlobalKey<NavigatorState>? key,
+    GlobalKey<NavigatorState>? key,
     GetDelegate? delegate,
   }) : this.pickPages(
           pickPages: (config) {
@@ -148,13 +104,13 @@
           emptyPage: (delegate) =>
               Get.routeTree.matchRoute(initialRoute).route ??
               delegate.notFoundRoute,
-          key: Get.nestedKey(anchorRoute)?.navigatorKey,
+          key: key,
           delegate: delegate,
         );
   GetRouterOutlet.pickPages({
     Widget Function(GetDelegate delegate)? emptyWidget,
     GetPage Function(GetDelegate delegate)? emptyPage,
-    required Iterable<GetPage> Function(RouteDecoder currentNavStack) pickPages,
+    required Iterable<GetPage> Function(GetNavConfig currentNavStack) pickPages,
     bool Function(Route<dynamic>, dynamic)? onPopPage,
     GlobalKey<NavigatorState>? key,
     GetDelegate? delegate,
@@ -162,11 +118,10 @@
           pageBuilder: (context, rDelegate, pages) {
             final pageRes = <GetPage?>[
               ...?pages,
-              if (pages == null || pages.length == 0)
-                emptyPage?.call(rDelegate),
+              if (pages == null || pages.isEmpty) emptyPage?.call(rDelegate),
             ].whereType<GetPage>();

-            if (pageRes.length > 0) {
+            if (pageRes.isNotEmpty) {
               return GetNavigator(
                 onPopPage: onPopPage ??
                     (route, result) {
@@ -180,15 +135,17 @@
                 key: key,
               );
             }
-            return (emptyWidget?.call(rDelegate) ?? SizedBox.shrink());
+            return (emptyWidget?.call(rDelegate) ?? const SizedBox.shrink());
           },
           pickPages: pickPages,
-          delegate: delegate ?? Get.rootController.rootDelegate,
+          delegate: delegate ?? Get.rootDelegate,
         );

   GetRouterOutlet.builder({
     required Widget Function(
       BuildContext context,
+      GetDelegate delegate,
+      GetNavConfig? currentRoute,
     )
         builder,
     GetDelegate? routerDelegate,
zbjumper commented 2 years ago

I have the same problem