304 lines
8.9 KiB
Dart
304 lines
8.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import '../Screens/home_screen.dart';
|
|
import '../theme/app_theme.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen>
|
|
with SingleTickerProviderStateMixin {
|
|
bool _isLogin = true;
|
|
bool _isLoading = false;
|
|
bool _obscurePassword = true;
|
|
|
|
final _usernameController = TextEditingController();
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: AppColors.background,
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24),
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
minHeight: MediaQuery.of(context).size.height -
|
|
MediaQuery.of(context).padding.top -
|
|
MediaQuery.of(context).padding.bottom,
|
|
),
|
|
child: IntrinsicHeight(
|
|
child: Column(
|
|
children: [
|
|
const SizedBox(height: 40),
|
|
_buildBrandHeader(),
|
|
const SizedBox(height: 32),
|
|
_buildModeToggle(),
|
|
const SizedBox(height: 28),
|
|
_buildForm(),
|
|
const SizedBox(height: 24),
|
|
_buildSubmitButton(),
|
|
const Spacer(),
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 24, bottom: 8),
|
|
child: Text(
|
|
'Versão 1.0.0',
|
|
style: AppText.caption,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildBrandHeader() {
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
width: 84,
|
|
height: 84,
|
|
decoration: BoxDecoration(
|
|
gradient: AppColors.brandGradient,
|
|
borderRadius: BorderRadius.circular(24),
|
|
boxShadow: AppShadows.brand,
|
|
),
|
|
child: const Icon(
|
|
Icons.auto_awesome_rounded,
|
|
color: Colors.white,
|
|
size: 42,
|
|
),
|
|
),
|
|
const SizedBox(height: 18),
|
|
const Text('DayMaker', style: AppText.h1),
|
|
const SizedBox(height: 6),
|
|
Text(
|
|
'Organize a sua rotina com inteligência',
|
|
style: AppText.bodySecondary,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildModeToggle() {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceAlt,
|
|
borderRadius: BorderRadius.circular(AppRadius.pill),
|
|
),
|
|
padding: const EdgeInsets.all(4),
|
|
child: Row(
|
|
children: [
|
|
_toggleButton('Entrar', _isLogin, () {
|
|
setState(() => _isLogin = true);
|
|
}),
|
|
_toggleButton('Criar Conta', !_isLogin, () {
|
|
setState(() => _isLogin = false);
|
|
}),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _toggleButton(String label, bool selected, VoidCallback onTap) {
|
|
return Expanded(
|
|
child: AnimatedContainer(
|
|
duration: const Duration(milliseconds: 200),
|
|
curve: Curves.easeOut,
|
|
decoration: BoxDecoration(
|
|
color: selected ? AppColors.surface : Colors.transparent,
|
|
borderRadius: BorderRadius.circular(AppRadius.pill),
|
|
boxShadow: selected ? AppShadows.soft : null,
|
|
),
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
borderRadius: BorderRadius.circular(AppRadius.pill),
|
|
onTap: onTap,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 12),
|
|
child: Center(
|
|
child: Text(
|
|
label,
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: selected
|
|
? AppColors.primary
|
|
: AppColors.textSecondary,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildForm() {
|
|
return Column(
|
|
children: [
|
|
_inputField(
|
|
controller: _emailController,
|
|
icon: Icons.email_outlined,
|
|
hint: 'Insira o seu email',
|
|
keyboard: TextInputType.emailAddress,
|
|
),
|
|
AnimatedSize(
|
|
duration: const Duration(milliseconds: 250),
|
|
curve: Curves.easeOut,
|
|
child: !_isLogin
|
|
? Padding(
|
|
padding: const EdgeInsets.only(top: 12),
|
|
child: _inputField(
|
|
controller: _usernameController,
|
|
icon: Icons.person_outline,
|
|
hint: 'Insira o seu nome',
|
|
),
|
|
)
|
|
: const SizedBox.shrink(),
|
|
),
|
|
const SizedBox(height: 12),
|
|
_inputField(
|
|
controller: _passwordController,
|
|
icon: Icons.lock_outline_rounded,
|
|
hint: 'Insira a sua palavra-passe',
|
|
obscure: _obscurePassword,
|
|
trailing: IconButton(
|
|
onPressed: () =>
|
|
setState(() => _obscurePassword = !_obscurePassword),
|
|
icon: Icon(
|
|
_obscurePassword
|
|
? Icons.visibility_off_outlined
|
|
: Icons.visibility_outlined,
|
|
color: AppColors.textSecondary,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _inputField({
|
|
required TextEditingController controller,
|
|
required IconData icon,
|
|
required String hint,
|
|
TextInputType? keyboard,
|
|
bool obscure = false,
|
|
Widget? trailing,
|
|
}) {
|
|
return Container(
|
|
decoration: AppDecorations.outlined(),
|
|
child: TextField(
|
|
controller: controller,
|
|
keyboardType: keyboard,
|
|
obscureText: obscure,
|
|
style: AppText.body,
|
|
decoration: InputDecoration(
|
|
prefixIcon: Icon(icon, color: AppColors.textSecondary),
|
|
suffixIcon: trailing,
|
|
hintText: hint,
|
|
hintStyle: TextStyle(color: AppColors.textTertiary),
|
|
border: InputBorder.none,
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: 8,
|
|
vertical: 16,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildSubmitButton() {
|
|
return AppButton(
|
|
label: _isLogin ? 'Entrar' : 'Criar Conta',
|
|
icon: Icons.arrow_forward_rounded,
|
|
loading: _isLoading,
|
|
onPressed: () => _isLogin ? _handleLogin() : _handleSignUp(),
|
|
);
|
|
}
|
|
|
|
Future<void> _handleLogin() async {
|
|
final email = _emailController.text.trim();
|
|
final password = _passwordController.text.trim();
|
|
if (email.isEmpty || password.isEmpty) {
|
|
AppSnack.error(context, 'Por favor, preencha todos os campos');
|
|
return;
|
|
}
|
|
setState(() => _isLoading = true);
|
|
try {
|
|
final response =
|
|
await Supabase.instance.client.auth.signInWithPassword(
|
|
email: email,
|
|
password: password,
|
|
);
|
|
if (response.user != null && mounted) {
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(builder: (_) => const HomeScreen()),
|
|
);
|
|
}
|
|
} on AuthException catch (e) {
|
|
if (mounted) AppSnack.error(context, 'Erro de login: ${e.message}');
|
|
} catch (e) {
|
|
if (mounted) AppSnack.error(context, 'Erro: $e');
|
|
} finally {
|
|
if (mounted) setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _handleSignUp() async {
|
|
final email = _emailController.text.trim();
|
|
final username = _usernameController.text.trim();
|
|
final password = _passwordController.text.trim();
|
|
if (email.isEmpty || username.isEmpty || password.isEmpty) {
|
|
AppSnack.error(context, 'Por favor, preencha todos os campos');
|
|
return;
|
|
}
|
|
setState(() => _isLoading = true);
|
|
try {
|
|
final response = await Supabase.instance.client.auth.signUp(
|
|
email: email,
|
|
password: password,
|
|
data: {'username': username},
|
|
);
|
|
if (response.user != null) {
|
|
await Supabase.instance.client.from('users').insert({
|
|
'id': response.user!.id,
|
|
'nome': username,
|
|
'email': email,
|
|
});
|
|
if (mounted) {
|
|
AppSnack.success(context, 'Conta criada com sucesso!');
|
|
setState(() => _isLogin = true);
|
|
}
|
|
}
|
|
} on AuthException catch (e) {
|
|
if (mounted) {
|
|
AppSnack.error(context, 'Erro ao criar conta: ${e.message}');
|
|
}
|
|
} catch (e) {
|
|
if (mounted) AppSnack.error(context, 'Erro: $e');
|
|
} finally {
|
|
if (mounted) setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
}
|