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 '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;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user