fix back button nav
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:ken_logger/ken_logger.dart';
|
||||||
import 'package:mzansi_innovation_hub/main.dart';
|
import 'package:mzansi_innovation_hub/main.dart';
|
||||||
|
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_scack_bar.dart';
|
||||||
import 'package:mzansi_innovation_hub/mih_packages/mih_home/components/mih_app_drawer.dart';
|
import 'package:mzansi_innovation_hub/mih_packages/mih_home/components/mih_app_drawer.dart';
|
||||||
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_package_tools.dart';
|
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_package_tools.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@@ -31,6 +35,7 @@ class _MihPackageState extends State<MihPackage>
|
|||||||
with SingleTickerProviderStateMixin {
|
with SingleTickerProviderStateMixin {
|
||||||
late PageController _pageController;
|
late PageController _pageController;
|
||||||
late AnimationController _animationController;
|
late AnimationController _animationController;
|
||||||
|
DateTime? lastPressedAt;
|
||||||
|
|
||||||
void unfocusAll() {
|
void unfocusAll() {
|
||||||
FocusScope.of(context).unfocus();
|
FocusScope.of(context).unfocus();
|
||||||
@@ -113,6 +118,32 @@ class _MihPackageState extends State<MihPackage>
|
|||||||
Size screenSize = MediaQuery.of(context).size;
|
Size screenSize = MediaQuery.of(context).size;
|
||||||
return GestureDetector(
|
return GestureDetector(
|
||||||
onTap: unfocusAll,
|
onTap: unfocusAll,
|
||||||
|
child: PopScope(
|
||||||
|
canPop: false,
|
||||||
|
onPopInvokedWithResult: (bool didPop, Object? result) {
|
||||||
|
if (GoRouterState.of(context).name == 'mihHome') {
|
||||||
|
if (lastPressedAt == null ||
|
||||||
|
DateTime.now().difference(lastPressedAt!) >
|
||||||
|
const Duration(seconds: 2)) {
|
||||||
|
// First press: show a message and update the timestamp.
|
||||||
|
lastPressedAt = DateTime.now();
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
MihSnackBar(
|
||||||
|
child: Text("Press back again to exit"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Second press within 2 seconds: exit the app.
|
||||||
|
KenLogger.warning('Exiting app...'); // Your custom logger
|
||||||
|
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
context.goNamed(
|
||||||
|
'mihHome',
|
||||||
|
extra: true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
child: Scaffold(
|
child: Scaffold(
|
||||||
drawer: widget.actionDrawer,
|
drawer: widget.actionDrawer,
|
||||||
body: SafeArea(
|
body: SafeArea(
|
||||||
@@ -180,6 +211,7 @@ class _MihPackageState extends State<MihPackage>
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
SnackBar MihSnackBar({
|
||||||
|
required Widget child,
|
||||||
|
}) {
|
||||||
|
return SnackBar(
|
||||||
|
content: child,
|
||||||
|
shape: StadiumBorder(),
|
||||||
|
behavior: SnackBarBehavior.floating,
|
||||||
|
duration: Duration(seconds: 2),
|
||||||
|
width: null,
|
||||||
|
action: SnackBarAction(
|
||||||
|
label: "Dismiss",
|
||||||
|
onPressed: () {},
|
||||||
|
),
|
||||||
|
// elevation: 30,
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user