Last active
January 7, 2021 01:58
-
-
Save felangel/b55d31066ad85195b1205f6b90ec605e to your computer and use it in GitHub Desktop.
[flutter_login] Login Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
)); | |
} | |
} |
It is just a warpper for running the callback
@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
Hi, somebody explain what is the function here
_onWidgetDidBuild