hardware_buttons 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

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 #


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;

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

void dispose() {
  // be sure to cancel on dispose

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!

1.0.0 #

  • Android: Migrate to AndroidX (#33)

0.2.4 #

  • Android: Fix issue #39

0.2.3 #

  • iOS: Fix Volume detection bug on iPhone 10.3.3.

0.2.2 #

  • iOS: Migrating to Swift 5

0.2.1 #

  • Update README.md

0.2.0 #

  • Supports lock button events subscription.
  • iOS: Fix bug where home button event got called when lock button was pressed.

0.1.1 #

  • Update README.md
  • Document Public APIs

0.1.0 #

  • Update README.md

0.0.1 #

  • Initial release
  • Supports volume button and home button detection


import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hardware_buttons/hardware_buttons.dart' as HardwareButtons;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  _MyAppState createState() => _MyAppState();

class _MyAppState extends State<MyApp> {
  String _latestHardwareButtonEvent;

  StreamSubscription<HardwareButtons.VolumeButtonEvent> _volumeButtonSubscription;
  StreamSubscription<HardwareButtons.HomeButtonEvent> _homeButtonSubscription;
  StreamSubscription<HardwareButtons.LockButtonEvent> _lockButtonSubscription;

  void initState() {
    _volumeButtonSubscription = HardwareButtons.volumeButtonEvents.listen((event) {
      setState(() {
        _latestHardwareButtonEvent = event.toString();

    _homeButtonSubscription = HardwareButtons.homeButtonEvents.listen((event) {
      setState(() {
        _latestHardwareButtonEvent = 'HOME_BUTTON';

    _lockButtonSubscription = HardwareButtons.lockButtonEvents.listen((event) {
      setState(() {
        _latestHardwareButtonEvent = 'LOCK_BUTTON';

  void dispose() {

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text('Value: $_latestHardwareButtonEvent\n'),


Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  hardware_buttons: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:hardware_buttons/hardware_buttons.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:hardware_buttons/hardware_buttons.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:hardware_buttons/hardware_buttons.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:hardware_buttons/hardware_buttons.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:hardware_buttons/hardware_buttons.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [hardware_buttons] that is in a package requiring null.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies