Le meta viewport manquant n'expliquait pas tout : le clavier s'ouvrait puis se refermait apres une demi-seconde. C'est le focus qui etait perdu, pas la saisie qui n'arrivait pas. Sur Flutter web, un AutofillGroup se materialise par un <form> DOM qui porte les inputs caches par lesquels passe la saisie. L'ouverture du clavier redimensionne la fenetre, l'ecran se reconstruit, le groupe d'autofill est renegocie et le <form> recree : l'input focus disparait avec lui. Sur desktop rien ne redimensionne au clic, d'ou un bug invisible hors mobile. L'AutofillGroup est retire — les autofillHints portes par chaque champ suffisent au navigateur pour proposer le remplissage, le groupe ne servait qu'a finishAutofillContext. Trois protections completent la correction : - les deux champs passent par un TextEditingController detenu par le State, donc la saisie survit a une reconstruction, la ou initialValue repartait de zero ; - ils portent une ValueKey stable, pour etre reapparies plutot que recrees si la structure de leurs freres bouge ; - le Scaffold ne se redimensionne plus a l'ouverture du clavier ; le contenu est deja dans un SingleChildScrollView, rien ne reste masque. Au passage, le pre-remplissage de developpement (localhost) etait ecrit dans build() a chaque reconstruction : il passe dans initState, ou il a du sens. Le routeur, lui, etait fabrique dans le builder d'un FutureBuilder et l'appel a getInstanceInfo partait de l'arbre passe a runApp. Chaque passage du builder rendait un GoRouter neuf, donc un arbre neuf et un historique perdu. Les deux sont desormais resolus une fois, avant runApp — 45 lignes de moins. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
422 lines
16 KiB
Dart
422 lines
16 KiB
Dart
import 'dart:html';
|
|
|
|
import 'package:auto_size_text/auto_size_text.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_svg/svg.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:manager_app/Components/common_loader.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/l10n/app_localizations.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/app_context.dart';
|
|
import 'package:manager_app/client.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
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 = kApiBaseUrl; // "https://api.myinfomate.be" // "https://api.mymuseum.be" // DEV = "http://192.168.31.96" // http://localhost:5000 // https://api.mymuseum.be // myCore http://192.168.31.140:8089
|
|
Client? clientAPI;
|
|
bool isLoading = false;
|
|
bool isRememberMe = false;
|
|
String pageTitle = "MyInfoMate";
|
|
String? token;
|
|
String? instanceId;
|
|
String? pinCode;
|
|
Storage localStorage = window.localStorage;
|
|
late final Future<String> appVersion = getAppVersion();
|
|
|
|
// Champs pilotes par un controller, et non par initialValue : l'ouverture du
|
|
// clavier mobile redimensionne la fenetre, ce qui reconstruit l'ecran. Avec
|
|
// initialValue, le TextFormField repartait de zero a chaque reconstruction.
|
|
final emailController = TextEditingController();
|
|
final passwordController = TextEditingController();
|
|
|
|
void authenticateTRY(AppContext appContext, bool fromClick) async {
|
|
clientAPI = Client(this.host!);
|
|
|
|
if (this.password.isNotEmpty || this.token != null) {
|
|
try {
|
|
if (fromClick) {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
}
|
|
|
|
var accessToken = this.token;
|
|
var instanceId = this.instanceId;
|
|
var pinCode = this.pinCode;
|
|
UserRole? userRole;
|
|
|
|
if (accessToken == null) {
|
|
LoginDTO loginDTO = new LoginDTO(email: email, password: password);
|
|
|
|
TokenDTO? token = await clientAPI!.authenticationApi!
|
|
.authenticationAuthenticateWithJson(loginDTO);
|
|
|
|
if (token != null) {
|
|
accessToken = token.accessToken!;
|
|
instanceId = token.instanceId!;
|
|
pinCode = token.pinCode;
|
|
userRole = token.role;
|
|
|
|
showNotification(
|
|
kSuccess, kWhite, AppLocalizations.of(context)!.loginSuccess, context, null);
|
|
|
|
if (isRememberMe) {
|
|
if (!localStorage.containsKey("remember")) {
|
|
localStorage.addEntries({"remember": "true"}.entries);
|
|
}
|
|
|
|
if (!localStorage.containsKey("email") &&
|
|
!localStorage.containsKey("token")) {
|
|
localStorage.addEntries({"email": email}.entries);
|
|
localStorage.addEntries({"token": token.accessToken!}.entries);
|
|
localStorage.addEntries({"instanceId": token.instanceId!}.entries);
|
|
if (token.pinCode != null) {
|
|
localStorage.addEntries({"pinCode": token.pinCode!.toString()}.entries);
|
|
}
|
|
}
|
|
} else {
|
|
localStorage.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (accessToken != null) {
|
|
ManagerAppContext? managerAppContext = appContext.getContext();
|
|
if (managerAppContext == null) {
|
|
managerAppContext = new ManagerAppContext();
|
|
}
|
|
|
|
managerAppContext.email = email;
|
|
managerAppContext.host = host;
|
|
managerAppContext.instanceId = instanceId;
|
|
managerAppContext.pinCode = pinCode;
|
|
managerAppContext.accessToken = accessToken;
|
|
managerAppContext.role = userRole;
|
|
managerAppContext.clientAPI = clientAPI;
|
|
setAccessToken(accessToken);
|
|
|
|
InstanceDTO? instance = await managerAppContext.clientAPI!.instanceApi!
|
|
.instanceGetDetail(managerAppContext.instanceId!);
|
|
managerAppContext.instanceDTO = instance;
|
|
|
|
FileHelper().writeSession(new Session(
|
|
rememberMe: true,
|
|
instanceId: instanceId,
|
|
host: host,
|
|
token: accessToken,
|
|
password: password,
|
|
email: email,
|
|
role: userRole?.value));
|
|
appContext.setContext(managerAppContext);
|
|
|
|
if (instance!.isMobile!) {
|
|
context.go('/main/mobile');
|
|
} else {
|
|
if (instance.isTablet!) {
|
|
context.go('/main/tablet');
|
|
} else {
|
|
if (instance.isWeb!) {
|
|
context.go('/main/web');
|
|
} else {
|
|
if (instance.isVR!) {
|
|
context.go('/main/vr');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (fromClick) {
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
} else {
|
|
showNotification(Colors.orange, kWhite, AppLocalizations.of(context)!.loginError, context, null);
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
print("error auth");
|
|
print(e);
|
|
if (fromClick) {
|
|
showNotification(Colors.orange, kWhite, AppLocalizations.of(context)!.loginError, context, null);
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void setAccessToken(String accessToken) {
|
|
clientAPI!.apiApi!.addDefaultHeader('authorization', 'Bearer ' + accessToken);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
this.host = kApiBaseUrl;
|
|
|
|
if (localStorage.containsKey("remember")) {
|
|
this.isRememberMe = true;
|
|
if (localStorage.containsKey("email")) {
|
|
this.email = localStorage.entries.where((e) => e.key == "email").first.value;
|
|
}
|
|
if (localStorage.containsKey("token")) {
|
|
this.token = localStorage.entries.where((e) => e.key == "token").first.value;
|
|
this.password = "AnythingAsWeAlreadyHaveAccessToken";
|
|
}
|
|
if (localStorage.containsKey("instanceId")) {
|
|
this.instanceId = localStorage.entries.where((e) => e.key == "instanceId").first.value;
|
|
}
|
|
if (localStorage.containsKey("pinCode")) {
|
|
this.pinCode = localStorage.entries.where((e) => e.key == "pinCode").first.value;
|
|
}
|
|
}
|
|
if (window.location.href.contains("localhost")) {
|
|
email = "test@email.be";
|
|
password = "kljqsdkljqsd";
|
|
}
|
|
|
|
emailController.text = email;
|
|
passwordController.text = password;
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
emailController.dispose();
|
|
passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
ManagerAppContext initInstance(ManagerAppContext managerAppContext) {
|
|
var url = window.location.href;
|
|
if (!url.contains("localhost")) {
|
|
var urlParts = url.split('.');
|
|
if (urlParts.length > 0) {
|
|
var subdomain = urlParts[0].replaceAll('https://', '');
|
|
switch (subdomain) {
|
|
case "manager":
|
|
this.pageTitle = "MyInfoMate";
|
|
break;
|
|
case "fortsaintheribert":
|
|
this.pageTitle = "Fort de Saint Héribert";
|
|
break;
|
|
}
|
|
} else {
|
|
print("subdomain not found");
|
|
}
|
|
} else {
|
|
print("localhost.. set mymuseum instance by default");
|
|
}
|
|
|
|
return managerAppContext;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
|
|
initInstance(appContext.getContext());
|
|
|
|
return Scaffold(
|
|
// Le Scaffold ne se redimensionne pas a l'ouverture du clavier : sur web
|
|
// mobile ce redimensionnement relance une mise en page pendant que le
|
|
// navigateur ouvre le clavier, et le focus n'y survit pas. Le contenu est
|
|
// deja dans un SingleChildScrollView, rien ne reste sous le clavier.
|
|
resizeToAvoidBottomInset: false,
|
|
body: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final isMobile = constraints.maxWidth < 550;
|
|
final isTablet = constraints.maxWidth < 800;
|
|
final cardWidth = isMobile
|
|
? constraints.maxWidth - 32.0
|
|
: isTablet
|
|
? 420.0
|
|
: 440.0;
|
|
final horizontalPadding = isMobile ? 24.0 : 40.0;
|
|
|
|
return Container(
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
decoration: BoxDecoration(
|
|
gradient: LinearGradient(
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
colors: [
|
|
kPrimaryColor.withValues(alpha: 0.12),
|
|
kBackgroundColor,
|
|
kBackgroundColor,
|
|
],
|
|
),
|
|
),
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: EdgeInsets.all(isMobile ? 16.0 : 24.0),
|
|
child: Container(
|
|
width: cardWidth,
|
|
decoration: BoxDecoration(
|
|
color: kWhite,
|
|
borderRadius: BorderRadius.circular(16.0),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withValues(alpha: 0.08),
|
|
spreadRadius: 0,
|
|
blurRadius: 32,
|
|
offset: Offset(0, 8),
|
|
),
|
|
],
|
|
),
|
|
child: Padding(
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: horizontalPadding,
|
|
vertical: 40.0,
|
|
),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Container(
|
|
constraints: BoxConstraints(maxHeight: 80, minHeight: 40),
|
|
child: SvgPicture.asset('assets/images/MyInfoMate_logo_only.svg'),
|
|
),
|
|
SizedBox(height: 16),
|
|
AutoSizeText(
|
|
pageTitle,
|
|
style: TextStyle(
|
|
color: kPrimaryColor,
|
|
fontSize: 24,
|
|
fontFamily: "Helvetica",
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
maxLines: 2,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
FutureBuilder(
|
|
future: appVersion,
|
|
builder: (context, AsyncSnapshot<String> snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done &&
|
|
snapshot.data != null) {
|
|
return Padding(
|
|
padding: const EdgeInsets.only(top: 4),
|
|
child: Text(
|
|
snapshot.data!,
|
|
style: TextStyle(fontSize: 11, color: Colors.grey),
|
|
),
|
|
);
|
|
}
|
|
return SizedBox.shrink();
|
|
},
|
|
),
|
|
SizedBox(height: 32),
|
|
// Pas d'AutofillGroup ici : sur Flutter web il se
|
|
// materialise par un <form> DOM qui porte les inputs
|
|
// caches. La reconstruction declenchee par l'ouverture
|
|
// du clavier mobile le recreait, l'input focus etait
|
|
// detruit et le clavier se refermait aussitot. Les
|
|
// autofillHints des champs suffisent au navigateur.
|
|
Form(
|
|
key: widget.key,
|
|
child: Column(
|
|
children: [
|
|
RoundedInputField(
|
|
key: const ValueKey('login-email'),
|
|
hintText: AppLocalizations.of(context)!.email,
|
|
autofill: AutofillHints.email,
|
|
onChanged: (value) => email = value,
|
|
icon: Icons.person_outline,
|
|
controller: emailController,
|
|
isEmail: true,
|
|
fontSize: 16,
|
|
),
|
|
const SizedBox(height: kSpace4),
|
|
RoundedPasswordField(
|
|
key: const ValueKey('login-password'),
|
|
initialValue: password,
|
|
controller: passwordController,
|
|
onChanged: (value) => password = value,
|
|
),
|
|
if (kIsWeb)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 4),
|
|
child: Row(
|
|
children: [
|
|
Checkbox(
|
|
checkColor: kWhite,
|
|
activeColor: kPrimaryColor,
|
|
value: isRememberMe,
|
|
onChanged: (value) {
|
|
if (value != null) {
|
|
setState(() => isRememberMe = value);
|
|
}
|
|
},
|
|
),
|
|
Text(
|
|
AppLocalizations.of(context)!.rememberMe,
|
|
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(height: 24),
|
|
!isLoading
|
|
? SizedBox(
|
|
width: double.infinity,
|
|
child: RoundedButton(
|
|
text: AppLocalizations.of(context)!.connect,
|
|
fontSize: 16,
|
|
vertical: 15,
|
|
horizontal: 30,
|
|
press: () => authenticateTRY(appContext, true),
|
|
),
|
|
)
|
|
: CommonLoader(iconSize: 40),
|
|
SizedBox(height: 8),
|
|
TextButton(
|
|
onPressed: () => context.go('/forgot-password'),
|
|
child: Text(
|
|
AppLocalizations.of(context)!.forgotPasswordLink,
|
|
style: TextStyle(color: kPrimaryColor, fontSize: 13),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<String> getAppVersion() async {
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
return 'v${packageInfo.version} \u00b7 $kGitSha';
|
|
}
|
|
}
|