SchabanBo / qlevar_router

Manage you project Routes. Create nested routes. Simply navigation without context to your pages. Change only one sub widget in your page when navigating to new route.
MIT License
87 stars 22 forks source link

getx Bindings like GetPage #71

Closed kiaxseventh closed 2 years ago

kiaxseventh commented 2 years ago

hello dear @SchabanBo Thanks a lot for this excellent flutter router navigator For switch routing from Getx to Qlevar router, I need one solution or feature I have gone through all the issues successfully The only problem is bindings in getx

SchabanBo commented 2 years ago

Hi @kiaxseventh, I am glad you liked it. Thanks. To do this, you can define a middleware like this:

import 'package:get/get.dart';
import 'package:qlevar_router/qlevar_router.dart';

class ControllerRegister<T> extends QMiddleware {
  final T Function() creator;
  final bool lazy;
  ControllerRegister(this.creator, {this.lazy = true});

  @override
  Future onEnter() async {
    if (!Get.isRegistered<T>()) {
      if (lazy) {
        Get.lazyPut(creator);
      } else {
        Get.put(creator());
      }
    }
    return super.onEnter();
  }

  @override
  Future onExit() async {
    if (Get.isRegistered<T>()) {      
        await Get.delete<T>();    
    }
    return super.onExit();
  }
}

Then use it like this:

QRoute(
    path: '/name',
    middleware: [
        ControllerRegister(MainController.new),
        ControllerRegister(CartController.new),
    ],
)
kiaxseventh commented 2 years ago

@SchabanBo Thanks a lot for your fast answer and sloved my issue If possible, it should be separated from the middleware part in the next version