Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter flutter logo form bloc

create beautiful forms in flutter. the easiest way to prefill, async validation, update form fields, and show progress, failures, successes or navigate by reacting to the form state.

separate the form state and business logic from the user interface using form_bloc.

before to use this package you need to know the core concepts of bloc package and the basics of flutter_bloc

form bloc
form bloc – complexe login
form bloc – complex async
form bloc
form bloc – progress
form bloc – not auto validation
form bloc – fields
form bloc – fields async
form bloc – manually
form bloc – dynamic

widgets

note

formbloc, inputfieldbloc, textfieldbloc, booleanfieldbloc, selectfieldbloc, multiselectfieldbloc are blocs, so you can use blocbuilder or bloclistener of flutter_bloc for make any widget you want compatible with any fieldbloc or formbloc.

if you want me to add other widgets please let me know, or make a pull request.

examples

  • fieldblocs with async validation: blocui.
  • manually set fieldbloc error: blocui.
  • formbloc with submission progress: blocui.
  • formbloc without auto validation: blocui.
  • complex async prefilled formbloc: blocui.
  • and more examples.

basic example

dependencies:
  form_bloc: ^0.5.1
  flutter_form_bloc: ^0.4.2
  flutter_bloc: ^0.21.0
import 'package:form_bloc/form_bloc.dart';

class loginformbloc extends formbloc<string, string> {
  final emailfield = textfieldbloc(validators: [validators.email]);
  final passwordfield = textfieldbloc();

  final userrepository _userrepository;

  loginformbloc(this._userrepository);

  @override
  list<fieldbloc> get fieldblocs => [emailfield, passwordfield];

  @override
  stream<formblocstate<string, string>> onsubmitting() async* {
    try {
      _userrepository.login(
        email: emailfield.value,
        password: passwordfield.value,
      );
      yield currentstate.tosuccess();
    } catch (e) {
      yield currentstate.tofailure();
    }
  }
}
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
import 'package:flutter_form_bloc_example/forms/simple_login_form_bloc.dart';
import 'package:flutter_form_bloc_example/widgets/widgets.dart';

