· 4 years ago · Jul 13, 2021, 08:36 PM
1import 'package:flutter/material.dart';
2import 'dart:convert';
3// ignore: import_of_legacy_library_into_null_safe
4import 'package:http/http.dart' as http;
5import 'dart:async';
6import 'package:flutter/services.dart';
7import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
8
9void main() => runApp(MaterialApp(
10 home: MyApp(),
11 ));
12
13class MyApp extends StatelessWidget {
14 @override
15 Widget build(BuildContext context) {
16 return MaterialApp(
17 home: Scaffold(
18 appBar: AppBar(title: Text('User Login Form')),
19 body: Center(child: LoginUser())));
20 }
21}
22
23class LoginUser extends StatefulWidget {
24 LoginUserState createState() => LoginUserState();
25}
26
27class LoginUserState extends State {
28 // For CircularProgressIndicator.
29 bool visible = false;
30
31 // Getting value from TextField widget.
32 final gebruikersnaamController = TextEditingController();
33 final passwordController = TextEditingController();
34
35 Future userLogin() async {
36 // Showing CircularProgressIndicator.
37 setState(() {
38 visible = true;
39 });
40
41 // Getting value from Controller
42 String gebruikersnaam = gebruikersnaamController.text;
43 String password = passwordController.text;
44
45 // SERVER LOGIN API URL
46 var url = 'http://42312.hbcdeveloper.nl/CLIMA/api/login.php';
47
48 // Store all data with Param Name.
49 var data = {'gebruikersnaam': gebruikersnaam, 'wachtwoord': password};
50
51 // Starting Web API Call.
52 var response = await http.post(Uri.parse(url), body: data);
53 print(response.body);
54
55 // Getting Server response into variable.
56 var message = jsonDecode(response.body);
57
58 // If the Response Message is Matched.
59 if (!message['error']) {
60 // Hiding the CircularProgressIndicator.
61 setState(() {
62 visible = false;
63 });
64
65 // Navigate to Profile Screen & Sending Email to Next Screen.
66 Navigator.push(
67 context,
68 MaterialPageRoute(
69 builder: (context) =>
70 ProfileScreen(gebruikersnaam: message['gebruikersnaam'])));
71 } else {
72 // If Email or Password did not Matched.
73 // Hiding the CircularProgressIndicator.
74 setState(() {
75 visible = false;
76 });
77
78 // Showing Alert Dialog with Response JSON Message.
79 showDialog(
80 context: context,
81 builder: (BuildContext context) {
82 return AlertDialog(
83 title: new Text(message['gebruikersnaam'] ?? message['message']),
84 actions: <Widget>[
85 TextButton(
86 child: new Text("OK"),
87 onPressed: () {
88 Navigator.of(context).pop();
89 },
90 ),
91 ],
92 );
93 },
94 );
95 }
96 }
97
98 @override
99 Widget build(BuildContext context) {
100 return Scaffold(
101 body: SingleChildScrollView(
102 child: Center(
103 child: Column(
104 children: <Widget>[
105 Padding(
106 padding: const EdgeInsets.all(50.0),
107 child: Text('Inloggen', style: TextStyle(fontSize: 21))),
108 Divider(),
109 Container(
110 width: 280,
111 padding: EdgeInsets.all(10.0),
112 child: TextField(
113 controller: gebruikersnaamController,
114 autocorrect: true,
115 decoration: InputDecoration(hintText: 'Gebruikersnaam'),
116 )),
117 Container(
118 width: 280,
119 padding: EdgeInsets.all(10.0),
120 child: TextField(
121 controller: passwordController,
122 autocorrect: true,
123 obscureText: true,
124 decoration: InputDecoration(hintText: 'Wachtwoord'),
125 )),
126 Container(
127 margin: EdgeInsets.only(top: 30),
128 child: ConstrainedBox(
129 constraints: BoxConstraints.tightFor(width: 260, height: 50),
130 child: ElevatedButton(
131 onPressed: userLogin,
132 child: Text('Inloggen',
133 style: TextStyle(
134 fontWeight: FontWeight.bold,
135 fontSize: 20,
136 ))),
137 )),
138 Visibility(
139 visible: visible,
140 child: Container(
141 margin: EdgeInsets.only(bottom: 30),
142 child: CircularProgressIndicator())),
143 ],
144 ),
145 )));
146 }
147}
148
149class ProfileScreen extends StatelessWidget {
150// Creating String Var to Hold sent Email.
151 final String gebruikersnaam;
152
153// Receiving Email using Constructor.
154 ProfileScreen({Key? key, required this.gebruikersnaam}) : super(key: key);
155
156// User Logout Function.
157 logout(BuildContext context) {
158 Navigator.pop(context);
159 }
160
161 Future<void> scanQR(BuildContext context) async {
162 String barcodeScanRes;
163 // Platform messages may fail, so we use a try/catch PlatformException.
164 try {
165 barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
166 '#ff6666', 'Annuleren', true, ScanMode.QR);
167 print("QR scanned: $barcodeScanRes");
168
169 // SERVER LOGIN API URL
170 var url = 'http://42312.hbcdeveloper.nl/CLIMA/api/client.php';
171
172 // Starting Web API Call.
173 var response =
174 await http.post(Uri.parse(url), body: {'id': barcodeScanRes});
175
176 // Getting Server response into variable.
177 var jsonData = response.body;
178
179 Navigator.push(
180 context,
181 MaterialPageRoute(
182 builder: (context) => ClientPagina(data: jsonData)));
183 } on PlatformException {
184 barcodeScanRes = 'Failed to get platform version.';
185 }
186 }
187
188 @override
189 Widget build(BuildContext context) {
190 return Scaffold(
191 appBar: AppBar(
192 title: Text('CLIMA'),
193 ),
194 body: Center(
195 child: Column(
196 mainAxisAlignment: MainAxisAlignment.center,
197 crossAxisAlignment: CrossAxisAlignment.center,
198 children: <Widget>[
199 Container(
200 margin: EdgeInsets.only(bottom: 100),
201 child: Text('Welkom, ' + gebruikersnaam,
202 style: TextStyle(
203 fontWeight: FontWeight.bold,
204 fontSize: 40,
205 )),
206 ),
207 ConstrainedBox(
208 constraints: BoxConstraints.tightFor(width: 150, height: 75),
209 child: ElevatedButton(
210 onPressed: () {
211 scanQR(context);
212 },
213 child: Text('Scan QR',
214 style: TextStyle(
215 fontWeight: FontWeight.bold,
216 fontSize: 20,
217 ))),
218 )
219 ],
220 ),
221 ),
222 );
223 }
224}
225
226class ClientPagina extends StatelessWidget {
227 final String data;
228 const ClientPagina({required this.data});
229
230 @override
231 Widget build(BuildContext context) {
232 var client = jsonDecode(data);
233
234 var name;
235
236 if (client['tussenvoegsel'] != null) {
237 name = client['voornaam'] +
238 ' ' +
239 client['tussenvoegsel'] +
240 ' ' +
241 client['achternaam'];
242 } else {
243 name = client['voornaam'] + ' ' + client['achternaam'];
244 }
245
246 // Getting value from TextField widget.
247 final hartslagController = TextEditingController();
248 final bloeddrukController = TextEditingController();
249
250 return Scaffold(
251 appBar: AppBar(
252 title: Text(name),
253 ),
254 body: Column(
255 children: <Widget>[
256 Divider(),
257 Column(
258 mainAxisAlignment: MainAxisAlignment.center,
259 crossAxisAlignment: CrossAxisAlignment.start,
260 children: <Widget>[
261 Text('Cliënt ID: ' + client['id']),
262 Text('Naam: ' + name),
263 Text('Adres: ' + client['adres']),
264 Text('Postcode: ' + client['postcode']),
265 Text('Woonplaats: ' + client['woonplaats']),
266 Text('Telefoon: ' + client['telefoon']),
267 Text('E-mailadres: ' + client['emailadres']),
268 Text('Geslacht: ' + client['geslacht']),
269 Text('BSN: ' + client['bsn']),
270 Text('Geboortedatum: ' + client['geboortedatum']),
271 Text('Lengte: ' + client['lengte'] + ' cm'),
272 Text('Gewicht: ' + client['gewicht'] + ' kg'),
273 Text('Bloedgroep: ' + client['bloedgroep']),
274 Text('Huisarts: ' + client['huisarts']),
275 Text('Verzekeringmaatschappij: ' +
276 client['verzekeringmaatschappij']),
277 ],
278 ),
279 Center(
280 child: Column(
281 children: <Widget>[
282 Divider(),
283 Text('Vul de hartslag en bloeddruk hieronder in.',
284 style: TextStyle(
285 fontWeight: FontWeight.bold,
286 )),
287 Container(
288 width: 280,
289 padding: EdgeInsets.all(10.0),
290 child: TextField(
291 controller: hartslagController,
292 keyboardType: TextInputType.number,
293 inputFormatters: [
294 FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
295 ],
296 autocorrect: true,
297 decoration: InputDecoration(hintText: 'Hartslag'),
298 )),
299 Container(
300 width: 280,
301 padding: EdgeInsets.all(10.0),
302 child: TextField(
303 controller: bloeddrukController,
304 keyboardType: TextInputType.number,
305 inputFormatters: [
306 FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
307 ],
308 autocorrect: true,
309 decoration: InputDecoration(hintText: 'Bloeddruk'),
310 )),
311 Container(
312 margin: EdgeInsets.only(top: 30),
313 child: ConstrainedBox(
314 constraints:
315 BoxConstraints.tightFor(width: 260, height: 50),
316 child: ElevatedButton(
317 onPressed: () {
318 // SERVER LOGIN API URL
319 var url =
320 'http://42312.hbcdeveloper.nl/CLIMA/api/hartslag_bloeddruk.php';
321
322 // Starting Web API Call.
323 http.post(Uri.parse(url), body: {
324 'client_id': client['id'],
325 'hartslag': hartslagController.text,
326 'bloeddruk': bloeddrukController.text
327 });
328
329 hartslagController.clear();
330 bloeddrukController.clear();
331
332 showDialog(
333 context: context,
334 builder: (BuildContext context) {
335 return AlertDialog(
336 title: new Text(
337 "Hartslag en bloeddruk ingevoerd!"),
338 actions: <Widget>[
339 TextButton(
340 child: new Text("OK"),
341 onPressed: () {
342 Navigator.of(context).pop();
343 },
344 ),
345 ],
346 );
347 },
348 );
349 },
350 child: Text('Invoeren',
351 style: TextStyle(
352 fontWeight: FontWeight.bold,
353 fontSize: 20,
354 ))),
355 )),
356 ],
357 ))
358 ],
359 ));
360 }
361}
362