import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:seshat/routing/routes.dart'; import 'package:seshat/ui/auth/viewmodel/login_view_model.dart'; import 'package:seshat/ui/core/ui/await_loading.dart'; class LoginPage extends StatefulWidget { const LoginPage({super.key, required this.viewModel}); final LoginViewModel viewModel; @override State createState() => _LoginPageState(); } class _LoginPageState extends State { final GlobalKey _formKey = GlobalKey(); String username = ""; String password = ""; bool hidePassword = true; @override void initState() { super.initState(); widget.viewModel.login.addListener(_onResult); } @override void didUpdateWidget(covariant LoginPage oldWidget) { super.didUpdateWidget(oldWidget); oldWidget.viewModel.removeListener(_onResult); widget.viewModel.login.addListener(_onResult); } @override void dispose() { widget.viewModel.login.removeListener(_onResult); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: ListenableBuilder( listenable: widget.viewModel, builder: (context, child) => switch (widget.viewModel.isLoaded) { false => AwaitLoading(), true => switch (widget.viewModel.isUpToDate) { false => Center( child: SizedBox( width: 300, child: Text( "L'application que vous utilisez n'est pas à jour. Si aucune mise à jour n'est disponible, contactez dev@ueauvergne.fr", ), ), ), true => Center( child: ListenableBuilder( listenable: widget.viewModel.login, builder: (context, child) { return Form( key: _formKey, child: SingleChildScrollView( child: SizedBox( width: 300, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Text("Bienvenue", style: TextStyle(fontSize: 40)), SizedBox(height: 50), TextFormField( decoration: InputDecoration( labelText: "Identifiant de section", border: OutlineInputBorder(), ), validator: (value) { if (value == null || value.isEmpty) { return "Veuillez entrer un identifiant"; } return null; }, onSaved: (newValue) { username = newValue!; }, ), SizedBox(height: 10), TextFormField( decoration: InputDecoration( labelText: "Mot de passe", border: OutlineInputBorder(), suffixIcon: IconButton( onPressed: () { setState(() { hidePassword = !hidePassword; }); }, icon: Icon( (hidePassword) ? Icons.visibility : Icons.visibility_off, ), ), ), obscureText: hidePassword, enableSuggestions: false, autocorrect: false, validator: (value) { if (value == null || value.isEmpty) { return "Veuillez entrer un mot de passe"; } return null; }, onSaved: (newValue) { password = newValue!; }, ), SizedBox(height: 10), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); widget.viewModel.login.execute(( username, password, )); } }, child: Text("Valider"), ), ], ), ), ), ); }, ), ), }, }, ), ); } void _onResult() { if (widget.viewModel.login.completed) { widget.viewModel.login.clearResult(); context.go(Routes.add); } if (widget.viewModel.login.error) { widget.viewModel.login.clearResult(); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Une erreur est survenue lors de la connexion."), action: SnackBarAction( label: "Réessayer", onPressed: () => widget.viewModel.login.execute((username, password)), ), ), ); } } }