Separate class and try catch

This commit is contained in:
Valentin CZERYBA 2022-09-13 00:11:43 +02:00
parent 29338f807b
commit e7b4744016
2 changed files with 87 additions and 78 deletions

View 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;
});
}
}

View File

@ -4,6 +4,7 @@ import 'MyHomePage.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'classes/alert.dart';
void main() { void main() {
runApp(MyApp()); runApp(MyApp());
@ -24,7 +25,7 @@ class LoginDemo extends StatefulWidget {
_LoginDemoState createState() => _LoginDemoState(); _LoginDemoState createState() => _LoginDemoState();
} }
class _LoginDemoState extends State<LoginDemo> { class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
TextEditingController inputPseudo = TextEditingController(); TextEditingController inputPseudo = TextEditingController();
TextEditingController inputPassword = TextEditingController(); TextEditingController inputPassword = TextEditingController();
Future<void> _login(BuildContext context) async { Future<void> _login(BuildContext context) async {
@ -32,6 +33,7 @@ class _LoginDemoState extends State<LoginDemo> {
var pseudo = inputPseudo.text; var pseudo = inputPseudo.text;
var password = inputPassword.text; var password = inputPassword.text;
if ((pseudo.isNotEmpty) && (password.isNotEmpty)) { if ((pseudo.isNotEmpty) && (password.isNotEmpty)) {
try {
String credentials = "${pseudo}:${password}"; String credentials = "${pseudo}:${password}";
Codec<String, String> stringToBase64 = utf8.fuse(base64); Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials); String encoded = stringToBase64.encode(credentials);
@ -85,6 +87,9 @@ class _LoginDemoState extends State<LoginDemo> {
} }
showErrorDialog(context, text); showErrorDialog(context, text);
} }
} catch (e) {
showErrorDialog(context, "${e}");
}
} else { } else {
showErrorDialog(context, "Champ vide"); 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;
});
}
} }