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

Add DI (dependency injection) as well as QController for handling business logic of a page #91

Closed tejHackerDEV closed 1 year ago

tejHackerDEV commented 2 years ago

Without changing the complexities of the packages, here by I propose some enhancements to the package which will helps the developers in their development process with minimal efforts & not depending on other packages for simple things

Below is an example code which shows how to use DI & QController in generic way

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:qlevar_router/qlevar_router.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final routes = [
    QRoute(
      name: 'sign-in',
      path: '/sign-in',
      middleware: [
        QDependencyBuilder(SignInDependencies()),
      ],
      builder: () => const SignInPage(),
    ),
    QRoute(
      name: 'sign-up',
      path: '/sign-up',
      middleware: [
        QDependencyBuilder(SignUpDependencies()),
      ],
      builder: () => const SignUpPage(),
    ),
  ];

  MyApp({super.key});

  @override
  Widget build(BuildContext context) => MaterialApp.router(
        routeInformationParser: const QRouteInformationParser(),
        routerDelegate: QRouterDelegate(routes, initPath: '/sign-in'),
        theme: ThemeData.dark(),
      );
}

class SignInDependencies extends QDependencyBinder {
  @override
  FutureOr<void> onInit() {
    super.onInit();
    QR.dependencies.inject(SignInController());
  }
}

class SignInPage extends StatelessWidget {
  const SignInPage({Key? key}) : super(key: key);

  SignInController get controller => QR.dependencies.get();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sign In'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          TextField(
            controller: controller.textEditingController,
          ),
          ElevatedButton(
            onPressed: () => QR.navigator.replaceLastName('sign-up'),
            child: const Text('Sign-Up'),
          ),
        ],
      ),
    );
  }
}

class SignInController extends QController {
  final TextEditingController textEditingController = TextEditingController();

  @override
  void onDispose() {
    textEditingController.dispose();
    super.onDispose();
  }
}

class SignUpDependencies extends QDependencyBinder {
  @override
  FutureOr<void> onInit() {
    super.onInit();
    QR.dependencies.inject(SignUpController());
  }
}

class SignUpPage extends StatelessWidget {
  const SignUpPage({Key? key}) : super(key: key);

  SignUpController get controller => QR.dependencies.get<SignUpController>();

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('Sign Up'),
          centerTitle: true,
        ),
        body: Column(
          children: [
            TextField(
              controller: controller.textEditingController,
            ),
            ElevatedButton(
              onPressed: () => QR.navigator.replaceAllWithName('sign-in'),
              child: const Text('Sign-In'),
            ),
          ],
        ),
      );
}

class SignUpController extends QController {
  final TextEditingController textEditingController = TextEditingController();

  @override
  void onDispose() {
    textEditingController.dispose();
    super.onDispose();
  }
}
codecov[bot] commented 2 years ago

Codecov Report

Merging #91 (ba4c7a3) into develop (f96f6c3) will not change coverage. The diff coverage is n/a.

:exclamation: Current head ba4c7a3 differs from pull request most recent head 3171fe6. Consider uploading reports for the commit 3171fe6 to get more accurate results

@@           Coverage Diff            @@
##           develop      #91   +/-   ##
========================================
  Coverage    84.67%   84.67%           
========================================
  Files           29       29           
  Lines         1103     1103           
========================================
  Hits           934      934           
  Misses         169      169           

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more