Built with get pub, flutter_modular empowers modular Flutter development. It simplifies app creation by enabling you to decompose screens into reusable modules that can be effortlessly combined to build entirely new applications.
The motivation behind developing flutter_modular stems from the following challenges faced during app development:
-
Inefficient Debugging with Platform-Specific Plugins:
-
Dart-based features are efficiently debuggable on the web. However, introducing plugins exclusive to Android/iOS necessitates testing on mobile devices, a time-consuming process.
-
Removing these plugins leads to compilation errors due to missing libraries, forcing developers to comment out relevant code.
-
Restoring the code is required for debugging plugin-related features, causing a tedious back-and-forth cycle.
-
Solution: A mechanism to seamlessly add/remove Android/iOS plugins without affecting project code.
-
-
Efficient Code Reuse Across Projects:
-
Simultaneous development of multiple projects demands efficient reuse of existing pages.
-
Solution: A robust system for reusing and sharing common pages across projects.
-
-
Rapid App Development with Modular Reuse:
-
The ability to quickly assemble new apps by leveraging existing modules.
-
Solution: A framework for effortless module composition and rapid app creation.
-
flutter_modular addresses these challenges by providing a modular architecture that streamlines development and enhances code reusability.
- Decompose screens into reusable modules with ease.
- Easily combine modules to build new applications.
- Seamlessly add or remove modules without impacting your host app's codebase, no compilation errors.
- The root directory contains the core
flutter_modularlibrary code. - The
exampledirectory showcases a sample project demonstrating how to use flutter_modular. - The
exampledirectory includes submodules namedmodule_a,module_b, androutes_manager.
Thanks to get. It allows the total decoupling of the View, presentation logic, business logic, dependency injection, and navigation.
Basing on get, flutter_modular can decouple presentation logic, business logic and navigation into modules.
flutter_modular has two core components:
ModuleInterfaceexports the following methods:loadModulegives each module a chance to provide routes they support. But the screen widget should be provided by the modules.toNamedis a method that allows the host app to navigate to a screen in loaded modules. If a module is not loaded, navigating to the screen in the module will fail.
BaseModuleis a base class that implementsModuleInterface. It provides a default implementation ofloadModule, but provides no routes. It also provides a default implementation oftoNamed. But won't navigate to anywhere.
The main idea is host app can denpends any modules and can be moved at any time. When a module is absent we use a 'BaseModule' to replace it and prevent compilation errors and prevent navigate to the route in it.
That's it!
You can refer to the example project. It helps you understand more about how to use flutter_modular.
The example dendends the following modules:
routes_manager, which is the module that manages all the routes names, route arguments and result types. We can treat it as a package for models. Models should be shared in any projects. So we make it a standalone module.module_aandmodule_b, which are the modules that will be loaded. Both of them implementsModuleInterfaceand dependsroutes_manager.module_interface, which is the module that provides theModuleInterfaceandBaseModule.
Here is the key trick to know whearther a module is loaded or not.
Follow below steps to get started:
-
Let the example project run:
flutter pub add module_a module_b routes_manager flutter_modular
to add the modules to the project.
-
Wirte a script to read modules from the
pubspec.yamlfile. In the example,modules_reader.dartreadspubspec.yamlto find out each module is loaded or not. If a module is loaded create it Module instance, likeModuleAinstance. If not, create aBaseModuleinstance. The write the objects intolib/src/modules.dartfile. -
In example app, we use modules in
lib/src/modules.dartto load their routes. -
Try to remove add add modules to see how it works.
- Add
flutter_modularto yourpubspec.yaml. - Make a module defines all the routes all modules will provide. No matter a module will be loaded or not.
- Add modules to
pubspec.yaml. - Write a script to read modules from host app
pubspec.yamlfile. The script generates module instance when loaded andBaseModulewhen not loaded. - In host app, use modules in
lib/src/modules.dartto load their routes.
Any feedback and suggestions are appreciated.
I'm looking forward a part time job, please contact me if you are interested.
If this repo is useful, please give it a star. Buy me a coffee if you like.
Thank you.
MIT License