280 lines
9.6 KiB
Dart
280 lines
9.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../constants/app_colors.dart';
|
|
import '../constants/app_strings.dart';
|
|
import '../services/supabase_service.dart';
|
|
import '../screens/logado_screen.dart';
|
|
|
|
class EntrarSheet extends StatefulWidget {
|
|
const EntrarSheet({super.key});
|
|
|
|
@override
|
|
State<EntrarSheet> createState() => _EntrarSheetState();
|
|
}
|
|
|
|
class _EntrarSheetState extends State<EntrarSheet> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _isLoading = false;
|
|
|
|
Future<void> _handleLogin() async {
|
|
if (_formKey.currentState!.validate()) {
|
|
setState(() => _isLoading = true);
|
|
|
|
try {
|
|
await SupabaseService.signIn(
|
|
email: _emailController.text.trim(),
|
|
password: _passwordController.text,
|
|
);
|
|
|
|
if (mounted) {
|
|
// Close the bottom sheet first
|
|
Navigator.of(context).pop();
|
|
|
|
// Show success message above the sheet
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text(AppStrings.loginSuccess),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
|
|
// Navigate to LogadoScreen
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(builder: (context) => const LogadoScreen()),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
// Close the bottom sheet first
|
|
Navigator.of(context).pop();
|
|
|
|
// Show error message above the sheet
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(e.toString()), backgroundColor: Colors.red),
|
|
);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _handlePasswordReset() async {
|
|
final email = _emailController.text.trim();
|
|
if (email.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text(AppStrings.validatorEmailEmpty),
|
|
backgroundColor: Colors.orange,
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await SupabaseService.resetPassword(email);
|
|
if (mounted) {
|
|
// Close the bottom sheet first
|
|
Navigator.of(context).pop();
|
|
|
|
// Show success message above the sheet
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text(AppStrings.resetPasswordEmailSent),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
// Close the bottom sheet first
|
|
Navigator.of(context).pop();
|
|
|
|
// Show error message above the sheet
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(e.toString()), backgroundColor: Colors.red),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return DraggableScrollableSheet(
|
|
initialChildSize: 0.6, // Start at 60% for login form
|
|
minChildSize: 0.4, // 40% minimum
|
|
maxChildSize: 0.9, // 90% maximum
|
|
snap: true, // Enable snap points
|
|
snapSizes: [0.6, 0.9], // Snap to 60% or 90%
|
|
builder: (context, scrollController) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: AppColors.backgroundGrey,
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withValues(alpha: 0.1),
|
|
blurRadius: 10,
|
|
offset: const Offset(0, -5),
|
|
),
|
|
],
|
|
),
|
|
child: SingleChildScrollView(
|
|
controller: scrollController,
|
|
child: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// Drag handle
|
|
Center(
|
|
child: Container(
|
|
width: 60,
|
|
height: 6,
|
|
margin: const EdgeInsets.symmetric(vertical: 8),
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey[400],
|
|
borderRadius: BorderRadius.circular(3),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
|
|
// Title
|
|
const Text(
|
|
AppStrings.loginTitle,
|
|
style: TextStyle(
|
|
fontSize: 28,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
const SizedBox(height: 32),
|
|
|
|
// Email field
|
|
const Text(
|
|
AppStrings.labelEmail,
|
|
style: TextStyle(fontSize: 16, color: Colors.white70),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Form(
|
|
key: _formKey,
|
|
child: TextFormField(
|
|
controller: _emailController,
|
|
decoration: InputDecoration(
|
|
filled: true,
|
|
fillColor: AppColors.white.withValues(alpha: 0.5),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
hintText: AppStrings.hintEmail,
|
|
hintStyle: const TextStyle(color: Colors.white38),
|
|
),
|
|
style: const TextStyle(color: Colors.white),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return AppStrings.validatorEmailEmpty;
|
|
}
|
|
if (!value.contains('@')) {
|
|
return AppStrings.validatorEmailInvalid;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
// Password field
|
|
const Text(
|
|
AppStrings.labelPassword,
|
|
style: TextStyle(fontSize: 16, color: Colors.white70),
|
|
),
|
|
const SizedBox(height: 8),
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(
|
|
filled: true,
|
|
fillColor: AppColors.white.withValues(alpha: 0.5),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
hintText: AppStrings.hintPassword,
|
|
hintStyle: const TextStyle(color: Colors.white38),
|
|
),
|
|
style: const TextStyle(color: Colors.white),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return AppStrings.validatorPasswordEmpty;
|
|
}
|
|
if (value.length < 6) {
|
|
return AppStrings.validatorPasswordLength;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 32),
|
|
|
|
// Login button
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 60,
|
|
child: ElevatedButton(
|
|
onPressed: _isLoading ? null : _handleLogin,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppColors.buttonColor,
|
|
foregroundColor: AppColors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(30),
|
|
),
|
|
elevation: 5,
|
|
),
|
|
child: _isLoading
|
|
? const CircularProgressIndicator(
|
|
color: Colors.white,
|
|
)
|
|
: const Text(
|
|
AppStrings.btnLogin,
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
// Forgot password
|
|
Center(
|
|
child: TextButton(
|
|
onPressed: _handlePasswordReset,
|
|
child: const Text(
|
|
AppStrings.forgotPassword,
|
|
style: TextStyle(color: Colors.white70, fontSize: 16),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 40),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|