From 69129c91347d5ab56454137adc8c6646e0534f17 Mon Sep 17 00:00:00 2001 From: Erick Date: Sun, 10 Mar 2024 11:26:55 -0300 Subject: [PATCH] feat: Close dialogs on esc --- CHANGELOG.md | 1 + lib/src/widgets/dialogs/nes_dialog.dart | 83 ++++++++++++++++--------- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1703987..2474bd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 0.19.0 - feat: adding `NesSectionHeader` - feat: adding `NesIcons16.check` + - feat: Dialogs close on ESC key. # 0.18.0 - feat: add `NesIcons.wrench` diff --git a/lib/src/widgets/dialogs/nes_dialog.dart b/lib/src/widgets/dialogs/nes_dialog.dart index 2b7f5c6..ffc00ff 100644 --- a/lib/src/widgets/dialogs/nes_dialog.dart +++ b/lib/src/widgets/dialogs/nes_dialog.dart @@ -1,6 +1,11 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:nes_ui/nes_ui.dart'; +class _CloseDialogIntent extends Intent { + const _CloseDialogIntent(); +} + /// {@template nes_confirm_dialog} /// A dialog that shows a child. /// @@ -63,38 +68,56 @@ class NesDialog extends StatelessWidget { @override Widget build(BuildContext context) { - return Align( - child: Material( - color: Colors.transparent, - child: IntrinsicWidth( - stepHeight: 0.56, - child: SizedBox.expand( - child: Stack( - clipBehavior: Clip.none, - children: [ - NesContainer( - child: Padding( - padding: const EdgeInsets.all(16), - child: Center( - child: child, - ), - ), - ), - Positioned( - right: -8, - top: -8, - child: NesButton( - type: NesButtonType.error, - onPressed: () { - Navigator.of(context).pop(); - }, - child: NesIcon( - size: const Size(16, 16), - iconData: NesIcons.close, - ), + return Shortcuts( + shortcuts: { + LogicalKeySet(LogicalKeyboardKey.escape): const _CloseDialogIntent(), + }, + child: Actions( + actions: >{ + _CloseDialogIntent: CallbackAction<_CloseDialogIntent>( + onInvoke: (intent) { + Navigator.of(context).pop(); + return null; + }, + ), + }, + child: Focus( + autofocus: true, + child: Align( + child: Material( + color: Colors.transparent, + child: IntrinsicWidth( + stepHeight: 0.56, + child: SizedBox.expand( + child: Stack( + clipBehavior: Clip.none, + children: [ + NesContainer( + child: Padding( + padding: const EdgeInsets.all(16), + child: Center( + child: child, + ), + ), + ), + Positioned( + right: -8, + top: -8, + child: NesButton( + type: NesButtonType.error, + onPressed: () { + Navigator.of(context).pop(); + }, + child: NesIcon( + size: const Size(16, 16), + iconData: NesIcons.close, + ), + ), + ), + ], ), ), - ], + ), ), ), ),