· 5 years ago · Jul 01, 2020, 07:20 AM
1import 'dart:convert';
2
3import 'package:yasalala/models/user.dart';
4import 'package:yasalala/screens/register.dart';
5import 'package:yasalala/utility/api.dart';
6import 'package:yasalala/utility/app_locatization.dart';
7import 'package:flutter/material.dart';
8import 'package:yasalala/utility/colors.dart';
9import 'package:progress_dialog/progress_dialog.dart';
10import 'package:shared_preferences/shared_preferences.dart';
11import 'package:connectivity/connectivity.dart';
12
13ProgressDialog pr;
14
15class Login extends StatefulWidget {
16 Login({Key key}) : super(key: key);
17
18 _LoginState createState() => _LoginState();
19}
20
21class _LoginState extends State<Login> {
22 SharedPreferences prefs;
23 String language;
24 final _formKey = GlobalKey<FormState>();
25 final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
26
27 bool _autoValidate = false;
28 String _param;
29 String _password;
30
31 @override
32 void initState() {
33 loadLanguage();
34 super.initState();
35 }
36
37 void showSnakeBar(String message) {
38 _scaffoldKey.currentState.showSnackBar(SnackBar(
39 content: Text(message),
40 duration: Duration(seconds: 3),
41 ));
42 }
43
44 void loadLanguage() async {
45 prefs = await SharedPreferences.getInstance();
46 setState(() {
47 language = prefs.getString('language');
48 });
49 }
50
51 void validateLogin(BuildContext context) async {
52 if (_formKey.currentState.validate()) {
53 var connectivityResult = await (Connectivity().checkConnectivity());
54 if (connectivityResult != ConnectivityResult.none) {
55 pr.show();
56 _formKey.currentState.save();
57 Api api = Api();
58 var data = {
59 'param': _param,
60 'password': _password,
61 };
62 Map response = await api.post('v1/user/login', json.encode(data));
63
64 if (response != null && response['statusCode'] != null) {
65 var statusCode = int.parse(response['statusCode'].toString());
66 if (statusCode == 200) {
67 Map body = jsonDecode(response['body']);
68 if (int.parse(body['status'].toString()) == 1) {
69 User user = User.fromMap(body['data']);
70 bool saved = await user.save();
71 if (saved) {
72 Navigator.pop(context);
73 }
74 }
75 } else if (statusCode == 422) {
76 Map body = jsonDecode(response['body']);
77 showSnakeBar(body['error']);
78 }else{
79 showSnakeBar("Something went wrong. Please try again after sometime.");
80 }
81 } else {
82 showSnakeBar("Something went wrong. Please try again after sometime.");
83 }
84 pr.hide();
85 } else {
86 showSnakeBar( "No internet connection");
87 }
88 } else {
89 setState(() {
90 _autoValidate = true;
91 });
92 }
93 }
94
95 @override
96 Widget build(BuildContext context) {
97 pr = new ProgressDialog(context, type: ProgressDialogType.Normal);
98 pr.style(
99 message: AppLocalization.of(context).translate('logging_in'),
100 progressWidget: Container(
101 padding: EdgeInsets.all(8.0),
102 child: CircularProgressIndicator(
103 valueColor: AlwaysStoppedAnimation<Color>(primaryColor),
104 )));
105
106 return Form(
107 key: _formKey,
108 autovalidate: _autoValidate,
109 child: loginUi(context),
110 );
111 }
112
113 Widget loginUi(BuildContext context) {
114 return Scaffold(
115 key: _scaffoldKey,
116 backgroundColor: Colors.white,
117 appBar: PreferredSize(
118 preferredSize: Size.fromHeight(140.0),
119 child: AppBar(
120 centerTitle: false,
121 flexibleSpace: Container(
122 margin: EdgeInsets.symmetric(vertical: 35.0, horizontal: 20.0),
123 child: Align(
124 alignment: Alignment.bottomLeft,
125 child: Text(
126 AppLocalization.of(context).translate('sign_in'),
127 style: TextStyle(
128 color: Color(0xFF323643),
129 fontSize: 26.0,
130 fontWeight: FontWeight.w700),
131 ),
132 ),
133 ),
134 elevation: 0.0,
135 backgroundColor: Colors.white,
136 iconTheme: IconThemeData(color: Color(0xFFC5CCD6)),
137 ),
138 ),
139 body: SingleChildScrollView(
140 child: Container(
141 width: MediaQuery.of(context).size.width - 40,
142 margin: EdgeInsets.symmetric(horizontal: 20.0),
143 child: Center(
144 child: Column(
145 mainAxisAlignment: MainAxisAlignment.center,
146 crossAxisAlignment: CrossAxisAlignment.center,
147 children: <Widget>[
148 TextFormField(
149 validator: (value) {
150 if (value.isEmpty) {
151 return AppLocalization.of(context)
152 .translate('please_enter_email_or_mobile');
153 }
154 return null;
155 },
156 onSaved: (String val) {
157 _param = val;
158 },
159 cursorColor: Color(0xFF979797),
160 decoration: InputDecoration(
161 labelText: AppLocalization.of(context)
162 .translate('email_or_mobile'),
163 labelStyle: TextStyle(color: lightGrey),
164 focusColor: Colors.black,
165 enabledBorder: UnderlineInputBorder(
166 borderSide: BorderSide(color: Color(0xFF979797))),
167 border: UnderlineInputBorder(
168 borderSide: BorderSide(color: Color(0xFF979797))),
169 focusedErrorBorder: UnderlineInputBorder(
170 borderSide: BorderSide(color: Color(0xFF979797))),
171 disabledBorder: UnderlineInputBorder(
172 borderSide: BorderSide(color: Color(0xFF979797))),
173 errorBorder: UnderlineInputBorder(
174 borderSide: BorderSide(color: Color(0xFF979797))),
175 focusedBorder: UnderlineInputBorder(
176 borderSide: BorderSide(color: Color(0xFF979797)))),
177 ),
178 SizedBox(
179 height: 20.0,
180 ),
181 TextFormField(
182 validator: (value) {
183 if (value.isEmpty) {
184 return AppLocalization.of(context)
185 .translate('please_enter_password');
186 }
187 return null;
188 },
189 onSaved: (String val) {
190 _password = val;
191 },
192 obscureText: true,
193 cursorColor: Color(0xFF979797),
194 decoration: InputDecoration(
195 labelText:
196 AppLocalization.of(context).translate('password'),
197 labelStyle: TextStyle(color: lightGrey),
198 focusColor: Colors.black,
199 enabledBorder: UnderlineInputBorder(
200 borderSide: BorderSide(color: Color(0xFF979797))),
201 border: UnderlineInputBorder(
202 borderSide: BorderSide(color: Color(0xFF979797))),
203 focusedErrorBorder: UnderlineInputBorder(
204 borderSide: BorderSide(color: Color(0xFF979797))),
205 disabledBorder: UnderlineInputBorder(
206 borderSide: BorderSide(color: Color(0xFF979797))),
207 errorBorder: UnderlineInputBorder(
208 borderSide: BorderSide(color: Color(0xFF979797))),
209 focusedBorder: UnderlineInputBorder(
210 borderSide: BorderSide(color: Color(0xFF979797)))),
211 ),
212 SizedBox(
213 height: 20.0,
214 ),
215 GestureDetector(
216 onTap: () async {
217 validateLogin(context);
218 },
219 child: Container(
220 width: MediaQuery.of(context).size.width - 75,
221 height: 48,
222 decoration: BoxDecoration(
223 gradient: LinearGradient(
224 colors: [primaryColor, secondaryColorGreen],
225 begin: Alignment.topLeft,
226 end: Alignment.bottomRight),
227 borderRadius: BorderRadius.circular(6.0),
228 boxShadow: [
229 BoxShadow(
230 color: Color(0xFF9DA3B4).withOpacity(0.1),
231 blurRadius: 65.0,
232 offset: Offset(0.0, 15.0))
233 ]),
234 child: Center(
235 child: Text(
236 AppLocalization.of(context).translate('sign_in'),
237 style: TextStyle(
238 color: Color(0xFFFBFBFB),
239 fontSize: 18.0,
240 fontWeight: FontWeight.w500),
241 ),
242 ),
243 ),
244 ),
245 SizedBox(
246 height: 20.0,
247 ),
248 GestureDetector(
249 onTap: () async {
250 var router = new MaterialPageRoute(
251 builder: (BuildContext context) => Register());
252 Navigator.of(context).pushReplacement(router);
253 },
254 child: Text(
255 AppLocalization.of(context)
256 .translate('new_here_create_the_account'),
257 style: TextStyle(
258 color: blueGrey,
259 fontSize: 15.0,
260 fontWeight: FontWeight.w500,
261 decoration: TextDecoration.none),
262 ),
263 ),
264 ],
265 ),
266 ),
267 ),
268 ),
269 );
270 }
271}