flutter 桌面应用程序不尊重对话框上的操作快捷方式

zqry0prt  于 2023-04-13  发布在  Flutter
关注(0)|答案(1)|浏览(96)

我正在使用一个flutter桌面应用程序,它有一个页面,依赖于很多快捷方式。在首页,它的工作奇迹。问题是当我打开一个对话框(页面有2个)。快捷方式不再起作用了。我试着在主页面构建和对话框构建时初始化对话框快捷方式,两个都不起作用。我还尝试了FocusableActionDetector,从快捷方式/动作方法中进行单次调用,没有任何运气。如果你需要我发布更多代码,请让我知道。OBS:我使用cubit来管理状态。

  • 页面构建
class VendaScreen extends StatelessWidget {
  const VendaScreen({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ActivityListener<VendaCubit, VendaState>(
      child: Shortcuts(
        shortcuts: VendaHelper().mainShortCuts(),
        child: Actions(
          actions: VendaHelper().mainActions(context),
          child: const ScaffoldWidget(
            appBar: AppBarWidget(title: 'Venda'),
            body: VendaScreenBody(),
          ),
        ),
      ),
    );
  }
}
  • 快捷方式和操作
Map<ShortcutActivator, Intent> mainShortCuts() {
    return {
      LogicalKeySet(LogicalKeyboardKey.f7): FocusCod(),
      LogicalKeySet(LogicalKeyboardKey.f6): FocusName(),
      LogicalKeySet(LogicalKeyboardKey.f8): FocusQtd(),
      LogicalKeySet(LogicalKeyboardKey.f2): CancelBuy(),
      LogicalKeySet(LogicalKeyboardKey.f10): EndVenda(),
      LogicalKeySet(LogicalKeyboardKey.enter): AddItem(),
      LogicalKeySet(LogicalKeyboardKey.tab): AddItem(),
    };
  }

  Map<Type, Action<Intent>> mainActions(BuildContext context) {
    final _cubit = context.read<VendaCubit>();
    return {
      FocusCod: CallbackAction<FocusCod>(
        onInvoke: (_) => _cubit.focusCodNode(),
      ),
      FocusName: CallbackAction<FocusName>(
        onInvoke: (_) => _cubit.focusNameNode(context),
      ),
      FocusQtd: CallbackAction<FocusQtd>(
        onInvoke: (_) => _cubit.focusQtdNode(),
      ),
      AddItem: CallbackAction<AddItem>(
        onInvoke: (_) => _cubit.addItem(context),
      ),
      CancelBuy: CallbackAction<CancelBuy>(
        onInvoke: (_) => _cubit.cancelVenda(),
      ),
      EndVenda: CallbackAction<EndVenda>(
          onInvoke: (_) => VendaEndDialog.show(context)),
    };
  }
  • 对话
class VendaEndDialog {
  static Future<void> show(BuildContext context) async {
    final _cubit = context.read<VendaCubit>();
    return await showDialog(
      context: context,
      builder: (context) {
        return BlocProvider<VendaCubit>.value(
          value: _cubit,
          child: const EndDialogWidget(),
        );
      },
    );
  }
}

class EndDialogWidget extends StatelessWidget {
  const EndDialogWidget({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return DialogWidget(
      size: DialogSize.lg,
      title: Messages.of('end.finalizar'),
      actions: [
        _return(context),
        _finalizarPedido(context),
        _finalizarNFCe(context),
      ],
      child: _body(context),
    );
  }
brgchamk

brgchamk1#

你应该使用焦点部件/类的Flutter的焦点上的孩子,你要实现你的快捷方式。
设置:

Focus(autofocus: true, child: WidgetUsedForShortcut),

参考:https://api.flutter.dev/flutter/widgets/Shortcuts-class.html
焦点、操作和快捷方式小部件/类是相对的。

相关问题