Coder Social home page Coder Social logo

bloc_auth_flow's Introduction

Flutter bloc auth project

Getting Started

  • create a Login Page with Username & Password Field (TextFormField because it comes with the option to add a validator)
  • define the actions/events (class LoginEvent)
    • username and password can change and the button can be clicked
    • lets call these events: LoginUsernameChanged, LoginPasswordChanged, LoginSubmitted
    • The first 2 events hold onto the values of their related Inputfields, that they are responsible for watching, whereas the LoginSubmitted is just an event that you can add, and nothing needs to be passed in to the constructor, because you will be actually able to keep track of the username and password from the state.
  • Next create the LoginState which is kind of the counter part that essentially gonna takes the event be mapped to the state. so consider: whenever you update the username, you dont want to go back to fetch the password and vice versa
    • copyWith() constructor ..which allows to update one value without needing to pass in the other value And 2nd part of the State is the Submission for which you can create a separate (abstract) class to keep track of the 4 "FormSubmissionStatus" (initial, formsubmitting, submit success, submit failure)
    • after defining those, create a 3rd parameter in the LoginState class "formStatus" of Type FormSubmissionStatus (and assign InitialFormStatus to it in the constructor)
  • now create the LoginBloc that is interacting with those 2 objects (the event and the state)
  • create the auth repository to simulate (for now) the login with a Future.delayed and get this loading state
  • pass this auth repo to the LoginBloc constructor (so that you can call login() on the LoginSubmitted event)
  • supplying the repository in main.dart so that it can be accessed further down the widget tree in other word: wrap the LoginView in the "RepositoryProvider"
  • in login_view wrap the _loginForm() with a BlocProvider and access the AuthRepo via the context
  • wrap usernameField() and passwordField() with a BlocBuilder
  • you could do the validation inside loginState instead of determine whether a value (username or password) is valid or not directly in the UI (inside the TextFormField) You can achieve that by simply adding additional boolean fields like:
bool get isValidUsername => usernmae.length > 6

or with the help of a little extension..

extension StringValidators on String {
  bool get containsUppercase => contains(RegExp(r'[A-Z]'));
  bool get containsLowercase => contains(RegExp(r'[a-z]'));
}
 final String password;
bool get isValidPassword => password.length >= 6 &&
                            password.containsUppercase &&
                            password.containsLower;
  • next wrap the login Button in a blocBuilder too and then watch for the state to make sure whether to show the button or (when in submitting state) a circularProgressIndicator..

bloc_auth_flow's People

Contributors

haudegenhh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.