Skip to content

Commit

Permalink
Make Register account works
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaffailhami committed Jun 19, 2024
1 parent 112cf53 commit bb07c33
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 11 deletions.
11 changes: 9 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:gasku/firebase_options.dart';
import 'package:gasku/pages/main.dart';
import 'package:gasku/pages/masuk.dart';
import 'package:google_fonts/google_fonts.dart';

void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}

Expand All @@ -27,7 +34,7 @@ class MyApp extends StatelessWidget {
dragHandleColor: Colors.white,
backgroundColor: Colors.transparent,
)),
home: const MyMainPage(),
home: const MyMasukPage(),
);
}
}
143 changes: 143 additions & 0 deletions lib/models/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:firebase_database/firebase_database.dart';

class User {
final String id;
final String nama;
final String nik;
final String kk;
final String noTelepon;
final String kataSandi;

User({
required this.id,
required this.nama,
required this.nik,
required this.kk,
required this.noTelepon,
required this.kataSandi,
});

factory User.autoID({
required String nama,
required String nik,
required String kk,
required String noTelepon,
required String kataSandi,
}) {
final id = DateTime.now().millisecondsSinceEpoch.toString();

return User(
id: id,
nama: nama,
nik: nik,
kk: kk,
noTelepon: noTelepon,
kataSandi: kataSandi,
);
}

User copyWith({
String? id,
String? nama,
String? nik,
String? kk,
String? noTelepon,
String? kataSandi,
}) {
return User(
id: id ?? this.id,
nama: nama ?? this.nama,
nik: nik ?? this.nik,
kk: kk ?? this.kk,
noTelepon: noTelepon ?? this.noTelepon,
kataSandi: kataSandi ?? this.kataSandi,
);
}

static Future<void> add(User user) async {
final hashedPassword = sha1.convert(utf8.encode(user.kataSandi)).toString();
final newUser = user.copyWith(kataSandi: hashedPassword);

await FirebaseDatabase.instance
.ref('users/${newUser.id}')
.set(newUser.toMap());
}

static Future<User?> get(String userID) async {
final snapshot = await FirebaseDatabase.instance.ref('users/$userID').get();

if (!snapshot.exists) {
print('User tidak ditemukan!');
return null;
}

print(snapshot.value);
return User.fromMap(snapshot.value as Map<String, dynamic>);
}

static Future<void> resetKataSandi(
String userID, String kataSandiBaru) async {
final hashedPassword = sha1.convert(utf8.encode(kataSandiBaru)).toString();

await FirebaseDatabase.instance
.ref('users/${userID}')
.update({'kataSandi': hashedPassword});
}

Map<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'nama': nama,
'nik': nik,
'kk': kk,
'noTelepon': noTelepon,
'kataSandi': kataSandi,
};
}

factory User.fromMap(Map<String, dynamic> map) {
return User(
id: map['id'] as String,
nama: map['nama'] as String,
nik: map['nik'] as String,
kk: map['kk'] as String,
noTelepon: map['noTelepon'] as String,
kataSandi: map['kataSandi'] as String,
);
}

String toJson() => json.encode(toMap());

factory User.fromJson(String source) =>
User.fromMap(json.decode(source) as Map<String, dynamic>);

@override
String toString() {
return 'User(id: $id, nama: $nama, nik: $nik, kk: $kk, noTelepon: $noTelepon, kataSandi: $kataSandi)';
}

@override
bool operator ==(covariant User other) {
if (identical(this, other)) return true;

return other.id == id &&
other.nama == nama &&
other.nik == nik &&
other.kk == kk &&
other.noTelepon == noTelepon &&
other.kataSandi == kataSandi;
}

@override
int get hashCode {
return id.hashCode ^
nama.hashCode ^
nik.hashCode ^
kk.hashCode ^
noTelepon.hashCode ^
kataSandi.hashCode;
}
}
99 changes: 96 additions & 3 deletions lib/pages/daftar.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:gasku/models/user.dart';
import 'package:gasku/pages/masuk.dart';
import 'package:gasku/utils/show_loading_screen.dart';
import 'package:gasku/widgets/filled_button.dart';
import 'package:gasku/widgets/text_form_field.dart';