class loginform extends statelesswidget {
  @override
  widget build(buildcontext context) {
    return blocprovider<loginformbloc>(
      builder: (context) =>
          loginformbloc(repositoryprovider.of<userrepository>(context)),
      child: builder(
        builder: (context) {
          final formbloc = blocprovider.of<loginformbloc>(context);

          return scaffold(
            appbar: appbar(title: text('simple login')),
            body: formbloclistener<loginformbloc, string, string>(
              onsubmitting: (context, state) => loadingdialog.show(context),
              onsuccess: (context, state) {
                loadingdialog.hide(context);
                navigator.of(context).pushreplacementnamed('success');
              },
              onfailure: (context, state) {
                loadingdialog.hide(context);
                notifications.showsnackbarwitherror(
                    context, state.failureresponse);
              },
              child: listview(
                children: <widget>[
                  textfieldblocbuilder(
                    textfieldbloc: formbloc.emailfield,
                    keyboardtype: textinputtype.emailaddress,
                    decoration: inputdecoration(
                      labeltext: 'email',
                      prefixicon: icon(icons.email),
                    ),
                  ),
                  textfieldblocbuilder(
                    textfieldbloc: formbloc.passwordfield,
                    suffixbutton: suffixbutton.obscuretext,
                    decoration: inputdecoration(
                      labeltext: 'password',
                      prefixicon: icon(icons.lock),
                    ),
                  ),
                  padding(
                    padding: const edgeinsets.all(8.0),
                    child: raisedbutton(
                      onpressed: formbloc.submit,
                      child: center(child: text('login')),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

basic usage

1. import it

import 'package:form_bloc/form_bloc.dart';

2. create a class that extends formbloc<successresponse, failureresponse>

formbloc<successresponse, failureresponse>

successresponse the type of the success response.

failureresponse the type of the failure response.

for example, the successresponse type and failureresponse type of loginformbloc will be string.

import 'package:form_bloc/form_bloc.dart';

class loginformbloc extends formbloc<string, string> {}

2. create field blocs

you need to create field blocs, and these need to be final.

you can create:

for example the loginformbloc will have two textfieldbloc.

import 'package:form_bloc/form_bloc.dart';

class loginformbloc extends formbloc<string, string> {
  final emailfield = textfieldbloc(validators: [validators.email]);
  final passwordfield = textfieldbloc();
}

3. add services/repositories

in this example we need a userrepository for make the login.

import 'package:form_bloc/form_bloc.dart';

class loginformbloc extends formbloc<string, string> {
  final emailfield = textfieldbloc(validators: [validators.email]);
  final passwordfield = textfieldbloc();

  final userrepository _userrepository;

  loginformbloc(this._userrepository);
}

4. implement the get method fieldblocs

you need to override the get method fieldblocs and return a list with all fieldblocs.

for example the loginformbloc need to return a list with emailfield and passwordfield.

import 'package:form_bloc/form_bloc.dart';

class loginformbloc extends formbloc<string, string> {
  final emailfield = textfieldbloc(validators: [validators.email]);
  final passwordfield = textfieldbloc();

  final userrepository _userrepository;

  loginformbloc(this._userrepository);

  @override
  list<fieldbloc> get fieldblocs => [emailfield, passwordfield];
}

5. implement onsubmitting method

onsubmitting returns a stream<formblocstate<successresponse, failureresponse>>.

this method is called when you call loginformbloc.submit() and formblocstate.isvalid is true, so each field bloc has a valid value.

you can get the current value of each field bloc calling emailfield.value or passwordfield.value.

you must call all your business logic of this form here, and yield the corresponding state.

you can yield a new state using:

see other states here.

for example onsubmitting of loginformbloc will return a stream<formblocstate<string, string>> and yield currentstate.tosuccess() if the _userrepository.login method not throw any exception, and yield “currentstate.tofailure()` if throw a exception.

import 'package:form_bloc/form_bloc.dart';

class loginformbloc extends formbloc<string, string> {
  final emailfield = textfieldbloc(validators: [validators.email]);
  final passwordfield = textfieldbloc();

  final userrepository _userrepository;

  loginformbloc(this._userrepository);

  @override
  list<fieldbloc> get fieldblocs => [emailfield, passwordfield];

  @override
  stream<formblocstate<string, string>> onsubmitting() async* {
    try {
      _userrepository.login(
        email: emailfield.value,
        password: passwordfield.value,
      );
      yield currentstate.tosuccess();
    } catch (e) {
      yield currentstate.tofailure();
    }
  }
}

6. create a form widget

you need to create a widget with access to the formbloc.

in this case i will use blocprovider for do it.

class loginform extends statelesswidget {
  @override
  widget build(buildcontext context) {
    return blocprovider<loginformbloc>(
      builder: (context) =>
          loginformbloc(repositoryprovider.of<userrepository>(context)),
      child: builder(
        builder: (context) {
          final formbloc = blocprovider.of<loginformbloc>(context);

          return scaffold();
        },
      ),
    );
  }
}

6. add formbloclistener for manage form state changes

you need to add a formbloclistener.

in this example:

  • i will show a loading dialog when the state is loading.
  • i will hide the dialog when the state is success, and navigate to success screen.
  • i will hide the dialog when the state is failure, and show a snackbar with the error.
...
          return scaffold(
            appbar: appbar(title: text('simple login')),
            body: formbloclistener<loginformbloc, string, string>(
              onsubmitting: (context, state) => loadingdialog.show(context),
              onsuccess: (context, state) {
                loadingdialog.hide(context);
                navigator.of(context).pushreplacementnamed('success');
              },
              onfailure: (context, state) {
                loadingdialog.hide(context);
                notifications.showsnackbarwitherror(
                    context, state.failureresponse);
              },
              child: 
              ),
            ),
          );
...          

6. connect the field blocs with field blocs builder

in this example i will use textfieldblocbuilder for connect with emailfield and passwordfield of loginformbloc.

...
              child: listview(
                children: <widget>[
                  textfieldblocbuilder(
                    textfieldbloc: formbloc.emailfield,
                    keyboardtype: textinputtype.emailaddress,
                    decoration: inputdecoration(
                      labeltext: 'email',
                      prefixicon: icon(icons.email),
                    ),
                  ),
                  textfieldblocbuilder(
                    textfieldbloc: formbloc.passwordfield,
                    suffixbutton: suffixbutton.obscuretext,
                    decoration: inputdecoration(
                      labeltext: 'password',
                      prefixicon: icon(icons.lock),
                    ),
                  ),
                ],
              ),
...          

7. add a widget for submit the formbloc

in this example i will add a raisedbutton and pass submit method of formbloc to submit the form.

...
              child: listview(
                children: <widget>[
                  ...,
                  padding(
                    padding: const edgeinsets.all(8.0),
                    child: raisedbutton(
                      onpressed: formbloc.submit,
                      child: center(child: text('login')),
                    ),
                  ),
                ],
              ),
...          

download the full project for this post from the following button

this source is fully free for all time

download as zip


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.