import 'package:doctor_app_flutter/providers/projects_provider.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 'package:shared_preferences/shared_preferences.dart'; import '../../models/user_model.dart'; import '../../providers/auth_provider.dart'; import '../../routes.dart'; class LoginForm extends StatefulWidget { LoginForm({ Key key, }) : super(key: key); @override _LoginFormState createState() => _LoginFormState(); } class _LoginFormState extends State { final loginFormKey = GlobalKey(); 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(context); return LayoutBuilder(builder: (ctx, constraints) { var smallScreenSize = 660; bool isSmallScreen = constraints.maxWidth <= smallScreenSize; return Form( key: loginFormKey, child: Container( width: constraints.maxWidth * 0.9, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( height: 20, ), TextFormField( decoration: InputDecoration( prefixIcon: Image.asset('assets/images/user_id_icon.png'), hintText: 'Enter ID', hintStyle: TextStyle( fontSize: isSmallScreen ? 14 : constraints.maxWidth * 0.024), 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 some text'; } return null; }, onSaved: (value) { userInfo.UserID = value; }, ), SizedBox( height: 20, ), TextFormField( obscureText: true, decoration: InputDecoration( prefixIcon: Image.asset('assets/images/password_icon.png'), hintText: 'Enter Password', hintStyle: TextStyle( fontSize: isSmallScreen ? 14 : constraints.maxWidth * 0.024), 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 some text'; } return null; }, onSaved: (value) { userInfo.Password = value; }), SizedBox( height: 20, ), // TextFormField( // decoration: InputDecoration( // prefixIcon: Image.asset('assets/images/hospital_icon.png'), // hintText: 'Select Project', // hintStyle: TextStyle( // fontSize: // isSmallScreen ? 14 : constraints.maxWidth * 0.024), // 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 some text'; // } // return null; // }, // ), // Container( decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(10.0)), // border: Border.all(color: Theme.of(context).primaryColor), ), width: double.infinity, child: DropdownButton( value: userInfo.ProjectID, iconSize: 24, elevation: 16, selectedItemBuilder: (BuildContext context) { return projectsList.map((item) { return Text(item['Desciption']); }).toList(); }, items: projectsList.map((item) { return DropdownMenuItem( child: Text('${item['Desciption']}'), value: item['ID'], ); }).toList(), onChanged: (val) { userInfo.ProjectID = val; }, ), ), SizedBox( height: 20, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Checkbox( value: true, activeColor: Theme.of(context).primaryColor, onChanged: (bool newValue) {}), Text("Remember me", style: TextStyle( fontSize: isSmallScreen ? 18 : constraints.maxWidth * 0.018)), ], ), ), 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: 140, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('LOG IN', style: TextStyle( fontSize: isSmallScreen ? 20 : constraints.maxWidth * 0.020)), Image.asset('assets/images/login_btn_arrow_icon.png') ], ), ), ) ], ), ], ), ), ); }); } login(context, AuthProvider authProv) { loginFormKey.currentState.validate(); loginFormKey.currentState.save(); authProv.login(userInfo).then((res) { if (res['MessageStatus'] == 1) { insertDeviceImei(res, authProv); // call insert imei // save imei on shared preferance // handel errors } else { // handel error } Navigator.of(context).pushNamed(HOME); }).catchError((err) { print('$err'); }); } insertDeviceImei(res, AuthProvider authProv) { if (_platformImei != 'Unknown') { var imeiInfo = { "IMEI": _platformImei, "LogInType": 1, "DoctorID": res['DoctorID'], "DoctorName": "Test User", "Gender": 1, "ClinicID": 3, "ProjectID": 15, "DoctorTitle": "Mr.", "ClinicName": "MED", "ProjectName": "", "DoctorImageURL": "UNKNOWN", "LogInTokenID": res['LogInTokenID'], "VersionID": 1.2 }; authProv.insertDeviceImei(imeiInfo).then((res) { if (res['MessageStatus'] == 1) { setSharedPref('platformImei', _platformImei); // save imei on shared preferance } else { // handel error } }).catchError((err) { print(err); }); } } // Platform messages are asynchronous, so we initialize in an async method. Future 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 setSharedPref(key, value) async { Future _prefs = SharedPreferences.getInstance(); final SharedPreferences prefs = await _prefs; prefs.setString(key, value).then((bool success) { print('EEEEEEEEEEee'); print(prefs.getString('platformImei')); return value; }); } getProjectsList() { ProjectsProvider projectsProv = Provider.of(context); projectsProv.getProjectsList().then((res) { if (res['MessageStatus'] == 1) { setState(() { projectsList = res['ListProject']; }); } else { // handel error } }).catchError((err) { print(err); }); } }