123 lines
3.8 KiB
Dart
123 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:ontime_user_flutter/core/fcm_service.dart';
|
|
import 'package:ontime_user_flutter/features/auth/application/auth_controller.dart';
|
|
|
|
class LoginScreen extends ConsumerStatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends ConsumerState<LoginScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _emailController = TextEditingController();
|
|
final _phoneController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailController.dispose();
|
|
_phoneController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
|
|
final fcmToken = await FcmService.getToken();
|
|
final auth = ref.read(authControllerProvider.notifier);
|
|
await auth.login(
|
|
email: _emailController.text.trim().isEmpty
|
|
? null
|
|
: _emailController.text.trim(),
|
|
phone: _phoneController.text.trim().isEmpty
|
|
? null
|
|
: _phoneController.text.trim(),
|
|
password: _passwordController.text,
|
|
fcmToken: fcmToken,
|
|
);
|
|
|
|
final state = ref.read(authControllerProvider);
|
|
if (state.user != null) {
|
|
if (mounted) {
|
|
context.go('/home');
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final state = ref.watch(authControllerProvider);
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Masuk'),
|
|
),
|
|
body: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
TextFormField(
|
|
controller: _emailController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _phoneController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'No. Telepon',
|
|
),
|
|
keyboardType: TextInputType.phone,
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Kata sandi',
|
|
),
|
|
obscureText: true,
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Password wajib diisi';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
if (state.errorMessage != null)
|
|
Text(
|
|
state.errorMessage!,
|
|
style: TextStyle(
|
|
color: Theme.of(context).colorScheme.error,
|
|
),
|
|
),
|
|
const Spacer(),
|
|
ElevatedButton(
|
|
onPressed: state.isLoading ? null : _submit,
|
|
child: state.isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Text('Masuk'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|