alert dialog

This commit is contained in:
Valentin CZERYBA 2022-09-06 22:02:37 +02:00
parent 595e63c4a7
commit be4804fad1

View File

@ -25,9 +25,14 @@ class LoginDemo extends StatefulWidget {
} }
class _LoginDemoState extends State<LoginDemo> { class _LoginDemoState extends State<LoginDemo> {
Future<void> _login() async { TextEditingController inputPseudo = TextEditingController();
TextEditingController inputPassword = TextEditingController();
Future<void> _login(BuildContext context) async {
var url = Uri.parse("http://localhost:8083/api/token"); var url = Uri.parse("http://localhost:8083/api/token");
String credentials = "peter93:toto"; var pseudo = inputPseudo.text;
var password = inputPassword.text;
if ((pseudo.isNotEmpty) && (password.isNotEmpty)) {
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);
var response = await http.get(url, headers: { var response = await http.get(url, headers: {
@ -35,8 +40,15 @@ class _LoginDemoState extends State<LoginDemo> {
}); });
if ((response.statusCode == 200) || (response.statusCode == 201)) { if ((response.statusCode == 200) || (response.statusCode == 201)) {
Navigator.push(context, Navigator.push(
MaterialPageRoute(builder: (_) => MyHomePage(title: 'Flutter Demo'))); context,
MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Flutter Demo')));
} else {
showErrorDialog(context, "Probleme d'authentification");
}
} else {
showErrorDialog(context, "Champ vide");
} }
} }
@ -66,6 +78,7 @@ class _LoginDemoState extends State<LoginDemo> {
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
padding: EdgeInsets.symmetric(horizontal: 15), padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField( child: TextField(
controller: inputPseudo,
decoration: InputDecoration( decoration: InputDecoration(
border: OutlineInputBorder(), border: OutlineInputBorder(),
labelText: 'Email', labelText: 'Email',
@ -77,6 +90,7 @@ class _LoginDemoState extends State<LoginDemo> {
left: 15.0, right: 15.0, top: 15, bottom: 0), left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15), //padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField( child: TextField(
controller: inputPassword,
obscureText: true, obscureText: true,
decoration: InputDecoration( decoration: InputDecoration(
border: OutlineInputBorder(), border: OutlineInputBorder(),
@ -100,7 +114,7 @@ class _LoginDemoState extends State<LoginDemo> {
color: Colors.blue, borderRadius: BorderRadius.circular(20)), color: Colors.blue, borderRadius: BorderRadius.circular(20)),
child: FlatButton( child: FlatButton(
onPressed: () { onPressed: () {
_login(); _login(context);
}, },
child: Text( child: Text(
'Login', 'Login',
@ -117,4 +131,31 @@ 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;
});
}
} }