Created
October 29, 2019 20:21
-
-
Save Sorbh/9a1f588e1e5dfb62d4bb50f5a4c9b327 to your computer and use it in GitHub Desktop.
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:bic/data/PreferencesManager.dart'; | |
import 'package:bic/data/tachos_database.dart'; | |
import 'package:bic/home/home_page.dart'; | |
import 'package:bic/login/login_bloc.dart'; | |
import 'package:bic/login/login_event.dart'; | |
import 'package:bic/login/login_state.dart'; | |
import 'package:bic/res/colors.dart'; | |
import 'package:bic/res/strings.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:package_info/package_info.dart'; | |
import 'package:progress_dialog/progress_dialog.dart'; | |
class LoginForm extends StatefulWidget { | |
final LoginBloc loginBloc; | |
LoginForm({Key key, @required this.loginBloc}) : super(key: key); | |
@override | |
State<LoginForm> createState() => _LoginFormState(); | |
} | |
class _LoginFormState extends State<LoginForm> { | |
final focus = FocusNode(); | |
bool _obscurePassword = true; | |
final _loginTextContoller = TextEditingController(); | |
final _passwordTextController = TextEditingController(); | |
LoginBloc get _loginBloc => widget.loginBloc; | |
ProgressDialog _progressDialog; | |
String version = ""; | |
String buildNumber = ""; | |
@override | |
void initState() { | |
super.initState(); | |
String username = PreferencesManager.getPrefWithDefault(PreferencesManager.USER, ""); | |
String pass = PreferencesManager.getPrefWithDefault(PreferencesManager.PASS, ""); | |
print('Username - $username , Password - $pass'); | |
_loginTextContoller.text = username; | |
_passwordTextController.text = pass; | |
PackageInfo.fromPlatform().then((PackageInfo packageInfo) { | |
String appName = packageInfo.appName; | |
String packageName = packageInfo.packageName; | |
version = packageInfo.version; | |
buildNumber = packageInfo.buildNumber; | |
setState(() {}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return BlocListener( | |
bloc: _loginBloc, | |
listener: (context, state) { | |
print("LoginBlocListener " + state.toString()); | |
//hide progress bar in anycase | |
if (_progressDialog != null && _progressDialog.isShowing()) _progressDialog.hide(); | |
if (state is LoginLoading) { | |
if (_progressDialog == null) _progressDialog = new ProgressDialog(context, ProgressDialogType.Normal); | |
if (!_progressDialog.isShowing()) { | |
_progressDialog.setMessage(ProjectStrings.LOADING); | |
_progressDialog.show(); | |
} | |
} | |
if (state is LoginFailure) { | |
Scaffold.of(context).showSnackBar( | |
SnackBar( | |
content: Text('${state.error}'), | |
backgroundColor: Colors.red, | |
), | |
); | |
} | |
if (state is LoginFinish) { | |
goToNextScreen(); | |
} | |
// Scaffold.of(context).showSnackBar( | |
// SnackBar( | |
// content: Text(state.toString()), | |
// backgroundColor: Colors.red, | |
// ), | |
// ); | |
}, | |
child: BlocBuilder<LoginEvent, LoginState>( | |
bloc: _loginBloc, | |
condition: (previousState, currentState) { | |
if (currentState is LoginLoading || currentState is LoginFailure || currentState is LoginFinish) return false; | |
return true; | |
}, | |
builder: (context, state) { | |
print("LoingBlocBuilder " + state.toString()); | |
return Form( | |
child: SingleChildScrollView( | |
child: Container( | |
height: double.maxFinite, | |
decoration: new BoxDecoration(color: ProjectColors.COLOR_BACKGROUND), | |
child: Column( | |
children: [ | |
Padding( | |
padding: const EdgeInsets.only(top: 60.0), | |
), | |
Image.asset( | |
"data_repo/icon.png", | |
width: 160, | |
height: 160, | |
fit: BoxFit.cover, | |
), | |
Text( | |
"Version No : ${version}+${buildNumber}", | |
style: TextStyle(color: Colors.green, fontSize: 18), | |
), | |
Padding( | |
padding: const EdgeInsets.only(top: 40.0), | |
// child: Image( | |
// width: 160, | |
// height: 160, | |
// image: AssetImage(ProjectImages.BIC_LOGO), | |
// fit: BoxFit.contain) | |
), | |
Padding( | |
padding: EdgeInsetsDirectional.only(start: 50.0, end: 50.0), | |
child: TextFormField( | |
keyboardType: TextInputType.emailAddress, | |
textInputAction: TextInputAction.next, | |
onFieldSubmitted: (v) { | |
FocusScope.of(context).requestFocus(focus); | |
}, | |
controller: _loginTextContoller, | |
decoration: new InputDecoration( | |
hintText: ProjectStrings.MAIL, | |
), | |
), | |
), | |
Container( | |
padding: EdgeInsetsDirectional.only(start: 50.0, end: 50.0), | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.end, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Flexible( | |
child: TextFormField( | |
focusNode: focus, | |
onFieldSubmitted: (v) { | |
FocusScope.of(context).unfocus(); | |
}, | |
controller: _passwordTextController, | |
decoration: new InputDecoration(hintText: ProjectStrings.PASSWORD), | |
obscureText: _obscurePassword, | |
), | |
), | |
IconButton( | |
icon: _obscurePassword | |
? Icon(Icons.remove_red_eye, color: Colors.grey) | |
: Icon(Icons.remove_red_eye, color: Colors.blue), | |
onPressed: _toggle) | |
], | |
), | |
), | |
Padding( | |
padding: EdgeInsetsDirectional.only(top: 50.0), | |
child: MaterialButton( | |
minWidth: 200, | |
color: ProjectColors.COLOR_ACCENT, | |
child: Text( | |
ProjectStrings.AUTH, | |
style: TextStyle(fontSize: 16.0, color: Colors.white), | |
), | |
onPressed: _onLoginButtonPressed, | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
}, | |
), | |
); | |
} | |
void _toggle() { | |
setState(() { | |
_obscurePassword = !_obscurePassword; | |
}); | |
} | |
void goToNextScreen() { | |
Navigator.pushReplacement( | |
context, | |
MaterialPageRoute(builder: (context) { | |
return HomePage(); | |
}), | |
); | |
} | |
_onLoginButtonPressed() { | |
//on login clean the db first and create it again. | |
SystemChannels.textInput.invokeMethod('TextInput.hide').then((_) { | |
_loginBloc.dispatch( | |
LoginButtonPressed( | |
username: _loginTextContoller.text, | |
password: _passwordTextController.text, | |
), | |
); | |
}); | |
// TachosDatabase.get().refresh(null).then((_) { | |
// TachosDatabase.get().init().then((_) { | |
// }); | |
// }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment