236 lines
8.4 KiB
Dart
236 lines
8.4 KiB
Dart
import 'package:app_sensor/login_page.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
|
|
class SignUpPage extends StatefulWidget {
|
|
@override
|
|
_SignUpPageState createState() => _SignUpPageState();
|
|
}
|
|
|
|
class _SignUpPageState extends State<SignUpPage> {
|
|
final TextEditingController nameController = TextEditingController();
|
|
final TextEditingController emailController = TextEditingController();
|
|
final TextEditingController passwordController = TextEditingController();
|
|
|
|
bool isEmailValid = false;
|
|
bool isLoading = false;
|
|
bool _obscurePassword = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
emailController.addListener(_validateEmail);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
emailController.removeListener(_validateEmail);
|
|
emailController.dispose();
|
|
nameController.dispose();
|
|
passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _validateEmail() {
|
|
setState(() {
|
|
isEmailValid = emailController.text.endsWith('@gmail.com');
|
|
});
|
|
}
|
|
|
|
Future<void> _saveUserData(String email, String password) async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('email', email);
|
|
await prefs.setString('password', password);
|
|
}
|
|
|
|
Future<void> _registerUser() async {
|
|
String email = emailController.text.trim();
|
|
String password = passwordController.text.trim();
|
|
String name = nameController.text.trim();
|
|
|
|
if (name.isEmpty || password.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Nama dan Password tidak boleh kosong')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
|
|
try {
|
|
await FirebaseAuth.instance.createUserWithEmailAndPassword(
|
|
email: email,
|
|
password: password,
|
|
);
|
|
|
|
await _saveUserData(email, password);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Registrasi berhasil! Silakan login.')),
|
|
);
|
|
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => LoginPage()),
|
|
);
|
|
} on FirebaseAuthException catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(e.message ?? 'Terjadi kesalahan saat registrasi')),
|
|
);
|
|
} finally {
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Container(
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
decoration: const BoxDecoration(
|
|
gradient: LinearGradient(
|
|
colors: [
|
|
Color(0xFFB3E5FC), // biru pastel muda
|
|
Color(0xFFE1F5FE), // biru pastel pucat
|
|
],
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
),
|
|
),
|
|
padding: const EdgeInsets.all(20.0),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
children: [
|
|
SizedBox(height: 60),
|
|
Icon(Icons.person_add, size: 80, color: Colors.blueGrey[800]),
|
|
SizedBox(height: 10),
|
|
Text(
|
|
"Buat Akun Baru",
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.blueGrey[800],
|
|
),
|
|
),
|
|
SizedBox(height: 30),
|
|
Container(
|
|
padding: EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white.withOpacity(0.95),
|
|
borderRadius: BorderRadius.circular(16),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.blueGrey.withOpacity(0.1),
|
|
blurRadius: 20,
|
|
offset: Offset(0, 10),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
children: [
|
|
TextField(
|
|
controller: nameController,
|
|
decoration: InputDecoration(
|
|
labelText: 'Nama',
|
|
prefixIcon: Icon(Icons.person, color: Colors.blueGrey),
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
),
|
|
SizedBox(height: 15),
|
|
TextField(
|
|
controller: emailController,
|
|
keyboardType: TextInputType.emailAddress,
|
|
decoration: InputDecoration(
|
|
labelText: 'Email',
|
|
prefixIcon: Icon(Icons.email, color: Colors.blueGrey),
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
),
|
|
SizedBox(height: 15),
|
|
TextField(
|
|
controller: passwordController,
|
|
obscureText: _obscurePassword,
|
|
decoration: InputDecoration(
|
|
labelText: 'Password',
|
|
prefixIcon: Icon(Icons.lock, color: Colors.blueGrey),
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscurePassword ? Icons.visibility_off : Icons.visibility,
|
|
color: Colors.blueGrey,
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscurePassword = !_obscurePassword;
|
|
});
|
|
},
|
|
),
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
),
|
|
if (!isEmailValid && emailController.text.isNotEmpty)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 10),
|
|
child: Text(
|
|
'Email harus berakhiran @gmail.com',
|
|
style: TextStyle(color: Colors.redAccent),
|
|
),
|
|
),
|
|
SizedBox(height: 20),
|
|
ElevatedButton(
|
|
onPressed: isEmailValid && !isLoading ? _registerUser : null,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Color(0xFF81D4FA),
|
|
minimumSize: Size(double.infinity, 48),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
),
|
|
child: isLoading
|
|
? CircularProgressIndicator(color: Colors.white)
|
|
: Text(
|
|
'DAFTAR',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white,
|
|
letterSpacing: 1.1,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 10),
|
|
OutlinedButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
style: OutlinedButton.styleFrom(
|
|
minimumSize: Size(double.infinity, 48),
|
|
side: BorderSide(color: Colors.blueGrey),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
),
|
|
child: Text(
|
|
'KEMBALI',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.blueGrey[700],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|