repair back button

This commit is contained in:
kicap
2023-08-10 01:21:43 +08:00
parent 696c52efeb
commit 6c5bfde828
29 changed files with 956 additions and 845 deletions

View File

@ -1,5 +1,5 @@
import 'package:reza_app/ui/views/meja_detail/meja_detail_view.dart';
import 'package:reza_app/ui/views/user_ui/pesanan_list/keranjang_saya/keranjang_saya_view.dart';
import 'package:reza_app/services/global_var.dart';
import 'package:reza_app/services/other_function.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:stacked/stacked_annotations.dart';
@ -9,10 +9,12 @@ import '../ui/views/daftar_user_ui/input_informasi_diri/input_informasi_diri_vie
import '../ui/views/daftar_user_ui/masukan_no_hp/masukan_no_hp_view.dart';
import '../ui/views/daftar_user_ui/verifikasi_no_hp/verifikasi_no_hp_view.dart';
import '../ui/views/login_user/login_user_view.dart';
import '../ui/views/meja_detail/meja_detail_view.dart';
import '../ui/views/splash_screen/splash_screen_view.dart';
import '../ui/views/user_ui/akun_user/akun_user_view.dart';
import '../ui/views/user_ui/makanan_list/detail_makanan/detail_makanan_view.dart';
import '../ui/views/user_ui/makanan_list/makanan_list_view.dart';
import '../ui/views/user_ui/pesanan_list/keranjang_saya/keranjang_saya_view.dart';
import '../ui/views/user_ui/pesanan_list/pesanan_list_view.dart';
import '../ui/views/user_ui/reservasi_meja/reservasi_meja_view.dart';
import '../ui/views/user_ui/user_index_tracking/user_index_tracking_view.dart';
@ -50,6 +52,8 @@ import '../ui/views/user_ui/user_index_tracking/user_index_tracking_view.dart';
LazySingleton(classType: MyEasyLoading),
LazySingleton(classType: MyHttpServices),
LazySingleton(classType: OtherFunction),
LazySingleton(classType: GlobalVar),
],
logger: StackedLogger(),
)

View File

