re factor rog
This commit is contained in:
71
lib/features/auth/login/login_page.dart
Normal file
71
lib/features/auth/login/login_page.dart
Normal file
@ -0,0 +1,71 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:rogapp/features/home/home_page.dart';
|
||||
import 'package:rogapp/features/services/auth_repo.dart';
|
||||
import 'package:rogapp/features/state/user_state.dart';
|
||||
|
||||
class LoginPage extends ConsumerStatefulWidget {
|
||||
@override
|
||||
_LoginScreenState createState() => _LoginScreenState();
|
||||
}
|
||||
|
||||
class _LoginScreenState extends ConsumerState<LoginPage> {
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _passwordController = TextEditingController();
|
||||
bool _isLoading = false; // Track loading state
|
||||
|
||||
void _login() {
|
||||
setState(() {
|
||||
_isLoading = true; // Start loading
|
||||
});
|
||||
String email = _emailController.text.trim();
|
||||
String password = _passwordController.text;
|
||||
|
||||
ref.read(authProvider("$email|$password").future).then((user) {
|
||||
setState(() {
|
||||
_isLoading = false; // Stop loading
|
||||
});
|
||||
if (user != null) {
|
||||
ref.read(userNotifierProvider.notifier).setUser(user);
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(
|
||||
builder: (context) => const HomePage(),
|
||||
));
|
||||
} else {
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(const SnackBar(content: Text('Login failed')));
|
||||
}
|
||||
}).catchError((error) {
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(SnackBar(content: Text('Error: $error')));
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Login')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _emailController,
|
||||
decoration: const InputDecoration(labelText: 'Email'),
|
||||
),
|
||||
TextFormField(
|
||||
controller: _passwordController,
|
||||
decoration: const InputDecoration(labelText: 'Password'),
|
||||
obscureText: true,
|
||||
),
|
||||
_isLoading
|
||||
? CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: _login,
|
||||
child: const Text('Login'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user