Tela de login completa
This commit is contained in:
74
lib/controllers/login_controller.dart
Normal file
74
lib/controllers/login_controller.dart
Normal file
@@ -0,0 +1,74 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LoginController with ChangeNotifier {
|
||||
final TextEditingController emailController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
|
||||
bool _isLoading = false;
|
||||
bool _obscurePassword = true;
|
||||
String? _emailError;
|
||||
String? _passwordError;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
bool get obscurePassword => _obscurePassword;
|
||||
String? get emailError => _emailError;
|
||||
String? get passwordError => _passwordError;
|
||||
|
||||
void togglePasswordVisibility() {
|
||||
_obscurePassword = !_obscurePassword;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
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}$');
|
||||
if (!emailRegex.hasMatch(value)) {
|
||||
return 'Por favor, insira um email válido';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String? validatePassword(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Por favor, insira a sua password';
|
||||
}
|
||||
if (value.length < 6) {
|
||||
return 'A password deve ter pelo menos 6 caracteres';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> login() async {
|
||||
_emailError = validateEmail(emailController.text);
|
||||
_passwordError = validatePassword(passwordController.text);
|
||||
|
||||
if (_emailError != null || _passwordError != null) {
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
|
||||
// Simula login bem-sucedido
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return true;
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
_emailError = 'Erro no login. Tente novamente.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
emailController.dispose();
|
||||
passwordController.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user