@ -12,8 +12,10 @@ import 'package:stacked_services/src/navigation/navigation_service.dart';
import 'package:stacked_services/src/snackbar/snackbar_service.dart';
import 'package:stacked_shared/stacked_shared.dart';
import '../services/global_var.dart';
import '../services/http_services.dart';
import '../services/my_easyloading.dart';
import '../services/other_function.dart';
final locator = StackedLocator.instance;
@ -32,4 +34,6 @@ Future<void> setupLocator({
locator.registerLazySingleton(() => BottomSheetService());
locator.registerLazySingleton(() => MyEasyLoading());
locator.registerLazySingleton(() => MyHttpServices());
locator.registerLazySingleton(() => OtherFunction());
locator.registerLazySingleton(() => GlobalVar());
}

View File

@ -3,8 +3,10 @@ import 'package:flutter/services.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import '../../services/global_var.dart';
import '../../services/http_services.dart';
import '../../services/my_easyloading.dart';
import '../../services/other_function.dart';
import '../app.locator.dart';
import '../themes/app_colors.dart';
@ -15,6 +17,8 @@ class CustomBaseViewModel extends BaseViewModel {
final snackbarService = locator<SnackbarService>();
final easyLoading = locator<MyEasyLoading>();
final httpService = locator<MyHttpServices>();
final otherFunction = locator<OtherFunction>();
final globalVar = locator<GlobalVar>();
// late bool backPressed;

View File

@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Panti Asuhan Aisyiyah Abadi',
title: 'Reza Restaurant App',
theme: appTheme,
debugShowCheckedModeBanner: false,
navigatorKey: StackedService.navigatorKey,

View File

@ -0,0 +1,3 @@
class GlobalVar {
String backPressed = 'backNormal';
}

View File

@ -1,8 +1,6 @@
import 'package:flutter_easyloading/flutter_easyloading.dart';
class MyEasyLoading {
int backPressed = 1;
showLoading() {
EasyLoading.show(
status: 'loading...',

View File

@ -40,7 +40,7 @@ class InputInformasiDiriView extends StatelessWidget {
),
body: WillPopScope(
onWillPop: () async {
if (model.easyLoading.backPressed == 1) {
if (model.globalVar.backPressed == 'backNormal') {
model.navigationService.navigateToMasukanNoHpView();
}
return false;

View File

@ -33,13 +33,13 @@ class InputInformasiDiriViewModel extends CustomBaseViewModel {
}
goToLogin() async {
easyLoading.backPressed = 0;
globalVar.backPressed = 'cantBack';
easyLoading.customLoading("Mendaftarkan Akun Anda");
await Future.delayed(const Duration(seconds: 2));
easyLoading.customLoading("Ke Halaman Login");
await Future.delayed(const Duration(seconds: 2));
easyLoading.dismissLoading();
easyLoading.backPressed = 1;
globalVar.backPressed = 'backNormal';
notifyListeners();
await navigationService.navigateToLoginUserView();
}

View File

@ -36,7 +36,7 @@ class MasukanNoHpView extends StatelessWidget {
),
body: WillPopScope(
onWillPop: () async {
if (model.easyLoading.backPressed == 1) {
if (model.globalVar.backPressed == 'backNormal') {
model.navigationService.navigateToLoginUserView();
}
return false;

View File

@ -16,9 +16,9 @@ class MasukanNoHpViewModel extends CustomBaseViewModel {
selanjutnya() async {
_easyloading.customLoading("Menghantar Kode OTP \nke WhatsApp Anda");
easyLoading.backPressed = 0;
globalVar.backPressed = 'cantBack';
await Future.delayed(const Duration(seconds: 3));
easyLoading.backPressed = 1;
globalVar.backPressed = 'backNormal';
notifyListeners();
_easyloading.dismissLoading();
await navigationService.navigateToVerifikasiNoHpView();

View File

@ -36,7 +36,7 @@ class VerifikasiNoHpView extends StatelessWidget {
),
body: WillPopScope(
onWillPop: () async {
if (model.easyLoading.backPressed == 1) {
if (model.globalVar.backPressed == 'backNormal') {
model.back();
}
return false;

View File

@ -9,11 +9,11 @@ class VerifikasiNoHpViewModel extends CustomBaseViewModel {
Future<void> init() async {}
goToInputInformasiDiri() async {
easyLoading.backPressed = 0;
globalVar.backPressed = 'cantBack';
easyLoading.customLoading("Ke Halaman Input Informasi Diri");
await Future.delayed(const Duration(seconds: 3));
easyLoading.dismissLoading();
easyLoading.backPressed = 1;
globalVar.backPressed = 'backNormal';
notifyListeners();
await navigationService.navigateToInputInformasiDiriView(
noHp: "082293246583",

View File

@ -23,7 +23,7 @@ class LoginUserView extends StatelessWidget {
return Scaffold(
body: WillPopScope(
onWillPop: () async {
if (model.easyLoading.backPressed == 1) {
if (model.globalVar.backPressed == 'backNormal') {
// model.back();
model.quitApp(context);
}

View File

@ -17,12 +17,12 @@ class LoginUserViewModel extends CustomBaseViewModel {
login() async {
setBusy(true);
easyLoading.backPressed = 0;
globalVar.backPressed = 'cantBack';
easyLoading.showLoading();
await Future.delayed(const Duration(seconds: 5));
easyLoading.dismissLoading();
setBusy(false);
easyLoading.backPressed = 1;
globalVar.backPressed = 'backNormal';
notifyListeners();
await navigationService.navigateToUserIndexTrackingView();
}

View File

@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:reza_app/app/app.router.dart';
import 'package:reza_app/app/themes/app_colors.dart';
import 'package:reza_app/app/themes/app_text.dart';
import 'package:stacked/stacked.dart';
@ -26,19 +25,35 @@ class MejaDetailView extends StatelessWidget {
MejaDetailViewModel model,
Widget? child,
) {
return Scaffold(
return WillPopScope(
onWillPop: () async {
if (model.globalVar.backPressed == 'backNormal') {
model.globalVar.backPressed = 'exitApp';
return true;
}
// model.quitApp(context);
return false;
},
child: Scaffold(
appBar: AppBar(
title: const Text('MejaDetailView',
style: TextStyle(color: Colors.white)),
backgroundColor: mainColor,
leading: IconButton(
onPressed: () => model.navigationService.popUntil((route) =>
route.settings.name == Routes.userIndexTrackingView),
onPressed: () {
if (model.globalVar.backPressed == 'backNormal') {
model.globalVar.backPressed = 'exitApp';
model.navigationService.back();
// return true;
}
},
icon: const Icon(Icons.arrow_back, color: Colors.white),
),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10),
padding:
const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
@ -139,6 +154,7 @@ class MejaDetailView extends StatelessWidget {
floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
floatingActionButtonLocation:
FloatingActionButtonLocation.centerFloat,
),
);
},
);

View File

@ -13,7 +13,7 @@ class MejaDetailViewModel extends CustomBaseViewModel {
log.i('MejaDetailViewModel init');
log.i('mejaId : $mejaId');
this.mejaId = mejaId;
easyLoading.backPressed = 3;
globalVar.backPressed = 'backNormal';
// seperate the number from the string
var number = int.parse(mejaId.replaceAll(RegExp(r'[^0-9]'), ''));
// log.i('number : $number');

View File

@ -20,7 +20,17 @@ class AkunUserView extends StatelessWidget {
AkunUserViewModel model,
Widget? child,
) {
return Scaffold(
return WillPopScope(
onWillPop: () async {
// model.log.i('onWillPop di akun_user_view.dart');
if (model.globalVar.backPressed == 'exitApp') {
model.quitApp(context);
model.globalVar.backPressed = 'exitApp';
return false;
}
return false;
},
child: Scaffold(
appBar: AppBar(
title: Text(
'Akun Saya',
@ -126,6 +136,7 @@ class AkunUserView extends StatelessWidget {
],
),
),
),
);
},
);

View File

@ -2,6 +2,6 @@ import 'package:reza_app/app/core/custom_base_view_model.dart';
class AkunUserViewModel extends CustomBaseViewModel {
Future<void> init() async {
easyLoading.backPressed = 2;
globalVar.backPressed = 'exitApp';
}
}

View File

@ -28,7 +28,17 @@ class DetailMakananView extends HookWidget {
DetailMakananViewModel model,
Widget? child,
) {
return Scaffold(
return WillPopScope(
onWillPop: () async {
// model.log.i('onWillPop di reservasi_meja_view.dart');
// if (model.globalVar.backPressed == 'exitApp') {
model.globalVar.backPressed = 'exitApp';
model.back();
return false;
// }
// return false;
},
child: Scaffold(
backgroundColor: backgroundColor,
body: SizedBox(
width: MediaQuery.of(context).size.width,
@ -203,7 +213,9 @@ class DetailMakananView extends HookWidget {
),
],
),
));
),
),
);
},
);
}
@ -257,6 +269,7 @@ class TopBarWidget extends ViewModelWidget<DetailMakananViewModel> {
),
child: IconButton(
onPressed: () {
viewModel.globalVar.backPressed = 'exitApp';
viewModel.back();
},
icon: const Icon(

View File

@ -5,6 +5,6 @@ class DetailMakananViewModel extends CustomBaseViewModel {
final log = getLogger('DetailMakananViewModel');
Future<void> init() async {
easyLoading.backPressed = 3;
globalVar.backPressed = 'backNormal';
}
}

View File

@ -21,7 +21,17 @@ class MakananListView extends StatelessWidget {
MakananListViewModel model,
Widget? child,
) {
return Scaffold(
return WillPopScope(
onWillPop: () async {
// model.log.i('onWillPop di reservasi_meja_view.dart');
if (model.globalVar.backPressed == 'exitApp') {
model.quitApp(context);
model.globalVar.backPressed = 'exitApp';
return false;
}
return false;
},
child: Scaffold(
backgroundColor: backgroundColor,
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
@ -38,7 +48,14 @@ class MakananListView extends StatelessWidget {
Expanded(
flex: 2,
child: IconButton(
onPressed: () => model.back(),
onPressed: () {
if (model.globalVar.backPressed == 'exitApp') {
model.quitApp(context);
model.globalVar.backPressed = 'exitApp';
} else {
model.navigationService.back();
}
},
icon: const Icon(Icons.arrow_back),
),
),
@ -230,7 +247,9 @@ class MakananListView extends StatelessWidget {
],
),
),
));
),
),
);
},
);
}

View File

@ -5,7 +5,7 @@ import '../../../../app/core/custom_base_view_model.dart';
class MakananListViewModel extends CustomBaseViewModel {
final log = getLogger('MakananListViewModel');
Future<void> init() async {
easyLoading.backPressed = 2;
globalVar.backPressed = 'exitApp';
}
goToDetailMakanan() {

View File

@ -21,7 +21,17 @@ class KeranjangSayaView extends StatelessWidget {
KeranjangSayaViewModel model,
Widget? child,
) {
return Scaffold(
return WillPopScope(
onWillPop: () async {
// model.log.i('onWillPop di reservasi_meja_view.dart');
if (model.globalVar.backPressed == 'backNormal') {
// model.quitApp(context);
model.globalVar.backPressed = 'exitApp';
return true;
}
return false;
},
child: Scaffold(
appBar: AppBar(
title: const Text('Keranjang Saya',
style: TextStyle(color: Colors.white)),
@ -67,7 +77,8 @@ class KeranjangSayaView extends StatelessWidget {
height: 150,
width: double.infinity,
fit: BoxFit.fill,
errorBuilder: (context, error, stackTrace) {
errorBuilder:
(context, error, stackTrace) {
return const Icon(Icons.error);
},
),
@ -77,7 +88,8 @@ class KeranjangSayaView extends StatelessWidget {
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
@ -165,7 +177,8 @@ class KeranjangSayaView extends StatelessWidget {
Icons.shopping_cart_outlined,
color: Colors.white,
),
));
)),
);
},
);
}

View File

@ -5,6 +5,6 @@ import '../../../../../app/app.logger.dart';
class KeranjangSayaViewModel extends CustomBaseViewModel {
final log = getLogger('KeranjangSayaViewModel');
Future<void> init() async {
easyLoading.backPressed = 3;
globalVar.backPressed = 'backNormal';
}
}

View File

@ -22,7 +22,17 @@ class PesananListView extends StatelessWidget {
PesananListViewModel model,
Widget? child,
) {
return Scaffold(
return WillPopScope(
onWillPop: () async {
// model.log.i('onWillPop di reservasi_meja_view.dart');
if (model.globalVar.backPressed == 'exitApp') {
model.quitApp(context);
model.globalVar.backPressed = 'exitApp';
return false;
}
return false;
},
child: Scaffold(
appBar: AppBar(
title: Text(
'List Pesanan',
@ -173,7 +183,8 @@ class PesananListView extends StatelessWidget {
Expanded(
child: Text(
'Nama Makanan',
style: regularTextStyle.copyWith(
style:
regularTextStyle.copyWith(
fontSize: 17,
),
overflow: TextOverflow.ellipsis,
@ -186,7 +197,8 @@ class PesananListView extends StatelessWidget {
alignment: Alignment.centerRight,
child: Text(
'x 1',
style: regularTextStyle.copyWith(
style:
regularTextStyle.copyWith(
color: mainGrey,
fontSize: 17,
),
@ -199,7 +211,8 @@ class PesananListView extends StatelessWidget {
alignment: Alignment.centerRight,
child: Text(
'Rp. 100.000',
style: regularTextStyle.copyWith(
style:
regularTextStyle.copyWith(
color: redColor,
fontSize: 17,
),
@ -303,6 +316,7 @@ class PesananListView extends StatelessWidget {
// ),
// backgroundColor: mainColor,
// ),
),
);
},
);

View File

@ -2,6 +2,6 @@ import 'package:reza_app/app/core/custom_base_view_model.dart';
class PesananListViewModel extends CustomBaseViewModel {
Future<void> init() async {
easyLoading.backPressed = 2;
globalVar.backPressed = 'exitApp';
}
}

View File

@ -23,7 +23,17 @@ class ReservasiMejaView extends StatelessWidget {
ReservasiMejaViewModel model,
Widget? child,
) {
return Scaffold(
return WillPopScope(
onWillPop: () async {
// model.log.i('onWillPop di reservasi_meja_view.dart');
if (model.globalVar.backPressed == 'exitApp') {
model.quitApp(context);
model.globalVar.backPressed = 'exitApp';
return false;
}
return false;
},
child: Scaffold(
appBar: AppBar(
title: const Text('RESERVASI MEJA',
style: TextStyle(color: Colors.white)),
@ -75,7 +85,8 @@ class ReservasiMejaView extends StatelessWidget {
),
CarouselSlider.builder(
itemCount: model.imagePaths.length,
itemBuilder: (BuildContext context, int index, int realIndex) {
itemBuilder:
(BuildContext context, int index, int realIndex) {
return SizedBox(
width: MediaQuery.of(context)
.size
@ -105,7 +116,9 @@ class ReservasiMejaView extends StatelessWidget {
// model.controllerCompleter.complete(webViewController);
},
onProgress: (int progress) {
model.log.i('WebView is loading (progress : $progress%)');
// model.log.i('WebView is loading (progress : $progress%)');
// show a loading indicator when the WebView is loading using circular progress indicator
model.easyLoading.customLoading('Loading Data Meja');
},
// javascriptChannels: <JavascriptChannel>{
// _toasterJavascriptChannel(context),
@ -115,10 +128,10 @@ class ReservasiMejaView extends StatelessWidget {
name: 'messageHandler',
onMessageReceived: (JavascriptMessage message) async {
// model.log.i('messageHandler : ${message.message}');
model.easyLoading.backPressed = 1;
// model.easyLoading.backPressed = 1;
model.notifyListeners();
model.log.i(
'backPressed : ${model.easyLoading.backPressed}');
// model.log.i(
// 'backPressed : ${model.easyLoading.backPressed}');
String mejaId = message.message;
model.navigationService.navigateToView(
MejaDetailView(
@ -150,6 +163,7 @@ class ReservasiMejaView extends StatelessWidget {
},
onPageFinished: (String url) {
model.log.i('Page finished loading: $url');
model.easyLoading.dismissLoading();
},
gestureNavigationEnabled: true,
backgroundColor: const Color(0x00000000),
@ -201,6 +215,7 @@ class ReservasiMejaView extends StatelessWidget {
),
],
),
),
);
},
);

View File

@ -13,6 +13,6 @@ class ReservasiMejaViewModel extends CustomBaseViewModel {
];
Future<void> init() async {
easyLoading.backPressed = 2;
globalVar.backPressed = 'exitApp';
}
}

View File

@ -1,52 +1,49 @@
import 'package:back_button_interceptor/back_button_interceptor.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:reza_app/services/my_easyloading.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import '../../../../app/app.locator.dart';
import '../../../../app/app.logger.dart';
import '../../../../app/app.router.dart';
import '../../../../app/themes/app_colors.dart';
import '../../../../services/global_var.dart';
class UserIndexTrackingViewModel extends IndexTrackingViewModel {
final log = getLogger('UserIndexTrackingViewModel');
final navigationService = locator<NavigationService>();
final easyLoading = locator<MyEasyLoading>();
final globalVar = locator<GlobalVar>();
// late bool backPressed;
Future<void> init() async {
BackButtonInterceptor.add(myInterceptor);
// BackButtonInterceptor.add(myInterceptor);
// backPressed = easyLoading.backPressed;
easyLoading.backPressed = 2;
globalVar.backPressed = 'exitApp';
}
bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
// print("BACK BUTTON!"); // Do some stuff.
// bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
// // print("BACK BUTTON!"); // Do some stuff.
log.i('myInterceptor : ${easyLoading.backPressed}');
// log.i('myInterceptor : ${easyLoading.backPressed}');
if (easyLoading.backPressed == 2) {
quitApp(null);
}
// if (easyLoading.backPressed == 2) {
// quitApp(null);
// }
if (easyLoading.backPressed == 3) {
// if (easyLoading.backPressed == 3) {
// // easyLoading.backPressed = 2;
// // notifyListeners();
// // navigationService.back();
// easyLoading.backPressed = 2;
// notifyListeners();
// navigationService.back();
easyLoading.backPressed = 2;
return false;
}
// return false;
// }
if (easyLoading.backPressed == 1) {
// easyLoading.backPressed = 2;
// notifyListeners();
// navigationService.back();
return false;
}
// if (easyLoading.backPressed == 1) {
// // easyLoading.backPressed = 2;
// // notifyListeners();
// // navigationService.back();
// return false;
// }
return true;
}
// return true;
// }
final _bottomNavBarList = [
{
@ -95,37 +92,37 @@ class UserIndexTrackingViewModel extends IndexTrackingViewModel {
);
}
quitApp(BuildContext? context) {
easyLoading.backPressed = 0;
showDialog(
context: context ?? StackedService.navigatorKey!.currentContext!,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Keluar'),
content: const Text('Apakah Anda yakin ingin keluar?'),
actions: [
TextButton(
onPressed: () {
easyLoading.backPressed = 2;
Navigator.of(context).pop(false);
},
child: const Text('Batal'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text(
'Keluar',
style: TextStyle(color: dangerColor),
),
),
],
);
},
).then((value) {
if (value == true) {
SystemNavigator.pop();
}
});
notifyListeners();
}
// quitApp(BuildContext? context) {
// easyLoading.backPressed = 0;
// showDialog(
// context: context ?? StackedService.navigatorKey!.currentContext!,
// builder: (BuildContext context) {
// return AlertDialog(
// title: const Text('Keluar'),
// content: const Text('Apakah Anda yakin ingin keluar?'),
// actions: [
// TextButton(
// onPressed: () {
// easyLoading.backPressed = 2;
// Navigator.of(context).pop(false);
// },
// child: const Text('Batal'),
// ),
// TextButton(
// onPressed: () => Navigator.of(context).pop(true),
// child: const Text(
// 'Keluar',
// style: TextStyle(color: dangerColor),
// ),
// ),
// ],
// );
// },
// ).then((value) {
// if (value == true) {
// SystemNavigator.pop();
// }
// });
// notifyListeners();
// }
}