import 'dart:async'; import 'dart:io'; import 'package:doctor_app_flutter/config/config.dart'; import 'package:doctor_app_flutter/config/shared_pref_kay.dart'; import 'package:doctor_app_flutter/config/size_config.dart'; import 'package:doctor_app_flutter/core/enum/viewstate.dart'; import 'package:doctor_app_flutter/core/model/hospitals/get_hospitals_response_model.dart'; import 'package:doctor_app_flutter/core/service/auth_service.dart'; import 'package:doctor_app_flutter/core/viewModel/imei_view_model.dart'; import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart'; import 'package:doctor_app_flutter/models/doctor/user_model.dart'; import 'package:doctor_app_flutter/screens/auth/verification_methods_screen.dart'; import 'package:doctor_app_flutter/screens/base/base_view.dart'; import 'package:doctor_app_flutter/util/helpers.dart'; import 'package:doctor_app_flutter/util/translations_delegate_base.dart'; import 'package:doctor_app_flutter/widgets/shared/app_loader_widget.dart'; import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart'; import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart'; import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart'; import 'package:doctor_app_flutter/widgets/shared/text_fields/app_text_form_field.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:hexcolor/hexcolor.dart'; import 'package:provider/provider.dart'; import '../../lookups/auth_lookup.dart'; import '../../util/dr_app_shared_pref.dart'; import '../../widgets/auth/auth_header.dart'; import '../../widgets/shared/app_scaffold_widget.dart'; DrAppSharedPreferances sharedPref = new DrAppSharedPreferances(); class Loginsreen extends StatefulWidget { @override _LoginsreenState createState() => _LoginsreenState(); } class _LoginsreenState extends State { String platformImei; final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); bool _isLoading = true; ProjectViewModel projectViewModel; AuthService authService = AuthService(); //TODO change AppTextFormField to AppTextFormFieldCustom final loginFormKey = GlobalKey(); var projectIdController = TextEditingController(); List projectsList = []; FocusNode focusPass = FocusNode(); FocusNode focusProject = FocusNode(); // HospitalViewModel hospitalViewModel; var userInfo = UserModel(); @override void initState() { super.initState(); _firebaseMessaging.setAutoInitEnabled(true); if (Platform.isIOS) { _firebaseMessaging.requestNotificationPermissions(); } _firebaseMessaging.getToken().then((String token) async { if (DEVICE_TOKEN == "" && projectViewModel.isLogin == false) { DEVICE_TOKEN = token; changeLoadingState(true); authService.selectDeviceImei(DEVICE_TOKEN).then((value) { print(authService.dashboardItemsList); if (authService.dashboardItemsList.length > 0) { sharedPref.setObj( LAST_LOGIN_USER, authService.dashboardItemsList[0]); Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (BuildContext context) => VerificationMethodsScreen( password: null, ))); } else { changeLoadingState(false); } }); } else { changeLoadingState(false); } }).catchError((err) { print(err); }); } void changeLoadingState(isLoading) { setState(() { _isLoading = isLoading; }); } @override Widget build(BuildContext context) { projectViewModel = Provider.of(context); return BaseView( onModelReady: (model) => {}, builder: (_, model, w) => AppScaffold( baseViewModel: model, isShowAppBar: false, backgroundColor: HexColor('#F8F8F8'), body: SafeArea( child: (_isLoading == false) ? ListView(children: [ Container( margin: EdgeInsetsDirectional.fromSTEB(30, 0, 30, 30), alignment: Alignment.topLeft, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ AuthHeader(loginType.knownUser), SizedBox( height: 40, ), Form( key: loginFormKey, child: Column( mainAxisAlignment: MainAxisAlignment .spaceBetween, children: [ Container( width: SizeConfig .realScreenWidth * 0.90, height: SizeConfig .realScreenHeight * 0.65, child: Column( crossAxisAlignment: CrossAxisAlignment .start, children: [ buildSizedBox(), Padding( child: AppText( TranslationBase .of(context) .enterCredentials, fontSize: 18, fontWeight: FontWeight .bold, ), padding: EdgeInsets.only( top: 10, bottom: 10)), Container( decoration: BoxDecoration( borderRadius: BorderRadius .all( Radius.circular( 6.0)), border: Border.all( width: 1.0, color: HexColor( "#CCCCCC"), ), color: Colors.white), child: Column( crossAxisAlignment: CrossAxisAlignment .start, children: [ Padding( padding: EdgeInsets .only( left: 10, top: 10), child: AppText( TranslationBase .of(context) .enterId, fontWeight: FontWeight .w800, fontSize: 14, )), AppTextFormField( labelText: '', borderColor: Colors .white, textInputAction: TextInputAction .next, validator: (value) { if (value != null && value .isEmpty) { return TranslationBase .of(context) .pleaseEnterYourID; } return null; }, onSaved: (value) { if (value != null) setState(() { userInfo .userID = value .trim(); }); }, onChanged: (value) { if (value != null) setState(() { userInfo .userID = value .trim(); }); }, onFieldSubmitted: ( _) { focusPass .nextFocus(); }, ) ])), buildSizedBox(), Container( decoration: BoxDecoration( borderRadius: BorderRadius .all( Radius.circular( 6.0)), border: Border.all( width: 1.0, color: HexColor( "#CCCCCC"), ), color: Colors.white), child: Column( crossAxisAlignment: CrossAxisAlignment .start, children: [ Padding( padding: EdgeInsets .only( left: 10, top: 10), child: AppText( TranslationBase .of(context) .enterPassword, fontWeight: FontWeight .w800, fontSize: 14, )), AppTextFormField( focusNode: focusPass, obscureText: true, borderColor: Colors .white, textInputAction: TextInputAction .next, validator: (value) { if (value != null && value .isEmpty) { return TranslationBase .of(context) .pleaseEnterPassword; } return null; }, onSaved: (value) { if (value != null) setState(() { userInfo .password = value; }); }, onChanged: (value){ if (value != null) setState(() { userInfo .password = value; }); }, onFieldSubmitted: ( _) { focusPass .nextFocus(); Helpers .showCupertinoPicker( context, projectsList, 'facilityName', onSelectProject); }, onTap: () { this.getProjects( userInfo .userID, model); }, ) ])), buildSizedBox(), projectsList.length > 0 ? Container( decoration: BoxDecoration( borderRadius: BorderRadius .all( Radius.circular( 6.0)), border: Border.all( width: 1.0, color: HexColor( "#CCCCCC"), ), color: Colors.white), child: Column( crossAxisAlignment: CrossAxisAlignment .start, children: [ Padding( padding: EdgeInsets .only( left: 10, top: 10), child: AppText( TranslationBase .of(context) .selectYourProject, fontWeight: FontWeight .w600, )), AppTextFormField( focusNode: focusProject, controller: projectIdController, borderColor: Colors .white, suffixIcon: Icons .arrow_drop_down, onTap: () { Helpers .showCupertinoPicker( context, projectsList, 'facilityName', onSelectProject); }, validator: ( value) { if (value != null && value .isEmpty) { return TranslationBase .of( context) .pleaseEnterYourProject; } return null; }) ])) : Container( decoration: BoxDecoration( borderRadius: BorderRadius .all( Radius.circular( 6.0)), border: Border.all( width: 1.0, color: HexColor( "#CCCCCC"), ), color: Colors.white), child: Column( crossAxisAlignment: CrossAxisAlignment .start, children: [ Padding( padding: EdgeInsets .only( left: 10, top: 10), child: AppText( TranslationBase .of(context) .selectYourProject, fontWeight: FontWeight .w800, fontSize: 14, )), AppTextFormField( readOnly: true, borderColor: Colors .white, prefix: IconButton( icon: Icon(Icons .arrow_drop_down), iconSize: 30, padding: EdgeInsets .only( bottom: 30), ), ) ])), ]), ), Row( mainAxisAlignment: MainAxisAlignment .end, children: [ Expanded( child: AppButton( title: TranslationBase .of(context) .login, color: HexColor( '#D02127'), disabled: userInfo .userID == null || userInfo.password == null, fontWeight: FontWeight .bold, onPressed: () { login(context, model); }, )), ], ) ], ), ) ], ) ])) ]) : Center(child: AppLoaderWidget()), ), )); } SizedBox buildSizedBox() { return SizedBox( height: 20, ); } login(context, IMEIViewModel model,) async { if (loginFormKey.currentState.validate()) { loginFormKey.currentState.save(); GifLoaderDialogUtils.showMyDialog(context); sharedPref.setInt(PROJECT_ID, userInfo.projectID); await model.login(userInfo); if (model.state == ViewState.ErrorLocal) { Helpers.showErrorToast(model.error); } else { if (model.loginInfo['MessageStatus'] == 1) { saveObjToString(LOGGED_IN_USER, model.loginInfo); sharedPref.remove(LAST_LOGIN_USER); sharedPref.setString(TOKEN, model.loginInfo['LogInTokenID']); GifLoaderDialogUtils.hideDialog(context); Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (BuildContext context) => VerificationMethodsScreen( password: userInfo.password, ))); } } } } Future setSharedPref(key, value) async { sharedPref.setString(key, value).then((success) { print("sharedPref.setString" + success.toString()); }); } saveObjToString(String key, value) async { sharedPref.setObj(key, value); } onSelectProject(index) { setState(() { userInfo.projectID = projectsList[index].facilityId; projectIdController.text = projectsList[index].facilityName; }); primaryFocus.unfocus(); } getProjects(memberID, IMEIViewModel model)async { if (memberID != null && memberID != '') { if (projectsList.length == 0) { await model.getHospitalsList(memberID); if(model.state == ViewState.Idle) { projectsList = model.hospitals; setState(() { userInfo.projectID = projectsList[0].facilityId; projectIdController.text = projectsList[0].facilityName; }); } } } } }