Expand All @@ -13,6 +16,43 @@ class MyDaftarPage extends StatefulWidget {
class _MyDaftarPageState extends State<MyDaftarPage> {
final _formKey = GlobalKey<FormState>();

String _nama = '';
String _nik = '';
String _kk = '';
String _noTelepon = '';
String _kataSandi = '';

Future<void> onSubmit() async {
if (!_formKey.currentState!.validate()) return;

showLoadingScreen(context);

await User.add(
User.autoID(
nama: _nama,
nik: _nik,
kk: _kk,
noTelepon: _noTelepon,
kataSandi: _kataSandi,
),
);

Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => MyMasukPage()),
(_) => false,
);

ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Akun berhasil didaftarkan'),
backgroundColor: Theme.of(context).colorScheme.primary,
action: SnackBarAction(
label: 'Tutup',
onPressed: () => ScaffoldMessenger.of(context).hideCurrentSnackBar(),
),
));
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -44,38 +84,91 @@ class _MyDaftarPageState extends State<MyDaftarPage> {
const SizedBox(height: 10),
Form(
key: _formKey,
child: const Column(
child: Column(
children: [
MyTextFormField(
title: 'Nama Lengkap',
label: 'Masukkan Nama Anda',
onChanged: (value) => _nama = value,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Nama tidak boleh kosong';
}
if (value.length > 40) {
return 'Nama maksimal 40 karakter';
}
return null;
},
),
MyTextFormField(
title: 'Nomor Induk Kependudukan (NIK)',
label: 'Masukkan NIK Anda',
keyboardType: TextInputType.number,
onChanged: (value) => _nik = value,
validator: (value) {
if (value == null || value.isEmpty) {
return 'NIK tidak boleh kosong';
}
if (value.length != 16) {
return 'NIK harus 16 digit';
}
return null;
},
),
MyTextFormField(
title: 'Nomor Kartu Keluarga (KK)',
label: 'Masukkan Nomor KK Anda',
keyboardType: TextInputType.number,
onChanged: (value) => _kk = value,
validator: (value) {
if (value == null || value.isEmpty) {
return 'KK tidak boleh kosong';
}
if (value.length != 16) {
return 'KK harus 16 digit';
}
return null;
},
),
MyTextFormField(
prefix: Text('+62'),
keyboardType: TextInputType.number,
title: 'Nomor Telepon',
label: 'Masukkan Nomor Telepon Anda',
onChanged: (value) => _noTelepon = value,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Nomor Telepon tidak boleh kosong';
}
if (value.length > 20) {
return 'Nomor Telepon maksimal 20 karakter';
}
return null;
},
),
MyTextFormField(
isObscure: true,
title: 'Kata Sandi',
label: 'Masukkan Kata Sandi',
onChanged: (value) => _kataSandi = value,
onFieldSubmitted: onSubmit,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Kata Sandi tidak boleh kosong';
}
if (value.length > 40) {
return 'Kata Sandi maksimal 40 karakter';
}
return null;
},
),
],
),
),
const SizedBox(height: 35),
const SizedBox(
SizedBox(
width: double.infinity,
child: MyFilledButton(text: 'Daftar'),
child: MyFilledButton(text: 'Daftar', onPressed: onSubmit),
),
const SizedBox(height: 15),
RichText(
Expand Down
18 changes: 18 additions & 0 deletions lib/utils/show_loading_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';

void showLoadingScreen(BuildContext context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return Container(
color: Colors.black26,
child: Center(
child: CircularProgressIndicator(
color: Theme.of(context).colorScheme.primary,
),
),
);
},
);
}
11 changes: 9 additions & 2 deletions lib/widgets/filled_button.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import 'package:flutter/material.dart';

class MyFilledButton extends StatelessWidget {
const MyFilledButton({super.key, required this.text, this.color});
const MyFilledButton({
super.key,
required this.text,
this.color,
this.onPressed,
});

final String text;
final Color? color;
final void Function()? onPressed;

@override
Widget build(BuildContext context) {
return FilledButton(
onPressed: () {},
onPressed: onPressed,
style: FilledButton.styleFrom(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
Expand Down
12 changes: 9 additions & 3 deletions lib/widgets/text_form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class MyTextFormField extends StatefulWidget {
required this.label,
this.prefix,
this.keyboardType,
this.validator,
this.onChanged,
this.onFieldSubmitted,
});

final String? initialValue;
Expand All @@ -17,6 +20,9 @@ class MyTextFormField extends StatefulWidget {
final String label;
final Widget? prefix;
final TextInputType? keyboardType;
final String? Function(String?)? validator;
final void Function(String value)? onChanged;
final void Function()? onFieldSubmitted;

@override
State<MyTextFormField> createState() => _MyTextFormFieldState();
Expand All @@ -41,9 +47,9 @@ class _MyTextFormFieldState extends State<MyTextFormField> {
keyboardType: widget.keyboardType,
obscureText: widget.isObscure ? _isInvisible : false,
textInputAction: TextInputAction.next,
onFieldSubmitted: (a) {
print('SUBMIT');
},
validator: widget.validator,
onChanged: widget.onChanged,
onFieldSubmitted: (a) => widget.onFieldSubmitted!(),
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: widget.label,
Expand Down
Loading

0 comments on commit bb07c33

Please sign in to comment.