diff --git a/lib/cubits/pengguna_masuk.dart b/lib/cubits/pengguna_masuk.dart index 23c129f..f9aaed8 100644 --- a/lib/cubits/pengguna_masuk.dart +++ b/lib/cubits/pengguna_masuk.dart @@ -17,6 +17,12 @@ class PenggunaMasukCubit extends Cubit { emit(pengguna); } + Future masukTanpaSandi({required String nik}) async { + final pengguna = await Pengguna.get(nik); + + emit(pengguna); + } + Future edit(Pengguna pengguna) async { await Pengguna.edit(pengguna.nik, pengguna); diff --git a/lib/main.dart b/lib/main.dart index f6d0952..cb946f0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,8 +4,11 @@ import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:gasku/cubits/daftar_pangkalan.dart'; import 'package:gasku/cubits/pengguna_masuk.dart'; import 'package:gasku/cubits/posisi_pengguna.dart'; +import 'package:gasku/pages/main.dart'; import 'package:gasku/pages/masuk.dart'; +import 'package:gasku/utils/show_loading_screen.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'package:shared_preferences/shared_preferences.dart'; void main() async { await dotenv.load(fileName: ".env"); @@ -43,7 +46,35 @@ class MyApp extends StatelessWidget { dragHandleColor: Colors.white, backgroundColor: Colors.transparent, )), - home: const MyMasukPage(), + home: FutureBuilder( + future: SharedPreferences.getInstance(), + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return const SizedBox.shrink(); + } else { + final nikPengguna = snapshot.data!.getString('nik_pengguna_masuk'); + + if (nikPengguna == null) return const MyMasukPage(); + + return FutureBuilder( + future: context + .read() + .masukTanpaSandi(nik: nikPengguna), + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + if (context.read().state == null) { + return const MyMasukPage(); + } + + return const MyMainPage(); + } else { + return const SizedBox.shrink(); + } + }, + ); + } + }, + ), ); } } diff --git a/lib/pages/masuk.dart b/lib/pages/masuk.dart index e5273a2..d9bf965 100644 --- a/lib/pages/masuk.dart +++ b/lib/pages/masuk.dart @@ -8,6 +8,7 @@ import 'package:gasku/utils/show_loading_screen.dart'; import 'package:gasku/widgets/filled_button.dart'; import 'package:gasku/widgets/text_form_field.dart'; import 'package:gasku/pages/daftar.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class MyMasukPage extends StatefulWidget { const MyMasukPage({super.key}); @@ -32,6 +33,9 @@ class _MyMasukPageState extends State { .read() .masuk(nik: _nik, kataSandi: _kataSandi); + (await SharedPreferences.getInstance()) + .setString('nik_pengguna_masuk', _nik); + Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (_) => MyMainPage()), diff --git a/lib/pages/profil.dart b/lib/pages/profil.dart index 8db9a6f..2d96b61 100644 --- a/lib/pages/profil.dart +++ b/lib/pages/profil.dart @@ -8,8 +8,10 @@ import 'package:gasku/pages/edit_profil.dart'; import 'package:gasku/pages/faq.dart'; import 'package:gasku/pages/kontak_kami.dart'; import 'package:gasku/pages/masuk.dart'; +import 'package:gasku/utils/show_loading_screen.dart'; import 'package:gasku/widgets/divider.dart'; import 'package:gasku/widgets/list_tile_button.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class MyProfilPage extends StatelessWidget { const MyProfilPage({super.key}); @@ -26,14 +28,35 @@ class MyProfilPage extends StatelessWidget { child: const Text('Batal'), ), TextButton( - onPressed: () { - context.read().keluar(); + onPressed: () async { + showLoadingScreen(context); - Navigator.pushAndRemoveUntil( - context, - MaterialPageRoute(builder: (_) => const MyMasukPage()), - (_) => false, - ); + try { + (await SharedPreferences.getInstance()) + .remove('nik_pengguna_masuk'); + + if (!context.mounted) throw 'Terjadi kesalahan'; + + context.read().keluar(); + + Navigator.pushAndRemoveUntil( + context, + MaterialPageRoute(builder: (_) => const MyMasukPage()), + (_) => false, + ); + } catch (e) { + Navigator.pop(context); + + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text(e.toString()), + backgroundColor: Theme.of(context).colorScheme.primary, + action: SnackBarAction( + label: 'Tutup', + onPressed: () => + ScaffoldMessenger.of(context).hideCurrentSnackBar(), + ), + )); + } }, child: const Text('Keluar'), ), diff --git a/pubspec.lock b/pubspec.lock index 9218953..69725c2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -121,6 +121,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" + file: + dependency: transitive + description: + name: file + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" + url: "https://pub.dev" + source: hosted + version: "7.0.0" file_selector_linux: dependency: transitive description: @@ -568,6 +576,62 @@ packages: url: "https://pub.dev" source: hosted version: "4.1.0" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180 + url: "https://pub.dev" + source: hosted + version: "2.2.3" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "93d0ec9dd902d85f326068e6a899487d1f65ffcd5798721a95330b26c8131577" + url: "https://pub.dev" + source: hosted + version: "2.2.3" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "0a8a893bf4fd1152f93fec03a415d11c27c74454d96e2318a7ac38dd18683ab7" + url: "https://pub.dev" + source: hosted + version: "2.4.0" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a" + url: "https://pub.dev" + source: hosted + version: "2.3.0" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59" + url: "https://pub.dev" + source: hosted + version: "2.3.2" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 7648314..9a17a82 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -47,6 +47,7 @@ dependencies: image_picker: ^1.1.2 url_launcher: ^6.3.0 geolocator: ^12.0.0 + shared_preferences: ^2.2.3 dev_dependencies: flutter_test: