227 lines
7.8 KiB
Dart
227 lines
7.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/Components/loading_common.dart';
|
|
import 'package:manager_app/Components/message_notification.dart';
|
|
import 'package:manager_app/Components/rounded_button.dart';
|
|
import 'package:manager_app/Components/rounded_input_field.dart';
|
|
import 'package:manager_app/Components/rounded_password_field.dart';
|
|
import 'package:manager_app/Helpers/FileHelper.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/Models/session.dart';
|
|
import 'package:manager_app/Screens/Main/main_screen.dart';
|
|
import 'package:manager_app/app_context.dart';
|
|
import 'package:manager_app/client.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:managerapi/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
final Session session;
|
|
LoginScreen({Key key, this.session}) : super(key: key);
|
|
|
|
@override
|
|
_LoginScreenState createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
String email; // DEV "test@email.be"
|
|
String password; // DEV = "kljqsdkljqsd"
|
|
String host; // DEV = "http://192.168.31.96"
|
|
Client clientAPI;
|
|
bool isLoading = false;
|
|
bool isRememberMe = false;
|
|
|
|
void authenticateTRY(dynamic appContext) async {
|
|
//print("try auth.. ");
|
|
|
|
/*this.host = "http://localhost:5000";
|
|
this.email = "test@email.be";
|
|
this.password = "kljqsdkljqsd";*/
|
|
|
|
clientAPI = Client(this.host);
|
|
|
|
if (this.email != null && this.password != null && this.host != null) {
|
|
|
|
// if () {} // Add if token exist and not null + not expired
|
|
try {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
/*print(email);
|
|
print(password);*/
|
|
|
|
LoginDTO loginDTO = new LoginDTO(email: email, password: password);
|
|
TokenDTO token = await clientAPI.authenticationApi.authenticationAuthenticateWithJson(loginDTO);
|
|
setAccessToken(token.accessToken);
|
|
|
|
if (isRememberMe) {
|
|
Session updatedSession = new Session(rememberMe: isRememberMe, host: host, email: email, password: password);
|
|
// update JSON FILE
|
|
FileHelper().writeSession(updatedSession);
|
|
}
|
|
|
|
showNotification(kSuccess, kWhite, 'Connexion réussie', context, null);
|
|
|
|
ManagerAppContext managerAppContext = appContext.getContext();
|
|
// Set the appContext
|
|
if (managerAppContext == null) {
|
|
managerAppContext = new ManagerAppContext();
|
|
}
|
|
|
|
// store user info locally
|
|
managerAppContext.email = email;
|
|
managerAppContext.token = token;
|
|
managerAppContext.clientAPI = clientAPI;
|
|
//print(managerAppContext);
|
|
|
|
appContext.setContext(managerAppContext);
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
|
|
Navigator.pushAndRemoveUntil(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return MainScreen();
|
|
},
|
|
),
|
|
(Route<dynamic> route) => false // For pushAndRemoveUntil
|
|
);
|
|
}
|
|
catch (e) {
|
|
//print("error auth");
|
|
//print(e);
|
|
showNotification(Colors.orange, kWhite, 'Un problème est survenu lors de la connexion', context, null);
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void setAccessToken(String accessToken) {
|
|
clientAPI.apiApi.authentications.forEach((key, auth) {
|
|
if (auth is OAuth) {
|
|
auth.accessToken = accessToken;
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
this.isRememberMe = widget.session.rememberMe;
|
|
this.host = "http://localhost:5000"; //widget.session.host; // MDLF "http://192.168.1.19:8089"
|
|
this.email = "test@email.be"; //widget.session.email;
|
|
this.password = "kljqsdkljqsd"; //widget.session.password;
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
return Scaffold(
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
child: Container(
|
|
height: size.height *0.65,
|
|
width: size.width *0.5,
|
|
decoration: BoxDecoration(
|
|
color: kWhite,
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: kWhite.withOpacity(0.3),
|
|
spreadRadius: 0.5,
|
|
blurRadius: 0.5,
|
|
offset: Offset(0, 1.5), // changes position of shadow
|
|
),
|
|
],
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(left: 60.0, right: 60.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 20),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Icon(Icons.museum_outlined, color: kPrimaryColor, size: size.height*0.08),
|
|
),
|
|
Text("MyMuseum", style: TextStyle(color: kPrimaryColor, fontSize: 45, fontFamily: "Helvetica")),
|
|
],
|
|
),
|
|
),
|
|
RoundedInputField(
|
|
hintText: "Host",
|
|
onChanged: (value) {
|
|
host = value;
|
|
},
|
|
initialValue: host,
|
|
icon: Icons.home
|
|
),
|
|
RoundedInputField(
|
|
hintText: "Email",
|
|
onChanged: (value) {
|
|
email = value;
|
|
},
|
|
icon: Icons.person,
|
|
initialValue: email,
|
|
isEmail: true,
|
|
),
|
|
RoundedPasswordField(
|
|
initialValue: password,
|
|
onChanged: (value) {
|
|
password = value;
|
|
},
|
|
),
|
|
if(!kIsWeb) Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Container(
|
|
child: Checkbox(
|
|
checkColor: kTextLightColor,
|
|
activeColor: kPrimaryColor,
|
|
value: this.isRememberMe,
|
|
onChanged: (bool value) {
|
|
setState(() {
|
|
this.isRememberMe = value;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
Text("Se souvenir de moi", style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(height: size.height * 0.05),
|
|
!isLoading ? RoundedButton(
|
|
text: "LOGIN",
|
|
fontSize: 25,
|
|
press: () {
|
|
authenticateTRY(appContext);
|
|
},
|
|
): Container(
|
|
height: size.height * 0.1,
|
|
child: LoadingCommon()
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
);
|
|
}
|
|
} |