Skip to content

Instantly share code, notes, and snippets.

@felangel
Created October 8, 2018 16:56
Show Gist options
  • Save felangel/d39a3a70cb44274380b37cdfe06d772f to your computer and use it in GitHub Desktop.
Save felangel/d39a3a70cb44274380b37cdfe06d772f to your computer and use it in GitHub Desktop.
[bloc_package] LoginForm
class LoginForm extends StatelessWidget {
final LoginBloc loginBloc;
final usernameController = TextEditingController();
final passwordController = TextEditingController();
const LoginForm({Key key, @required this.loginBloc}): super(key: key);
@override
Widget build(BuildContext context) {
return BlocBuilder<LoginState>(
bloc: loginBloc,
builder: (
BuildContext context,
LoginState loginState,
) {
if (loginState.token.isNotEmpty) {
// user is authenticated do something...
}
return Form(
child: Column(
children: [
Text(
loginState.error,
),
TextFormField(
controller: usernameController,
),
TextFormField(
controller: passwordController,
obscureText: true,
),
RaisedButton(
onPressed: loginState.isLoginButtonEnabled
? _onLoginButtonPressed
: null,
child: Text('Login'),
),
Container(
child: loginState.isLoading
? CircularProgressIndicator()
: null,
),
],
),
);
},
);
}
_onLoginButtonPressed() {
loginBloc.onLoginButtonPressed(
username: usernameController.text,
password: passwordController.text,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment