stats page pagina melhrar
This commit is contained in:
@@ -1,25 +1,18 @@
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
class RegisterController with ChangeNotifier {
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
class RegisterController extends ChangeNotifier {
|
||||
// Chave para identificar e validar o formulário
|
||||
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
||||
|
||||
final TextEditingController emailController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
final TextEditingController confirmPasswordController = TextEditingController();
|
||||
|
||||
bool _isLoading = false;
|
||||
String? _emailError;
|
||||
String? _passwordError;
|
||||
String? _confirmPasswordError; // Novo!
|
||||
String? get confirmPasswordError => _confirmPasswordError; // Novo!
|
||||
final nameController = TextEditingController();
|
||||
final emailController = TextEditingController();
|
||||
final passwordController = TextEditingController();
|
||||
final confirmPasswordController = TextEditingController(); // Novo campo
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
String? get emailError => _emailError;
|
||||
String? get passwordError => _passwordError;
|
||||
bool isLoading = false;
|
||||
|
||||
// Validações
|
||||
// --- AS TUAS VALIDAÇÕES ---
|
||||
String? validateEmail(String? value) {
|
||||
if (value == null || value.isEmpty) return 'Por favor, insira o seu email';
|
||||
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
|
||||
@@ -32,6 +25,7 @@ class RegisterController with ChangeNotifier {
|
||||
if (value.length < 6) return 'A password deve ter pelo menos 6 caracteres';
|
||||
return null;
|
||||
}
|
||||
|
||||
String? validateConfirmPassword(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Por favor, confirme a sua password';
|
||||
@@ -41,57 +35,56 @@ class RegisterController with ChangeNotifier {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------------------------
|
||||
|
||||
// MÉTODO PARA CRIAR CONTA (SIGN UP)
|
||||
Future<bool> signUp() async {
|
||||
_emailError = validateEmail(emailController.text);
|
||||
_passwordError = validatePassword(passwordController.text);
|
||||
_emailError = validateEmail(emailController.text);
|
||||
_passwordError = validatePassword(passwordController.text);
|
||||
_confirmPasswordError = validateConfirmPassword(confirmPasswordController.text); // Valida aqui!
|
||||
|
||||
if (_emailError != null || _passwordError != null || _confirmPasswordError != null) {
|
||||
notifyListeners();
|
||||
return false;
|
||||
Future<void> signUp(BuildContext context) async {
|
||||
// 1. Verifica se o formulário é válido antes de fazer qualquer coisa
|
||||
if (!formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
_isLoading = true;
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await _auth.createUserWithEmailAndPassword(
|
||||
final AuthResponse res = await Supabase.instance.client.auth.signUp(
|
||||
email: emailController.text.trim(),
|
||||
password: passwordController.text.trim(),
|
||||
data: {'full_name': nameController.text.trim()},
|
||||
);
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return true;
|
||||
} on FirebaseAuthException catch (e) {
|
||||
_isLoading = false;
|
||||
_handleFirebaseError(e.code);
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void _handleFirebaseError(String code) {
|
||||
switch (code) {
|
||||
case 'email-already-in-use':
|
||||
_emailError = 'Este e-mail já está a ser utilizado.';
|
||||
break;
|
||||
case 'weak-password':
|
||||
_passwordError = 'A password é demasiado fraca.';
|
||||
break;
|
||||
default:
|
||||
_emailError = 'Erro ao registar: $code';
|
||||
final user = res.user;
|
||||
|
||||
if (user != null && context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Conta criada! Podes fazer login.')),
|
||||
);
|
||||
Navigator.pop(context);
|
||||
}
|
||||
} on AuthException catch (e) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(e.message), backgroundColor: Colors.red),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Erro inesperado'), backgroundColor: Colors.red),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
nameController.dispose();
|
||||
emailController.dispose();
|
||||
passwordController.dispose();
|
||||
confirmPasswordController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user