126 lines
3.8 KiB
Dart
126 lines
3.8 KiB
Dart
import 'package:bahasajepang/service/API_config.dart';
|
|
import 'package:bahasajepang/theme.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
|
|
class ForgotPasswordPage extends StatefulWidget {
|
|
@override
|
|
State<ForgotPasswordPage> createState() => _ForgotPasswordPageState();
|
|
}
|
|
|
|
class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
|
|
final TextEditingController _emailController = TextEditingController();
|
|
bool _isLoading = false;
|
|
|
|
Future<void> _sendEmail() async {
|
|
// Validasi email kosong
|
|
if (_emailController.text.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Email tidak boleh kosong')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
try {
|
|
final url = Uri.parse('${ApiConfig.baseUrl}/forgot-password');
|
|
final response = await http.post(
|
|
url,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
'Connection': 'keep-alive',
|
|
'ngrok-skip-browser-warning': 'true' // Untuk ngrok
|
|
},
|
|
body: json.encode({'email': _emailController.text}),
|
|
);
|
|
|
|
final responseData = json.decode(response.body);
|
|
|
|
if (response.statusCode == 200) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(responseData['message'])),
|
|
);
|
|
Navigator.pushNamed(context, '/token-verification', arguments: {
|
|
'email': _emailController.text,
|
|
});
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(responseData['message'] ?? 'Terjadi kesalahan')),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Gagal terhubung ke server: $e')),
|
|
);
|
|
} finally {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Forgot Password'),
|
|
backgroundColor: bgColor3,
|
|
),
|
|
backgroundColor: bgColor1,
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'Masukkan email Anda untuk reset password',
|
|
style: TextStyle(fontSize: 16),
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
'Pastikan Anda menginputkan email yang terdaftar dan bukan email palsu.',
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
color: Colors.redAccent,
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
TextField(
|
|
controller: _emailController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.email),
|
|
),
|
|
keyboardType: TextInputType.emailAddress,
|
|
),
|
|
const SizedBox(height: 20),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
padding: EdgeInsets.symmetric(vertical: 16),
|
|
backgroundColor: bgColor2,
|
|
),
|
|
onPressed: _isLoading ? null : _sendEmail,
|
|
child: _isLoading
|
|
? CircularProgressIndicator(color: bgColor2)
|
|
: Text(
|
|
'Kirim Token',
|
|
style: TextStyle(fontSize: 16, color: Colors.black),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|