200 lines
5.8 KiB
Dart
Raw Normal View History

2022-08-27 12:14:34 +02:00
import 'package:flutter/material.dart';
2022-09-03 21:17:47 +02:00
import 'MyHomePage.dart';
2022-08-27 12:14:34 +02:00
2022-09-04 20:09:09 +02:00
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';
2022-08-27 12:14:34 +02:00
void main() {
2022-09-03 21:17:47 +02:00
runApp(MyApp());
2022-08-27 12:14:34 +02:00
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
2022-09-03 21:17:47 +02:00
debugShowCheckedModeBanner: false,
home: LoginDemo(),
2022-08-27 12:14:34 +02:00
);
}
}
2022-09-03 21:17:47 +02:00
class LoginDemo extends StatefulWidget {
2022-08-27 12:14:34 +02:00
@override
2022-09-03 21:17:47 +02:00
_LoginDemoState createState() => _LoginDemoState();
2022-08-27 12:14:34 +02:00
}
2022-09-03 21:17:47 +02:00
class _LoginDemoState extends State<LoginDemo> {
2022-09-06 22:02:37 +02:00
TextEditingController inputPseudo = TextEditingController();
TextEditingController inputPassword = TextEditingController();
Future<void> _login(BuildContext context) async {
2022-09-04 20:09:09 +02:00
var url = Uri.parse("http://localhost:8083/api/token");
2022-09-06 22:02:37 +02:00
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',
});
2022-09-04 20:09:09 +02:00
2022-09-06 22:02:37 +02:00
if ((response.statusCode == 200) || (response.statusCode == 201)) {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Flutter Demo')));
} else {
2022-09-07 23:15:54 +02:00
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);
2022-09-06 22:02:37 +02:00
}
} else {
showErrorDialog(context, "Champ vide");
2022-09-04 20:09:09 +02:00
}
}
2022-08-27 12:14:34 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
2022-09-03 21:17:47 +02:00
backgroundColor: Colors.white,
2022-08-27 12:14:34 +02:00
appBar: AppBar(
2022-09-03 21:17:47 +02:00
title: Text("Login Page"),
2022-08-27 12:14:34 +02:00
),
2022-09-03 21:17:47 +02:00
body: SingleChildScrollView(
2022-08-27 12:14:34 +02:00
child: Column(
children: <Widget>[
2022-09-03 21:17:47 +02:00
Padding(
padding: const EdgeInsets.only(top: 60.0),
child: Center(
child: Container(
width: 200,
height: 150,
/*decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(50.0)),*/
child: Image.asset('asset/images/flutter-logo.png')),
),
),
Padding(
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
2022-09-06 22:02:37 +02:00
controller: inputPseudo,
2022-09-03 21:17:47 +02:00
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Email',
hintText: 'Enter valid email id as abc@gmail.com'),
2022-09-01 22:32:35 +02:00
),
),
2022-09-03 21:17:47 +02:00
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
2022-09-06 22:02:37 +02:00
controller: inputPassword,
2022-09-03 21:17:47 +02:00
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
hintText: 'Enter secure password'),
),
),
FlatButton(
onPressed: () {
//TODO FORGOT PASSWORD SCREEN GOES HERE
},
child: Text(
'Forgot Password',
style: TextStyle(color: Colors.blue, fontSize: 15),
),
),
Container(
height: 50,
width: 250,
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(20)),
child: FlatButton(
onPressed: () {
2022-09-06 22:02:37 +02:00
_login(context);
2022-09-03 21:17:47 +02:00
},
child: Text(
'Login',
style: TextStyle(color: Colors.white, fontSize: 25),
),
),
2022-08-27 12:14:34 +02:00
),
2022-09-03 21:17:47 +02:00
SizedBox(
height: 130,
2022-08-27 12:14:34 +02:00
),
2022-09-03 21:17:47 +02:00
Text('New User? Create Account')
2022-08-27 12:14:34 +02:00
],
),
),
);
}
2022-09-06 22:02:37 +02:00
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;
});
}
2022-08-27 12:14:34 +02:00
}