Files
Ontime/ontime_merchant_flutter/lib/features/auth/presentation/login_screen.dart
T
2026-03-11 15:29:37 +07:00

93 lines
3.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:ontime_merchant_flutter/core/fcm_service.dart';
import 'package:ontime_merchant_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 _phoneController = TextEditingController();
final _passwordController = TextEditingController();
@override
void 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(
noTelepon: _phoneController.text.trim(),
password: _passwordController.text,
fcmToken: fcmToken,
);
final state = ref.read(authControllerProvider);
if (state.merchant != null && mounted) context.go('/home');
}
@override
Widget build(BuildContext context) {
final state = ref.watch(authControllerProvider);
return Scaffold(
appBar: AppBar(title: const Text('Masuk Merchant')),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
controller: _phoneController,
decoration: const InputDecoration(labelText: 'No. Telepon'),
keyboardType: TextInputType.phone,
validator: (v) =>
(v == null || v.isEmpty) ? 'No. telepon wajib diisi' : null,
),
const SizedBox(height: 12),
TextFormField(
controller: _passwordController,
decoration: const InputDecoration(labelText: 'Kata sandi'),
obscureText: true,
validator: (v) =>
(v == null || v.isEmpty) ? 'Password wajib diisi' : null,
),
if (state.errorMessage != null) ...[
const SizedBox(height: 8),
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'),
),
],
),
),
),
),
);
}
}