You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
doctor_app_flutter/lib/widgets/auth/login_form.dart

335 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:imei_plugin/imei_plugin.dart';
import 'package:provider/provider.dart';
import '../../config/size_config.dart';
import '../../models/user_model.dart';
import '../../providers/auth_provider.dart';
import '../../providers/projects_provider.dart';
import '../../routes.dart';
import '../../util/dr_app_shared_pref.dart';
import '../../util/dr_app_toast_msg.dart';
DrAppSharedPreferances sharedPref = DrAppSharedPreferances();
DrAppToastMsg toastMsg = DrAppToastMsg();
class LoginForm extends StatefulWidget with DrAppToastMsg {
LoginForm({
Key key,
}) : super(key: key);
@override
_LoginFormState createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final loginFormKey = GlobalKey<FormState>();
String _platformImei = 'Unknown';
String uniqueId = "Unknown";
var projectsList = [];
var userInfo = UserModel(
UserID: '',
Password: '',
ProjectID: 15,
LanguageID: 2,
IPAdress: "11.11.11.11",
VersionID: 1.2,
Channel: 9,
SessionID: "i1UJwCTSqt");
@override
void initState() {
super.initState();
initPlatformState();
}
@override
Widget build(BuildContext context) {
if (projectsList.length == 0) {
getProjectsList();
}
AuthProvider authProv = Provider.of<AuthProvider>(context);
return Form(
key: loginFormKey,
child: Container(
width: SizeConfig.widthMultiplier * 90,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
buildSizedBox(),
TextFormField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
prefixIcon: Image.asset('assets/images/user_id_icon.png'),
hintText: 'Enter ID',
hintStyle: TextStyle(fontSize: 2 * SizeConfig.textMultiplier),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(color: Hexcolor('#CCCCCC')),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide:
BorderSide(color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your ID';
}
return null;
},
onSaved: (value) {
userInfo.UserID = value;
},
),
buildSizedBox(),
TextFormField(
obscureText: true,
decoration: InputDecoration(
prefixIcon: Image.asset('assets/images/password_icon.png'),
hintText: 'Enter Password',
hintStyle:
TextStyle(fontSize: 2 * SizeConfig.textMultiplier),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(color: Hexcolor('#CCCCCC')),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide:
BorderSide(color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your Password';
}
return null;
},
onSaved: (value) {
userInfo.Password = value;
}),
buildSizedBox(),
DropdownButtonFormField(
value: userInfo.ProjectID,
isExpanded: false,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10),
isDense: true,
prefixIcon: Image.asset('assets/images/hospital_icon.png'),
// hintText: 'Enter Password',
hintStyle:
TextStyle(fontSize: 2 * SizeConfig.textMultiplier),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(color: Hexcolor('#CCCCCC')),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide:
BorderSide(color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) {
print(value);
if (value == null) {
return 'Please select your project';
}
return null;
},
items: projectsList.map((item) {
return DropdownMenuItem(
child: Text(
'${item['Desciption']}',
),
value: item['ID'],
);
}).toList(),
onChanged: (value) {
print(value);
setState(() {
userInfo.ProjectID = value;
});
}),
buildSizedBox(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Checkbox(
value: true,
activeColor: Theme.of(context).primaryColor,
onChanged: (bool newValue) {}),
Text("Remember me",
style: TextStyle(
fontSize: 2 * SizeConfig.textMultiplier)),
],
),
),
RaisedButton(
onPressed: () {
login(context, authProv);
},
textColor: Colors.white,
elevation: 0.0,
padding: const EdgeInsets.all(0.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
side: BorderSide(width: 0.5, color: Hexcolor('#CCCCCC'))),
child: Container(
padding: const EdgeInsets.all(10.0),
height: 50,
width: SizeConfig.widthMultiplier * 30,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('LOGIN',
style: TextStyle(
fontSize: SizeConfig.isMobilePortrait
? 2.3 * SizeConfig.textMultiplier
: 2.3 * SizeConfig.textMultiplier)),
Image.asset('assets/images/login_btn_arrow_icon.png')
],
),
),
)
],
),
],
),
),
);
}
SizedBox buildSizedBox() {
return SizedBox(
height: 20,
);
}
login(context, AuthProvider authProv) {
if (loginFormKey.currentState.validate()) {
loginFormKey.currentState.save();
authProv.login(userInfo).then((res) {
if (res['MessageStatus'] == 1) {
insertDeviceImei(res, authProv);
} else {
// handel error
// widget.showCenterShortLoadingToast("watting");
showLoginError(res['ErrorEndUserMessage']);
}
// Navigator.of(context).pushNamed(HOME);
}).catchError((err) {
print('$err');
showLoginError();
});
}
}
insertDeviceImei(preRes, AuthProvider authProv) {
if (_platformImei != 'Unknown') {
var imeiInfo = {
"IMEI": _platformImei,
"LogInType": 1,
"DoctorID": preRes['DoctorID'],
"DoctorName": "Test User",
"Gender": 1,
"ClinicID": 3,
"ProjectID": 15,
"DoctorTitle": "Mr.",
"ClinicName": "MED",
"ProjectName": "",
"DoctorImageURL": "UNKNOWN",
"LogInTokenID": preRes['LogInTokenID'],
"VersionID": 1.2
};
authProv.insertDeviceImei(imeiInfo).then((res) {
if (res['MessageStatus'] == 1) {
setSharedPref('platformImei', _platformImei);
saveObjToString('loggedUser', preRes);
Navigator.of(context).pushNamed(HOME);
// save imei on shared preferance
} else {
// handel error
showLoginError(res['ErrorEndUserMessage']);
}
}).catchError((err) {
print(err);
showLoginError();
});
}
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformImei;
String idunique;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformImei =
await ImeiPlugin.getImei(shouldShowRequestPermissionRationale: false);
idunique = await ImeiPlugin.getId();
} on PlatformException {
platformImei = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformImei = platformImei;
uniqueId = idunique;
});
}
Future<void> setSharedPref(key, value) async {
sharedPref.setString(key, value).then((success) {
print("sharedPref.setString" + success.toString());
});
}
getProjectsList() {
ProjectsProvider projectsProv = Provider.of<ProjectsProvider>(context);
projectsProv.getProjectsList().then((res) {
if (res['MessageStatus'] == 1) {
setState(() {
projectsList = res['ListProject'];
});
} else {
// handel error
}
}).catchError((err) {
print(err);
});
}
showLoginError([msg = null]) {
String localMsg = 'Something wrong happened, please contact the admin';
if (msg != null) {
localMsg = msg.toString();
}
toastMsg.showErrorToast(localMsg);
}
saveObjToString(String key, value) async {
sharedPref.setObj(key, value);
}
}