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

插件详情v1.0.0

hardware_buttons

一个适用于 iOS 和 Android 的 Flutter 插件,用于检测各种硬件按钮,例如音量键和主页键。

近 30 天下载
50
喜欢数
49
Pub 分数
40

安装插件

flutter pub add hardware_buttons

插件介绍

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

hardware_buttons 包是 Flutter 插件,可检测 iOS 和 Android 设备上的物理硬件按钮,包括音量加/减和主页按钮。它提供了一个简单的 API 来监听按钮按下事件,使开发者能够响应应用内的硬件交互。这对于媒体控制、导航快捷方式或自定义设备特定行为尤其有用。该插件支持两个平台,并与 Flutter 的事件系统无缝集成。非常适合需要低级别硬件输入处理的应用程序。

适合场景

  • 媒体播放控制
  • 自定义导航手势
  • 设备特定的应用行为
  • 可访问性增强
  • 游戏控制器集成

主要特点

  • 实时按钮事件检测
  • 跨平台支持(iOS 和 Android)
  • 简单的事件监听器 API
  • 轻量且易于集成
  • 支持音量键和主页键

适合对象

  • 需要硬件交互的移动应用
  • 媒体播放器应用
  • 注重可访问性的应用
  • 使用物理控制的游戏
  • 需要设备特定逻辑的应用程序

使用建议

  • 仅限特定硬件按钮
  • 不支持第三方硬件
  • 需要平台特定权限
  • 可能不适用于所有设备型号
  • 事件处理依赖于操作系统级别的可用性

原始 README

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

DEPRECATED

This plugin is deprecated :( We're no longer able to maintain it.

hardware_buttons

Version GitHub license

A Flutter plugin for iOS and Android for detecting various hardware buttons.

Note: This plugin is still under development, and some APIs might not be available yet. Feedback and Pull Requests are most welcome!

Screenshots

screenshots

Features

  • Detect volume buttons
  • Detect home button
  • Detect lock(power) button
  • To be added...

Android specification

  • If you subscribe to volume button events, this plugin will inevitably request for ACTION_MANAGER_OVERLAY_PERMISSION, since we found out this was the only way to do it well in Flutter Plugin environment. We do show permission request window for you, so there's nothing you should do other than subscribing to the event channel. However, since users may be surprised why your app needs this permission, we suggest notifying users beforehand why this permission will be requested.
  • Migrated to AndroidX as of version 1.0.0. To use with original support libraries, use version 0.2.4. Note, however, pre-AndroidX will no longer be supported.

iOS specification

  • No VOLUME_DOWN events are emitted when the volume is already at its minimum. VOLUME_UP events vice versa. On the other hand, events always occur whenever user presses the button on Android.

Usage

Import the package

To use this plugin, follow the plugin installation instructions.

To use with AndroidX, install the latest version as above instructions. To use with original support libraries, install version 0.2.4 instead.

Use the plugin

Add the following import to your Dart code:

import 'package:hardware_buttons/hardware_buttons.dart';

In order to listen to volume button events, use volumeButtonEvents.listen as below:

StreamSubscription _volumeButtonSubscription;

@override
void initState() {
  super.initState();
  _volumeButtonSubscription = volumeButtonEvents.listen((VolumeButtonEvent event) {
    // do something
    // event is either VolumeButtonEvent.VOLUME_UP or VolumeButtonEvent.VOLUME_DOWN
  });
}

@override
void dispose() {
  super.dispose();
  // be sure to cancel on dispose
  _volumeButtonSubscription?.cancel();
}

Besides volume button events, there are also:

  1. Home button events, via homeButtonEvents.listen.
  2. Lock button events, via lockButtonEvents.listen.

Example

Find the example wiring in the example app.

API details

See the hardware_buttons.dart for more API details.

Issues and feedback

Please file issues to send feedback or report a bug. Thank you!

所属分类

源码仓库

使用此插件的项目

暂无相关数据。