Files
app_fideliter_client/lib/screens/complete_profile_screen.dart
2026-07-18 13:11:03 +02:00

135 lines
4.3 KiB
Dart

import 'package:flutter/material.dart';
import '../pocketbase_config.dart';
import '../services/session_client.dart';
import '../theme.dart';
/// Affiché quand le compte est connecté mais n'a pas encore de fiche fidélité
/// (ex : après une inscription avec confirmation d'email). On crée le profil.
class CompleteProfileScreen extends StatefulWidget {
const CompleteProfileScreen({super.key});
@override
State<CompleteProfileScreen> createState() => _CompleteProfileScreenState();
}
class _CompleteProfileScreenState extends State<CompleteProfileScreen> {
final _formKey = GlobalKey<FormState>();
final _nomCtrl = TextEditingController();
final _prenomCtrl = TextEditingController();
final _telCtrl = TextEditingController();
bool _enCours = false;
String? _erreur;
@override
void dispose() {
_nomCtrl.dispose();
_prenomCtrl.dispose();
_telCtrl.dispose();
super.dispose();
}
Future<void> _valider() async {
if (!_formKey.currentState!.validate()) return;
setState(() {
_enCours = true;
_erreur = null;
});
try {
await SessionClient.instance.creerProfil(
nom: _nomCtrl.text,
prenom: _prenomCtrl.text,
telephone: _telCtrl.text,
);
// creerProfil recharge la session → l'AuthGate affiche l'accueil.
} catch (e) {
if (mounted) {
setState(() {
_erreur = 'Erreur : $e';
_enCours = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Finaliser mon profil'),
actions: [
IconButton(
tooltip: 'Se déconnecter',
icon: const Icon(Icons.logout),
onPressed: () => pb.authStore.clear(),
),
],
),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Encore une étape pour activer votre carte de fidélité.',
style: TextStyle(color: AppTheme.grisTexte),
),
const SizedBox(height: 20),
TextFormField(
controller: _nomCtrl,
textCapitalization: TextCapitalization.words,
autofocus: true,
decoration: const InputDecoration(
labelText: 'Nom',
prefixIcon: Icon(Icons.person_outline),
),
validator: (v) =>
(v == null || v.trim().isEmpty) ? 'Nom obligatoire' : null,
),
const SizedBox(height: 14),
TextFormField(
controller: _prenomCtrl,
textCapitalization: TextCapitalization.words,
decoration: const InputDecoration(
labelText: 'Prénom',
prefixIcon: Icon(Icons.badge_outlined),
),
),
const SizedBox(height: 14),
TextFormField(
controller: _telCtrl,
keyboardType: TextInputType.phone,
decoration: const InputDecoration(
labelText: 'Téléphone (facultatif)',
prefixIcon: Icon(Icons.phone_outlined),
),
),
if (_erreur != null) ...[
const SizedBox(height: 16),
Text(_erreur!,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.red.shade600)),
],
const SizedBox(height: 24),
FilledButton(
onPressed: _enCours ? null : _valider,
child: _enCours
? const SizedBox(
height: 22,
width: 22,
child: CircularProgressIndicator(
strokeWidth: 2, color: Colors.white))
: const Text('Activer ma carte'),
),
],
),
),
),
),
);
}
}