391 lines
13 KiB
Dart
391 lines
13 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import '../Screens/home_screen.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
bool isLoginSelected = true;
|
|
bool _isLoading = false;
|
|
final TextEditingController _usernameController = TextEditingController();
|
|
final TextEditingController _emailController = TextEditingController();
|
|
final TextEditingController _passwordController = TextEditingController();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFFFE5CC), // Light orange background
|
|
appBar: AppBar(
|
|
backgroundColor: const Color(0xFF0066CC), // Blue app bar
|
|
elevation: 0,
|
|
toolbarHeight: 0, // Remove default app bar height
|
|
),
|
|
body: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
const SizedBox(height: 40),
|
|
|
|
// Blue icon with white box outline
|
|
Container(
|
|
width: 80,
|
|
height: 80,
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFF0066CC),
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Center(
|
|
child: Container(
|
|
width: 50,
|
|
height: 50,
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.white, width: 3),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
// App title and subtitle
|
|
const Text(
|
|
'DayMaker',
|
|
style: TextStyle(
|
|
fontSize: 32,
|
|
fontWeight: FontWeight.bold,
|
|
color: Color(0xFF0066CC),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
const Text(
|
|
'Organize sua rotina com inteligência',
|
|
style: TextStyle(fontSize: 16, color: Color(0xFF666666)),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
|
|
const SizedBox(height: 40),
|
|
|
|
// Toggle buttons for Login/Create Account
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: GestureDetector(
|
|
onTap: () => setState(() => isLoginSelected = true),
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 12),
|
|
decoration: BoxDecoration(
|
|
color: isLoginSelected
|
|
? const Color(0xFF0066CC)
|
|
: Colors.transparent,
|
|
borderRadius: const BorderRadius.only(
|
|
topLeft: Radius.circular(8),
|
|
bottomLeft: Radius.circular(8),
|
|
),
|
|
border: Border.all(
|
|
color: const Color(0xFF0066CC),
|
|
width: 2,
|
|
),
|
|
),
|
|
child: Text(
|
|
'Entrar',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: isLoginSelected
|
|
? Colors.white
|
|
: const Color(0xFF0066CC),
|
|
fontWeight: FontWeight.w600,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: GestureDetector(
|
|
onTap: () => setState(() => isLoginSelected = false),
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 12),
|
|
decoration: BoxDecoration(
|
|
color: !isLoginSelected
|
|
? const Color(0xFF0066CC)
|
|
: Colors.transparent,
|
|
borderRadius: const BorderRadius.only(
|
|
topRight: Radius.circular(8),
|
|
bottomRight: Radius.circular(8),
|
|
),
|
|
border: Border.all(
|
|
color: const Color(0xFF0066CC),
|
|
width: 2,
|
|
),
|
|
),
|
|
child: Text(
|
|
'Criar Conta',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: !isLoginSelected
|
|
? Colors.white
|
|
: const Color(0xFF0066CC),
|
|
fontWeight: FontWeight.w600,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
|
|
const SizedBox(height: 32),
|
|
|
|
// Email field
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: const Color(0xFFE0E0E0)),
|
|
),
|
|
child: TextField(
|
|
controller: _emailController,
|
|
keyboardType: TextInputType.emailAddress,
|
|
decoration: const InputDecoration(
|
|
prefixIcon: Icon(Icons.email, color: Color(0xFF666666)),
|
|
hintText: 'Digite seu email',
|
|
hintStyle: TextStyle(color: Color(0xFF999999)),
|
|
border: InputBorder.none,
|
|
contentPadding: EdgeInsets.symmetric(
|
|
horizontal: 16,
|
|
vertical: 16,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
// Username field (only shown when Criar Conta is selected)
|
|
if (!isLoginSelected)
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: const Color(0xFFE0E0E0)),
|
|
),
|
|
child: TextField(
|
|
controller: _usernameController,
|
|
decoration: const InputDecoration(
|
|
prefixIcon: Icon(Icons.person, color: Color(0xFF666666)),
|
|
hintText: 'Digite seu nome',
|
|
hintStyle: TextStyle(color: Color(0xFF999999)),
|
|
border: InputBorder.none,
|
|
contentPadding: EdgeInsets.symmetric(
|
|
horizontal: 16,
|
|
vertical: 16,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
if (!isLoginSelected) const SizedBox(height: 16),
|
|
|
|
// Password field
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: const Color(0xFFE0E0E0)),
|
|
),
|
|
child: TextField(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
prefixIcon: Icon(Icons.lock, color: Color(0xFF666666)),
|
|
hintText: 'Digite sua senha',
|
|
hintStyle: TextStyle(color: Color(0xFF999999)),
|
|
border: InputBorder.none,
|
|
contentPadding: EdgeInsets.symmetric(
|
|
horizontal: 16,
|
|
vertical: 16,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 32),
|
|
|
|
// Login/Create Account button
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 56,
|
|
child: ElevatedButton(
|
|
onPressed: _isLoading
|
|
? null
|
|
: () async {
|
|
if (isLoginSelected) {
|
|
await _handleLogin();
|
|
} else {
|
|
await _handleSignUp();
|
|
}
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF0066CC),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
elevation: 0,
|
|
),
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor: AlwaysStoppedAnimation<Color>(
|
|
Colors.white,
|
|
),
|
|
),
|
|
)
|
|
: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
isLoginSelected ? 'Entrar' : 'Criar Conta',
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
const Icon(
|
|
Icons.arrow_forward,
|
|
color: Colors.white,
|
|
size: 20,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
const Spacer(),
|
|
|
|
// Version text
|
|
const Text(
|
|
'Versão 1.0.0',
|
|
style: TextStyle(color: Color(0xFF666666), fontSize: 14),
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _handleLogin() async {
|
|
final email = _emailController.text.trim();
|
|
final password = _passwordController.text.trim();
|
|
|
|
if (email.isEmpty || password.isEmpty) {
|
|
_showErrorSnackBar('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) {
|
|
if (mounted) {
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(builder: (_) => const HomeScreen()),
|
|
);
|
|
}
|
|
}
|
|
} on AuthException catch (e) {
|
|
_showErrorSnackBar('Erro de login: ${e.message}');
|
|
} catch (e) {
|
|
_showErrorSnackBar('Erro ao fazer login: $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) {
|
|
_showErrorSnackBar('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) {
|
|
// Save user data to users table
|
|
await Supabase.instance.client.from('users').insert({
|
|
'id': response.user!.id,
|
|
'nome': username,
|
|
'email': email,
|
|
});
|
|
|
|
_showSuccessSnackBar('Conta criada com sucesso!');
|
|
setState(() => isLoginSelected = true);
|
|
}
|
|
} on AuthException catch (e) {
|
|
_showErrorSnackBar('Erro ao criar conta: ${e.message}');
|
|
} catch (e) {
|
|
_showErrorSnackBar('Erro ao criar conta: $e');
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void _showErrorSnackBar(String message) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(message), backgroundColor: Colors.red),
|
|
);
|
|
}
|
|
|
|
void _showSuccessSnackBar(String message) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(message), backgroundColor: Colors.green),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|