LoginForm.dart (2176B)
1 import 'package:flutter/cupertino.dart'; 2 import 'package:flutter/material.dart'; 3 import 'package:flutter_bloc/flutter_bloc.dart'; 4 5 import '../blocs/LoginBloc.dart'; 6 import '../events/LoginEvent.dart'; 7 import '../states/LoginState.dart'; 8 9 class LoginForm extends StatefulWidget { 10 @override 11 State<LoginForm> createState() => _LoginFormState(); 12 } 13 14 class _LoginFormState extends State<LoginForm> { 15 final _usernameController = TextEditingController(); 16 final _passwordController = TextEditingController(); 17 18 @override 19 Widget build(BuildContext context) { 20 _onLoginButtonPressed() { 21 BlocProvider.of<LoginBloc>(context).add( 22 LoginButtonPressed( 23 username: _usernameController.text, 24 password: _passwordController.text, 25 ), 26 ); 27 } 28 29 return BlocListener<LoginBloc, LoginState>( 30 listener: (context, state) { 31 if (state is LoginFailure) { 32 Scaffold.of(context).showSnackBar( 33 SnackBar( 34 content: Text('${state.error}'), 35 backgroundColor: Colors.red, 36 ), 37 ); 38 } 39 }, 40 child: BlocBuilder<LoginBloc, LoginState>( 41 builder: (context, state) { 42 return Form( 43 child: Column( 44 children: [ 45 TextFormField( 46 decoration: InputDecoration( 47 hintText: 'username', 48 ), 49 controller: _usernameController, 50 ), 51 TextFormField( 52 decoration: InputDecoration( 53 hintText: 'password', 54 ), 55 controller: _passwordController, 56 obscureText: true, 57 ), 58 RaisedButton( 59 onPressed: 60 state is! LoginLoading ? _onLoginButtonPressed : null, 61 child: Text('Login'), 62 ), 63 Container( 64 child: state is LoginLoading 65 ? CircularProgressIndicator() 66 : null, 67 ), 68 ], 69 ), 70 ); 71 }, 72 ), 73 ); 74 } 75 }