· 6 years ago · Oct 07, 2019, 02:34 PM
1import 'package:apps/_routing/routes.dart';
2import 'package:apps/bloc/login_bloc.dart';
3import 'package:apps/model/login_model.dart';
4import 'package:apps/utils/colors.dart';
5import 'package:apps/utils/utils.dart';
6import 'package:apps/views/home.dart';
7import 'package:apps/views/pages/home_page.dart';
8import 'package:flutter/material.dart';
9import 'package:flutter/services.dart';
10import 'package:line_icons/line_icons.dart';
11import 'package:oktoast/oktoast.dart';
12import 'package:http/http.dart' as http;
13import 'package:shared_preferences/shared_preferences.dart';
14
15class Login extends StatefulWidget {
16 @override
17 _LoginState createState() => _LoginState();
18}
19
20class _LoginState extends State<Login> {
21 final bloc = LoginBloc();
22 LoginModel loginModel;
23
24//what this code is correct?
25 bool _apiCall = false;
26 // member response
27 String _response = '';
28 // login shared prefs
29 bool alreadyLogin = false;
30
31 TextEditingController controllerUsername = TextEditingController();
32 TextEditingController controllerPassword = TextEditingController();
33
34 @override
35 void initState() {
36 super.initState();
37 //dataLogin();
38 }
39
40 @override
41 void dispose() {
42 bloc.dispose();
43 super.dispose();
44 }
45
46//display progress bar when loading get data from rest api
47 Widget progressWidget() {
48 if (_apiCall) {
49 return AlertDialog(
50 content: new Column(
51 children: <Widget>[
52 CircularProgressIndicator(),
53 Text("Please wait")
54 ],
55 ),
56 );
57 }
58
59 }
60
61
62
63
64 @override
65 Widget build(BuildContext context) {
66 // Change Status Bar Color
67 SystemChrome.setSystemUIOverlayStyle(
68 SystemUiOverlayStyle(statusBarColor: primaryColor),
69 );
70
71 final pageTitle = Column(
72 crossAxisAlignment: CrossAxisAlignment.start,
73 children: <Widget>[
74 Text(
75 "Log In.",
76 style: TextStyle(
77 fontWeight: FontWeight.bold,
78 color: Colors.white,
79 fontSize: 45.0,
80 ),
81 ),
82 Text(
83 appName,
84 style: TextStyle(
85 color: Colors.white,
86 fontSize: 18.0,
87 fontWeight: FontWeight.w500,
88 ),
89 )
90 ],
91 );
92
93 final emailField = TextFormField(
94 decoration: InputDecoration(
95 labelText: 'Username (Email Address)',
96 labelStyle: TextStyle(color: Colors.white),
97 prefixIcon: Icon(
98 LineIcons.envelope,
99 color: Colors.white,
100 ),
101 enabledBorder: UnderlineInputBorder(
102 borderSide: BorderSide(color: Colors.white),
103 ),
104 focusedBorder: UnderlineInputBorder(
105 borderSide: BorderSide(color: Colors.white),
106 ),
107 ),
108 controller: controllerUsername,
109 keyboardType: TextInputType.emailAddress,
110 style: TextStyle(color: Colors.white),
111 cursorColor: Colors.white,
112 );
113
114 final passwordField = TextFormField(
115 decoration: InputDecoration(
116 labelText: 'Password',
117 labelStyle: TextStyle(color: Colors.white),
118 prefixIcon: Icon(
119 LineIcons.lock,
120 color: Colors.white,
121 ),
122 enabledBorder: UnderlineInputBorder(
123 borderSide: BorderSide(color: Colors.white),
124 ),
125 focusedBorder: UnderlineInputBorder(
126 borderSide: BorderSide(color: Colors.white),
127 ),
128 ),
129 controller: controllerPassword,
130 keyboardType: TextInputType.text,
131 style: TextStyle(color: Colors.white),
132 cursorColor: Colors.white,
133 obscureText: true,
134 );
135
136 final loginForm = Padding(
137 padding: EdgeInsets.only(top: 30.0),
138 child: Form(
139 //key: _formKey,
140 child: Column(
141 children: <Widget>[
142 emailField,
143 SizedBox(
144 height: 20.0,
145 ),
146 passwordField
147 ],
148 ),
149 ),
150 );
151
152 final loginBtn = Container(
153 margin: EdgeInsets.only(top: 30.0),
154 height: 60.0,
155 width: MediaQuery.of(context).size.width,
156 decoration: BoxDecoration(
157 borderRadius: BorderRadius.circular(7.0),
158 border: Border.all(color: Colors.white),
159 color: Colors.white,
160 ),
161 child: RaisedButton(
162 elevation: 5.0,
163 onPressed: () {
164 if (controllerUsername.text == "") {
165 showToast("Please input username",
166 position: ToastPosition.bottom);
167 } else if (controllerPassword.text == "") {
168 showToast("Please input password",
169 position: ToastPosition.bottom);
170 } else {
171 bloc.fetchLogin(controllerUsername.text, controllerPassword.text,
172 AppConfig.flagApps, AppConfig.ip, AppConfig.versi);
173 }
174 },
175
176 color: Colors.white,
177 shape: new RoundedRectangleBorder(
178 borderRadius: new BorderRadius.circular(7.0),
179 ),
180
181 child: Text(
182 'SIGN IN',
183 style: TextStyle(
184 fontWeight: FontWeight.w800,
185 fontSize: 20.0,
186 ),
187 ),
188 ));
189
190
191
192
193
194 void getDataLogin(
195 String usercode,
196 nip,
197 coy,
198 cab,
199 dept,
200 fullname,
201 accessLevel,
202 salesmanCode,
203 supervisorCode,
204 dealerCode,
205 loginNumber,
206 kdCustomer,
207 chasis) async {
208
209 SharedPreferences prefs = await SharedPreferences.getInstance();
210 prefs.setString("usercode", usercode);
211 prefs.setString("nip", nip);
212 prefs.setString("coy", coy);
213 prefs.setString("cab", cab);
214 prefs.setString("dept", dept);
215 prefs.setString("fullname", fullname);
216 prefs.setString("accessLevel", accessLevel);
217 prefs.setString("salesmanCode", salesmanCode);
218 prefs.setString("supervisorCode", supervisorCode);
219 prefs.setString("dealerCode", dealerCode);
220 prefs.setString("loginNumber", loginNumber);
221 prefs.setString("kdCustomer", kdCustomer);
222 prefs.setString("chasis", chasis);
223
224
225 //if I activate this code, the loop will never stop
226 //final pref = await SharedPreferences.getInstance();
227 //setState(() {
228 //alreadyLogin = true;
229 //pref.setBool('login', alreadyLogin);
230 //});
231 //print(pref);
232
233 //Navigator.push(
234 //context, MaterialPageRoute(builder: (context) => Home()));
235 }
236
237
238
239 return StreamBuilder(
240 stream: bloc.login,
241 builder: (context, snapshot) {
242
243 if (snapshot.hasData) {
244 loginModel = snapshot.data;
245
246//what this code is correct?
247 //setState(() {
248 //_apiCall = false;
249 //_response = loginModel.loginFalse[0].message;
250 //});
251
252 if (loginModel.login == null) {
253
254 showToast(loginModel.loginFalse[0].message,
255 position: ToastPosition.bottom);
256
257 //_apiCall = false;
258 //_response = loginModel.loginFalse[0].message;
259
260 } else {
261
262 showToast("Welcome "+loginModel.login[0].fullname,
263 position: ToastPosition.bottom);
264
265 var usercode = loginModel.login[0].usercode;
266 var nip = loginModel.login[0].nip;
267 var coy = loginModel.login[0].company_code;
268 var cab = loginModel.login[0].branch_code;
269 var dept = loginModel.login[0].department_code;
270 var fullname = loginModel.login[0].fullname;
271 var accessLevel = loginModel.login[0].access_level;
272 var salesmanCode = loginModel.login[0].salesman_code;
273 var supervisorCode = loginModel.login[0].supervisor_code;
274 var dealerCode = loginModel.login[0].dealer_code;
275 var loginNumber = loginModel.login[0].login_number;
276 var kdCustomer = loginModel.login[0].kd_customer;
277 var chasis = loginModel.login[0].chasis;
278
279 getDataLogin(
280 usercode,
281 nip,
282 coy,
283 cab,
284 dept,
285 fullname,
286 accessLevel,
287 salesmanCode,
288 supervisorCode,
289 dealerCode,
290 loginNumber,
291 kdCustomer,
292 chasis);
293
294 }
295 } else if (snapshot.hasError) {
296 print(snapshot.error.toString());
297
298 //_apiCall = false;
299 //_response = snapshot.hasError.toString();
300 }
301
302 return OKToast(
303 child: Scaffold(
304 body: SingleChildScrollView(
305 child: Container(
306 padding: EdgeInsets.only(top: 150.0, left: 30.0, right: 30.0),
307 decoration: BoxDecoration(gradient: primaryGradient),
308 height: MediaQuery.of(context).size.height,
309 width: MediaQuery.of(context).size.width,
310 child: Column(
311 crossAxisAlignment: CrossAxisAlignment.start,
312 children: <Widget>[
313 pageTitle,
314 loginForm,
315 loginBtn,
316 ],
317 ),
318 ),
319 ),
320 ));
321 });
322 }
323}