Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active January 7, 2021 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/b55d31066ad85195b1205f6b90ec605e to your computer and use it in GitHub Desktop.
Save felangel/b55d31066ad85195b1205f6b90ec605e to your computer and use it in GitHub Desktop.
[flutter_login] Login Form
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_login/authentication/authentication.dart';
import 'package:flutter_login/login/login.dart';
class LoginForm extends StatefulWidget {
final LoginBloc loginBloc;
final AuthenticationBloc authenticationBloc;
LoginForm({
Key key,
@required this.loginBloc,
@required this.authenticationBloc,
}) : super(key: key);
@override
State<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
LoginBloc get _loginBloc => widget.loginBloc;
@override
Widget build(BuildContext context) {
return BlocBuilder<LoginEvent, LoginState>(
bloc: _loginBloc,
builder: (
BuildContext context,
LoginState state,
) {
if (state is LoginFailure) {
_onWidgetDidBuild(() {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('${state.error}'),
backgroundColor: Colors.red,
),
);
});
}
return Form(
child: Column(
children: [
TextFormField(
decoration: InputDecoration(labelText: 'username'),
controller: _usernameController,
),
TextFormField(
decoration: InputDecoration(labelText: 'password'),
controller: _passwordController,
obscureText: true,
),
RaisedButton(
onPressed:
state is! LoginLoading ? _onLoginButtonPressed : null,
child: Text('Login'),
),
Container(
child:
state is LoginLoading ? CircularProgressIndicator() : null,
),
],
),
);
},
);
}
void _onWidgetDidBuild(Function callback) {
WidgetsBinding.instance.addPostFrameCallback((_) {
callback();
});
}
_onLoginButtonPressed() {
_loginBloc.dispatch(LoginButtonPressed(
username: _usernameController.text,
password: _passwordController.text,
));
}
}
@knvpk
Copy link

knvpk commented Feb 27, 2019

Hi, somebody explain what is the function here _onWidgetDidBuild

@xj42
Copy link

xj42 commented Apr 8, 2019

It is just a warpper for running the callback

@Kurtoid
Copy link

Kurtoid commented Oct 25, 2019

@felangel
Can you update line 30 so it reads
return BlocBuilder<LoginBloc, LoginState>(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment