Detect Volume key pressed with flutter

Viewed 910

Hey I want to be able to take pictures when the user pressed on the volume down button, like in many devices.

I am using the Flutter Camera plugin and I can take pictures through buttons that I place on the screen but I can't detect physical button clicks.

I tried to use the Hardware buttons plugin but it seems that it's deprecated, and there is no support for the plugin anymore.

2 Answers

There is no such much-upgraded plugin now, which can help you. But for a workaround, we do have volume_watcher: ^2.0.1, which gives a callback when the volume is changed.

VolumeWatcher.addListener((volume) {
        print("Current Volume :" + volume.toString());
    })!;

Note: Volume carries from 0 to 1, where 0 means no volume and 1 means max volume.

I needed the same functionality (listen to volume down, don't change volume when listening) and it didn't exist yet in Flutter so I made a plugin for it myself, you can find it here: https://pub.dev/packages/flutter_android_volume_keydown

It only works on Android because overriding iOS hardware buttons is not allowed by the app store guidelines.

Related