volume_button_disabler

Flutter plugin to enable or disable the hardware volume buttons on Android while your app is in the foreground. Useful for media, kiosk, and guided experiences where you want to prevent accidental volume changes on specific screens.

Platform support: Android only. Calls are no-ops on other platforms (not implemented).

Features

  • Toggle volume buttons on/off from Flutter.
  • Works while your Flutter activity is in the foreground.
  • Per-screen control (disable in initState, re-enable in dispose).

Requirements

  • Android: minSdk 24+ (tested with compileSdk 36).
  • Flutter: >=3.3.0.

Installation

dependencies:
  volume_button_disabler: ^0.1.0

Then run:

flutter pub get

Quick start

import 'package:volume_button_disabler/volume_button_disabler.dart';

final _disabler = VolumeButtonDisabler();

// Disable volume keys for this screen.
@override
void initState() {
  super.initState();
  _disabler.disable();
}

// Restore normal behavior when leaving.
@override
void dispose() {
  _disabler.enable();
  super.dispose();
}

Or toggle via a switch:

bool enabled = true;
final disabler = VolumeButtonDisabler();

Future<void> toggle(bool value) async {
  await disabler.setEnabled(value);
  enabled = value;
}

To check current state:

final isEnabled = await disabler.areEnabled();

Example app

See example/lib/main.dart for a runnable demo with a switch to enable/disable the hardware volume buttons and guidance on per-screen usage.

How it works (Android)

  • The plugin wraps the Activity Window.Callback and intercepts KEYCODE_VOLUME_UP, KEYCODE_VOLUME_DOWN, and KEYCODE_VOLUME_MUTE.
  • When disabled, these key events are consumed, so the system volume does not change while your app is visible.

Limitations

  • Scope is foreground-only. Android does not allow a normal app/plugin to block hardware keys when your app is in the background.
  • System/ROM shortcuts or OEM behaviors outside the standard key events may not be intercepted.

API

  • Future<void> disable() — Block hardware volume keys.
  • Future<void> enable() — Restore normal behavior.
  • Future<void> setEnabled(bool enabled) — Explicit toggle.
  • Future<bool> areEnabled() — Query current state.

Contributing

Issues and PRs are welcome. Please include device/Android version info when reporting behavior differences.