279 lines
8.3 KiB
Dart
279 lines
8.3 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'lupasandiotp.dart';
|
|
|
|
class LupaKataSandiEmailPage extends StatefulWidget {
|
|
const LupaKataSandiEmailPage({super.key});
|
|
|
|
@override
|
|
State<LupaKataSandiEmailPage> createState() => _LupaKataSandiEmailPageState();
|
|
}
|
|
|
|
class _LupaKataSandiEmailPageState extends State<LupaKataSandiEmailPage> {
|
|
final TextEditingController emailController = TextEditingController();
|
|
final FocusNode emailFocusNode = FocusNode();
|
|
|
|
// Ganti sesuai device yang dipakai
|
|
// Android Emulator: 10.0.2.2
|
|
// HP Fisik: pakai IP laptop, misalnya 192.168.1.8
|
|
static const String requestOtpUrl = 'http://192.168.1.6:8000/api/RequestOtp';
|
|
|
|
@override
|
|
void dispose() {
|
|
emailController.dispose();
|
|
emailFocusNode.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFF5F9FF),
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: IconButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
icon: const Icon(Icons.arrow_back, color: Colors.black),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
Image.asset('assets/images/sandi.png', width: 170),
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
const Text(
|
|
'LUPA KATA SANDI',
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.black,
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
|
child: Text(
|
|
'Masukkan email yang sudah terdaftar,\nlalu atur ulang kata sandi Anda agar\nlebih mudah diingat.',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: Colors.grey[700],
|
|
height: 1.5,
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Container(
|
|
height: 1,
|
|
width: double.infinity,
|
|
color: Colors.grey[300],
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
const Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text(
|
|
'Email',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w600,
|
|
color: Colors.black87,
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
TextField(
|
|
controller: emailController,
|
|
focusNode: emailFocusNode,
|
|
keyboardType: TextInputType.emailAddress,
|
|
decoration: InputDecoration(
|
|
hintText: 'email@gmail.com',
|
|
hintStyle: TextStyle(color: Colors.grey[500]),
|
|
filled: true,
|
|
fillColor: Colors.white,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: BorderSide(
|
|
color: Colors.grey[300]!,
|
|
width: 1.0,
|
|
),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: BorderSide(
|
|
color: Colors.grey[300]!,
|
|
width: 1.0,
|
|
),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: const BorderSide(
|
|
color: Color(0xFF206E97),
|
|
width: 1.5,
|
|
),
|
|
),
|
|
prefixIcon: Icon(
|
|
Icons.email_outlined,
|
|
color: Colors.grey[600],
|
|
size: 20,
|
|
),
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
vertical: 14,
|
|
horizontal: 16,
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Container(
|
|
height: 1,
|
|
width: double.infinity,
|
|
color: Colors.grey[300],
|
|
),
|
|
|
|
const SizedBox(height: 40),
|
|
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 50,
|
|
child: ElevatedButton(
|
|
onPressed: () {
|
|
_prosesKirimEmail();
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF206E97),
|
|
foregroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
elevation: 0,
|
|
),
|
|
child: const Text(
|
|
'Lanjut',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _prosesKirimEmail() async {
|
|
if (emailController.text.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Harap masukkan email'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (!emailController.text.contains('@') ||
|
|
!emailController.text.contains('.')) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Format email tidak valid'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (context) => const AlertDialog(
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
CircularProgressIndicator(),
|
|
SizedBox(height: 16),
|
|
Text('Mengirim kode OTP ke email...'),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse(requestOtpUrl),
|
|
headers: {'Accept': 'application/json'},
|
|
body: {'email': emailController.text.trim(), 'type': 'email'},
|
|
);
|
|
|
|
final dynamic data = jsonDecode(response.body);
|
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.pop(context);
|
|
|
|
if (response.statusCode == 200) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(data['message'] ?? 'Kode OTP berhasil dikirim'),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) =>
|
|
LupaKataSandiOtpPage(email: emailController.text.trim()),
|
|
),
|
|
);
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
data['message'] ?? data['error'] ?? 'Gagal mengirim kode OTP',
|
|
),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
|
|
Navigator.pop(context);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Terjadi kesalahan koneksi: $e'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|