搜索插件、代码仓与 APP 分类

插件详情v1.2.0

focusable_control_builder

轻松创建可正确响应鼠标和键盘的自定义控件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1720
喜欢数
52
Pub 分数
140

安装插件

flutter pub add focusable_control_builder

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,可简化自定义 UI 控件的创建,并正确处理键盘和鼠标焦点。它提供了一种构建器模式,以在不同平台上一致地管理焦点状态、可访问性和输入事件。开发者可以轻松实现按钮、滑块或表单字段等交互元素,这些元素能正确响应标签导航、键盘快捷键和指针交互。该包确保辅助技术和依赖非触摸输入方式的用户获得无缝的用户体验。

适合场景

  • 创建可访问的表单输入
  • 构建自定义交互式小部件
  • 在复杂 UI 中启用键盘导航
  • 支持屏幕阅读器和辅助技术
  • 在应用程序中实现选项卡导航
  • 为桌面和网络设计响应式控件

主要特点

  • 内置焦点状态管理
  • 键盘事件处理
  • 鼠标指针集成
  • 跨平台一致性
  • 无障碍设计
  • 简单的构建器 API

适合对象

  • 开发无障碍应用的开发者
  • 面向桌面和网络的 Flutter 项目
  • 注重键盘用户体验的团队
  • 自定义 widget 创建者
  • 需要完整键盘导航的应用程序

使用建议

  • 需要理解焦点语义
  • 可能需要额外的样式以提供视觉反馈
  • 最好与语义化 UI 模式一起使用
  • 对于仅支持触摸的简单界面来说并非必需
  • 与现有焦点树的集成可能需要谨慎处理

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

focusable_control_builder

Using FocusableControlBuilder you can quickly create a new control that will work properly on all platforms and input devices.

Working "properly" entails support for:

  • tab key traversal
  • focus / rollover states
  • modified mouse cursor
  • support for keyboard shortcuts (Enter/Space by default)

Under the hood, this builder wraps the built in FocusableActionDetector and you can read there for more details about how it all works.

For more information on why you'd use these widgets, see this blog post: https://blog.gskinner.com/archives/2021/06/flutter-building-custom-components-with-focusableactiondetector.html

🔨 Installation

$ flutter pub add focusable_control_builder
⚙ Import
import 'package:focusable_control_builder/focusable_control_builder.dart';

🕹️ Usage

To create a custom button just implement the FocusableControlBuilder.builder method and assign an onPressed handler:

class MyCustomButton extends StatelessWidget {
  const MyCustomButton(this.label, {Key? key, required this.onPressed}) : super(key: key);
  final String label;
  final VoidCallback onPressed;
  @override
  Widget build(BuildContext context) {
    return FocusableControlBuilder(
        onPressed: onPressed,
        builder: (_, FocusableControlState control) {
          // Decide which colors to use, based on .isFocused and .isHovered
          Color outlineColor = control.isFocused ? Colors.black : Colors.transparent;
          Color bgColor = control.isHovered ? Colors.blue.shade100 : Colors.grey.shade200;
          // Return custom button contents
          return Container(
            padding: const EdgeInsets.all(8),
            child: Text(label),
            decoration: BoxDecoration(
              color: bgColor,
              border: Border.all(color: outlineColor, width: 1),
            ),
          );
        });
  }
}
Custom Mouse Cursor

FocusableControlBuilder will assume you want a "hand" cursor for your control, which is typically the case. If you'd like a different cursor, just assign it:

return FocusableControlBuilder(
    cursor: SystemMouseCursors.resizeDown,
    ...
);
Request Focus On Press

FocusableControlBuilder will assume you want your control to request focus when it is pressed. To disable, just set this to false:

return FocusableControlBuilder(
    requestFocusOnPress: false,
    ...
);
Custom Shortcuts

FocusableControlBuilder will create a default activation action which handles [Submit] and [Enter] keys. To create custom keyboard shortcuts you can set the .shortcuts and .actions parameters. This is a bit beyond the scope of this doc, but if you'd like to learn how, checkout the FocusableActionDetector docs which contain an example.

🐞 Bugs/Requests

If you encounter any problems please open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are welcome.

📃 License

MIT License

所属分类

源码仓库

使用此插件的项目

暂无相关数据。