Separate class and try catch
This commit is contained in:
parent
29338f807b
commit
e7b4744016
31
covas_mobile/lib/classes/alert.dart
Normal file
31
covas_mobile/lib/classes/alert.dart
Normal file
@ -0,0 +1,31 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../main.dart';
|
||||
|
||||
mixin ShowErrorDialog on State<LoginDemo> {
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
@ -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<LoginDemo> {
|
||||
class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
|
||||
TextEditingController inputPseudo = TextEditingController();
|
||||
TextEditingController inputPassword = TextEditingController();
|
||||
Future<void> _login(BuildContext context) async {
|
||||
@ -32,58 +33,62 @@ class _LoginDemoState extends State<LoginDemo> {
|
||||
var pseudo = inputPseudo.text;
|
||||
var password = inputPassword.text;
|
||||
if ((pseudo.isNotEmpty) && (password.isNotEmpty)) {
|
||||
String credentials = "${pseudo}:${password}";
|
||||
Codec<String, String> 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<String, String> 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<LoginDemo> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user