From e7b47440169e660e3d710d8eb0e5d68b51e6bf05 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 13 Sep 2022 00:11:43 +0200 Subject: [PATCH] Separate class and try catch --- covas_mobile/lib/classes/alert.dart | 31 +++++++ covas_mobile/lib/main.dart | 134 ++++++++++++---------------- 2 files changed, 87 insertions(+), 78 deletions(-) create mode 100644 covas_mobile/lib/classes/alert.dart diff --git a/covas_mobile/lib/classes/alert.dart b/covas_mobile/lib/classes/alert.dart new file mode 100644 index 0000000..0e30411 --- /dev/null +++ b/covas_mobile/lib/classes/alert.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; +import '../main.dart'; + +mixin ShowErrorDialog on State { + void showErrorDialog(BuildContext context, String text) { + // Create AlertDialog + AlertDialog dialog = AlertDialog( + title: Text("Error"), + content: Text(text), + actions: [ + ElevatedButton( + child: Text("OK"), + style: ElevatedButton.styleFrom( + primary: Colors.red, + padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20), + textStyle: + TextStyle(fontSize: 15, fontWeight: FontWeight.normal)), + onPressed: () { + Navigator.of(context).pop("Yes, Of course!"); // Return value + }), + ], + ); + + // Call showDialog function to show dialog. + Future futureValue = showDialog( + context: context, + builder: (BuildContext context) { + return dialog; + }); + } +} diff --git a/covas_mobile/lib/main.dart b/covas_mobile/lib/main.dart index d5f8833..8eca0a7 100644 --- a/covas_mobile/lib/main.dart +++ b/covas_mobile/lib/main.dart @@ -4,6 +4,7 @@ import 'MyHomePage.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import 'dart:io'; +import 'classes/alert.dart'; void main() { runApp(MyApp()); @@ -24,7 +25,7 @@ class LoginDemo extends StatefulWidget { _LoginDemoState createState() => _LoginDemoState(); } -class _LoginDemoState extends State { +class _LoginDemoState extends State with ShowErrorDialog { TextEditingController inputPseudo = TextEditingController(); TextEditingController inputPassword = TextEditingController(); Future _login(BuildContext context) async { @@ -32,58 +33,62 @@ class _LoginDemoState extends State { var pseudo = inputPseudo.text; var password = inputPassword.text; if ((pseudo.isNotEmpty) && (password.isNotEmpty)) { - String credentials = "${pseudo}:${password}"; - Codec stringToBase64 = utf8.fuse(base64); - String encoded = stringToBase64.encode(credentials); - var response = await http.get(url, headers: { - HttpHeaders.authorizationHeader: 'Basic $encoded', - }); + try { + String credentials = "${pseudo}:${password}"; + Codec stringToBase64 = utf8.fuse(base64); + String encoded = stringToBase64.encode(credentials); + var response = await http.get(url, headers: { + HttpHeaders.authorizationHeader: 'Basic $encoded', + }); - if ((response.statusCode == 200) || (response.statusCode == 201)) { - Navigator.push( - context, - MaterialPageRoute( - builder: (_) => MyHomePage(title: 'Flutter Demo'))); - } else { - var text = ""; - switch (response.statusCode) { - case 400: - { - text = "Requête mal construite"; - } - break; - case 406: - { - text = "Mot de passe incorrect"; - } - break; - case 404: - { - text = "Utilisateur inconnu"; - } - break; - case 403: - { - text = "Utilisateur desactive"; - } - break; - case 410: - { - text = "Token invalide"; - } - break; - case 500: - { - text = "Probleme interne du serveur"; - } - break; - default: - { - text = "Probleme d'authentification inconnu"; - } - break; + if ((response.statusCode == 200) || (response.statusCode == 201)) { + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => MyHomePage(title: 'Flutter Demo'))); + } else { + var text = ""; + switch (response.statusCode) { + case 400: + { + text = "Requête mal construite"; + } + break; + case 406: + { + text = "Mot de passe incorrect"; + } + break; + case 404: + { + text = "Utilisateur inconnu"; + } + break; + case 403: + { + text = "Utilisateur desactive"; + } + break; + case 410: + { + text = "Token invalide"; + } + break; + case 500: + { + text = "Probleme interne du serveur"; + } + break; + default: + { + text = "Probleme d'authentification inconnu"; + } + break; + } + showErrorDialog(context, text); } - showErrorDialog(context, text); + } catch (e) { + showErrorDialog(context, "${e}"); } } else { showErrorDialog(context, "Champ vide"); @@ -169,31 +174,4 @@ class _LoginDemoState extends State { ), ); } - - showErrorDialog(BuildContext context, String text) { - // Create AlertDialog - AlertDialog dialog = AlertDialog( - title: Text("Error"), - content: Text(text), - actions: [ - ElevatedButton( - child: Text("OK"), - style: ElevatedButton.styleFrom( - primary: Colors.red, - padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20), - textStyle: - TextStyle(fontSize: 15, fontWeight: FontWeight.normal)), - onPressed: () { - Navigator.of(context).pop("Yes, Of course!"); // Return value - }), - ], - ); - - // Call showDialog function to show dialog. - Future futureValue = showDialog( - context: context, - builder: (BuildContext context) { - return dialog; - }); - } }