207 lines
7.4 KiB
Dart
207 lines
7.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:flutter_application_1/app/app_session_view_model.dart';
|
|
import 'package:flutter_application_1/core/theme/app_colors.dart';
|
|
import 'package:flutter_application_1/features/auth/login_page.dart';
|
|
import 'package:flutter_application_1/shared/widgets/brand_logo.dart';
|
|
import 'package:flutter_application_1/shared/widgets/custom_text_field.dart';
|
|
import 'package:flutter_application_1/shared/widgets/primary_button.dart';
|
|
|
|
class SignUpPage extends StatefulWidget {
|
|
const SignUpPage({super.key});
|
|
|
|
@override
|
|
State<SignUpPage> createState() => _SignUpPageState();
|
|
}
|
|
|
|
class _SignUpPageState extends State<SignUpPage> {
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
final _confirmPasswordController = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
_confirmPasswordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _handleCreateAccount(BuildContext context) async {
|
|
FocusScope.of(context).unfocus();
|
|
final session = context.read<AppSessionViewModel>();
|
|
final messenger = ScaffoldMessenger.of(context);
|
|
final navigator = Navigator.of(context);
|
|
final email = _emailController.text.trim();
|
|
final password = _passwordController.text;
|
|
final confirmPassword = _confirmPasswordController.text;
|
|
|
|
if (email.isEmpty || password.isEmpty || confirmPassword.isEmpty) {
|
|
_showMessageWithMessenger(messenger, 'Semua field wajib diisi.');
|
|
return;
|
|
}
|
|
|
|
if (password.length < 6) {
|
|
_showMessageWithMessenger(messenger, 'Password minimal 6 karakter.');
|
|
return;
|
|
}
|
|
|
|
if (password != confirmPassword) {
|
|
_showMessageWithMessenger(messenger, 'Confirm password tidak sama.');
|
|
return;
|
|
}
|
|
|
|
final success = await session.createAccountWithEmailPassword(
|
|
email: email,
|
|
password: password,
|
|
);
|
|
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
|
|
if (success) {
|
|
await session.logoutToLogin();
|
|
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
|
|
navigator.pushAndRemoveUntil(
|
|
MaterialPageRoute(
|
|
builder: (_) => const LoginPage(
|
|
successMessage: 'Akun berhasil dibuat. Silakan login.',
|
|
),
|
|
),
|
|
(route) => false,
|
|
);
|
|
return;
|
|
}
|
|
|
|
_showMessageWithMessenger(
|
|
messenger,
|
|
session.authErrorMessage ?? 'Gagal membuat akun. Coba lagi.',
|
|
);
|
|
}
|
|
|
|
void _showMessageWithMessenger(
|
|
ScaffoldMessengerState messenger,
|
|
String message,
|
|
) {
|
|
messenger
|
|
..hideCurrentSnackBar()
|
|
..showSnackBar(
|
|
SnackBar(content: Text(message)),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final session = context.watch<AppSessionViewModel>();
|
|
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final double maxWidth =
|
|
constraints.maxWidth > 430 ? 430 : constraints.maxWidth;
|
|
final double horizontalPadding = maxWidth < 360 ? 16 : 24;
|
|
final double logoWidth = maxWidth < 360 ? 220 : 250;
|
|
|
|
return Center(
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 430),
|
|
child: SingleChildScrollView(
|
|
keyboardDismissBehavior:
|
|
ScrollViewKeyboardDismissBehavior.onDrag,
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: horizontalPadding,
|
|
vertical: 16,
|
|
),
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
minHeight: constraints.maxHeight - 32,
|
|
),
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Center(child: BrandLogo(width: logoWidth)),
|
|
const SizedBox(height: 46),
|
|
CustomTextField(
|
|
hintText: 'Email Address',
|
|
prefixIcon: Icons.mail_outline,
|
|
keyboardType: TextInputType.emailAddress,
|
|
controller: _emailController,
|
|
),
|
|
const SizedBox(height: 14),
|
|
CustomTextField(
|
|
hintText: 'Password',
|
|
prefixIcon: Icons.lock_outline,
|
|
obscureText: true,
|
|
controller: _passwordController,
|
|
),
|
|
const SizedBox(height: 14),
|
|
CustomTextField(
|
|
hintText: 'Confirm Password',
|
|
prefixIcon: Icons.check_circle_outline,
|
|
obscureText: true,
|
|
controller: _confirmPasswordController,
|
|
),
|
|
const SizedBox(height: 24),
|
|
PrimaryButton(
|
|
text: 'CREATE ACCOUNT',
|
|
isLoading: session.isAuthBusy,
|
|
onPressed: () => _handleCreateAccount(context),
|
|
),
|
|
const SizedBox(height: 30),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text(
|
|
'Already have an account?',
|
|
style: TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
const SizedBox(width: 6),
|
|
GestureDetector(
|
|
onTap: session.isAuthBusy
|
|
? null
|
|
: () {
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(
|
|
builder: (_) => const LoginPage(),
|
|
),
|
|
);
|
|
},
|
|
child: Text(
|
|
'Login',
|
|
style: TextStyle(
|
|
color: session.isAuthBusy
|
|
? AppColors.textMuted
|
|
: AppColors.primaryBlue,
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|