flutter clean project
a sample flutter app using injection, routing and simple authentication follows clean code and best practices
features
- cleaned flutter project
- used linter with rules as errors
- configured route generator
- pass parameters / variables through routes
- simple authservice & configured guard for routes
- well structured flutter project contains screens, components, services and so on..
used packages
dependencies
dev_dependencies
getting started
project root folder
the project root folder is src
. in order to take advantage of vs code settings open src
as your work folder.
building
auto generated files are committed into this repository. if you changed any part of codes which used auto generators you will have a compilation error. this can be
resolved by running the auto generation command to create the sources necessary again
to compile the app.
after checking out the branch simply run:
flutter pub run build_runner build --delete-conflicting-outputs
you can also use the watch
command to constantly generate files on change:
flutter pub run build_runner watch --delete-conflicting-outputs
dependency injection
dependency injection is done by using
injectable package.
after you run the build_runner
command a file named
setup.config.dart
will get generated which holds all the injections.
routing
routing is done by using
autoroute package.
there is a service called router
in /services
which using autoroute package to generate routes and paths in app.
after you run the build_runner
command a file named
router.gr.dart
will get generated which holds all the routes and classes used for routing.
also for showcasing how to pass parameters from paths to screens, see /home/message/message_screen_v.dart
state management
selected state management for this project is cubit
.
the cubit is a subset of the famous implementation of bloc pattern: bloclibrary.dev, it abandons the concept of events and simplifies the way of emitting states.
there are 3 parts for each component / screen to managing the states.
-
name
_m.dart- model: this file contains definition of models for state management. to reduce duplicate codes like
copywith
,isa[class]
and so on, i used freezed package to auto generate needed classes and constructors for each state model.
- each model extends from basestate class.
- model: this file contains definition of models for state management. to reduce duplicate codes like
-
name
_v.dart- view: this file contains the view for our component / screen which extends from baseview class. the
baseview
class itself extends fromstatelesswidget
and has a getter to get the viewmodel inside the view. it’s usinglocateservice<t>()
and injectable di.
- view: this file contains the view for our component / screen which extends from baseview class. the
-
name
_vm.dart- viewmodel: this file contains the viewmodel for our component / screen which extends from baseviewmodel class (t is the state model class). the
baseviewmodel<t>
class itself extends fromcubit<t>
.
- viewmodel: this file contains the viewmodel for our component / screen which extends from baseviewmodel class (t is the state model class). the
screens
i defined 3 screens in this project.
- home
- the root path of application.
- have a link to
message
screen with passing a parameter to it. - using
authservice
to logout user. - showing
counter
component in it.
- have a link to
- the root path of application.
- message
- a screen to demo how to get a parameter in our routing system.
- the input parameter is defined in view’s constructor [
string message
]. - also in the routing service, i have to define the input parameter for this screen.
- the input parameter is defined in view’s constructor [
- a screen to demo how to get a parameter in our routing system.
- login
- login screen with a simple button and use
authservice
to do the fake login in application.- there is optional parameter called
redirectpath
which used by router to navigate user back to where he/she been after successful login. - to guard selected routes and navigate user to this screen, i defined class
authenticateduser
in the routing service class.
- there is optional parameter called
- login screen with a simple button and use
there is a file in each folder with
folder_name.dart
pattern name, which includes exports of files inside that folder. i use this pattern to minimize imports for other parts of application and classes. in usage we only need to importcomponents.dart
orscreens.dart
to use each folders implementations.
components
right now there is only one component in this project to demonstrate how to use components in screens.
- counter
- a simple counter component with cubit state management.
- i used this component inside the
home
screen.
images
contact me
if you have any question or suggestion, please contact me :
Comments are closed.