Skip to content

Commit

Permalink
Quit app by shortcut keys
Browse files Browse the repository at this point in the history
Signed-off-by: huynguyennovem <huynguyennovem@gmail.com>
  • Loading branch information
huynguyennovem committed Oct 8, 2023
1 parent 514bd98 commit 713852d
Showing 1 changed file with 104 additions and 36 deletions.
140 changes: 104 additions & 36 deletions lib/main.dart
@@ -1,5 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:go_router/go_router.dart';
import 'package:netshare/ui/common_view/confirm_dialog.dart';
import 'package:provider/provider.dart';

import 'package:netshare/config/styles.dart';
import 'package:netshare/di/di.dart';
import 'package:netshare/plugin_management/plugins.dart';
Expand All @@ -13,34 +18,40 @@ import 'package:netshare/ui/receive/receive_widget.dart';
import 'package:netshare/ui/send/send_widget.dart';
import 'package:netshare/ui/server/server_widget.dart';
import 'package:netshare/util/utility_functions.dart';
import 'package:provider/provider.dart';

import 'package:netshare/config/constants.dart';
import 'package:netshare/ui/send/uploading_widget.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initPlugins();
setupDI();
runApp(MyApp());
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();
}

MyApp({super.key});
class _MyAppState extends State<MyApp> {

final GoRouter _router = GoRouter(
navigatorKey: _navigatorKey,
errorBuilder: (BuildContext context, GoRouterState state) => ErrorWidget(state.error!),
routes: <GoRoute>[
GoRoute(
path: mRootPath,
redirect: (context, state) {
if(UtilityFunctions.isMobile) {
return '/$mClientPath';
} else {
return '/$mServerPath';
}
},
redirect: (context, state) {
if (UtilityFunctions.isMobile) {
return '/$mClientPath';
} else {
return '/$mServerPath';
}
},
),
GoRoute(
name: mNavigationPath,
Expand All @@ -56,33 +67,34 @@ class MyApp extends StatelessWidget {
name: mClientPath,
path: '/$mClientPath',
builder: (context, state) => const ClientWidget(),
routes: [
GoRoute(
name: mSendPath,
path: mSendPath,
builder: (BuildContext context, GoRouterState state) => const SendWidget(),
routes: [
GoRoute(
name: mUploadingPath,
path: mUploadingPath,
builder: (context, state) => const UploadingWidget(),
)
],
),
GoRoute(
name: mReceivePath,
path: mReceivePath,
builder: (BuildContext context, GoRouterState state) => const ReceiveWidget(),
),
GoRoute(
name: mScanningPath,
path: mScanningPath,
builder: (BuildContext context, GoRouterState state) => const ScanQRWidget(),
),
],
routes: [
GoRoute(
name: mSendPath,
path: mSendPath,
builder: (BuildContext context, GoRouterState state) => const SendWidget(),
routes: [
GoRoute(
name: mUploadingPath,
path: mUploadingPath,
builder: (context, state) => const UploadingWidget(),
)
],
),
GoRoute(
name: mReceivePath,
path: mReceivePath,
builder: (BuildContext context, GoRouterState state) => const ReceiveWidget(),
),
GoRoute(
name: mScanningPath,
path: mScanningPath,
builder: (BuildContext context, GoRouterState state) => const ScanQRWidget(),
),
],
),
],
);
bool _isKeyboardListenerEnabled = true;

@override
Widget build(BuildContext context) {
Expand All @@ -101,7 +113,63 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: seedColor, background: backgroundColor),
),
routerConfig: _router,
builder: (context, child) {
// Handle keyboard listener here
RawKeyboard.instance.addListener((RawKeyEvent value) => _handleKeyEvent(value));
return child ?? const SizedBox.shrink();
},
),
);
}

void _handleKeyEvent(RawKeyEvent value) async {
if (!_isKeyboardListenerEnabled) return;

// If user pressed Command/Control + W keys, quit the app
if (value.isMetaPressed && value.logicalKey == LogicalKeyboardKey.keyW ||
value.isControlPressed && value.logicalKey == LogicalKeyboardKey.keyW) {

if(_navigatorKey.currentContext == null) return;

// show confirm dialog
_showQuitAppConfirmationDialog(_navigatorKey.currentContext!, (confirmCallback) {
if (confirmCallback) {
SystemNavigator.pop(); // Quit the app
}
// listen keyboard again
_isKeyboardListenerEnabled = true;
});
}
}

void _showQuitAppConfirmationDialog(BuildContext context, Function(bool)? confirmCallback) {
// Disable the keyboard listener.
_isKeyboardListenerEnabled = false;

showDialog(
barrierDismissible: false,
context: context,
builder: (dialogContext) {
return ConfirmDialog(
dialogWidth: MediaQuery.of(dialogContext).size.width / 2,
header: Text(
'Quit App',
style: Theme.of(dialogContext).textTheme.headlineMedium?.copyWith(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
body: const Text(
'Are you sure you want to quit the app?',
textAlign: TextAlign.center,
),
cancelButtonTitle: 'No',
okButtonTitle: 'Yes, I\'m sure',
onCancel: () => confirmCallback?.call(false),
onConfirm: () => confirmCallback?.call(true),
);
},
);
}
}

0 comments on commit 713852d

Please sign in to comment.