volume_button_disabler 0.1.0 copy "volume_button_disabler: ^0.1.0" to clipboard
volume_button_disabler: ^0.1.0 copied to clipboard

PlatformAndroid

Flutter plugin to enable/disable hardware volume buttons on Android while your app is in the foreground.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:volume_button_disabler/volume_button_disabler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Volume Button Disabler Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
        useMaterial3: true,
      ),
      home: const VolumeControlDemoPage(),
    );
  }
}

class VolumeControlDemoPage extends StatefulWidget {
  const VolumeControlDemoPage({super.key});

  @override
  State<VolumeControlDemoPage> createState() => _VolumeControlDemoPageState();
}

class _VolumeControlDemoPageState extends State<VolumeControlDemoPage> {
  final VolumeButtonDisabler _disabler = VolumeButtonDisabler();
  bool _enabled = true;

  @override
  void initState() {
    super.initState();
    _initState();
  }

  Future<void> _initState() async {
    final current = await _disabler.areEnabled();
    if (!mounted) return;
    setState(() => _enabled = current);
  }

  Future<void> _setEnabled(bool value) async {
    await _disabler.setEnabled(value);
    if (!mounted) return;
    setState(() => _enabled = value);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Volume Button Disabler'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const Text(
              'Hardware volume buttons behavior',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 24),
            Card(
              elevation: 2,
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SwitchListTile(
                      title: const Text('Volume buttons enabled'),
                      subtitle: const Text(
                        'When OFF, volume up/down presses are blocked while this app '
                        'is in the foreground.',
                      ),
                      value: _enabled,
                      onChanged: (value) => _setEnabled(value),
                    ),
                    const SizedBox(height: 8),
                    const Text(
                      'Try pressing the hardware volume keys while this screen is '
                      'visible to see the effect.',
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 24),
            const Text(
              'Per‑screen control',
              style: TextStyle(fontWeight: FontWeight.w600),
            ),
            const SizedBox(height: 8),
            const Text(
              'In your own app you can call `VolumeButtonDisabler().disable()` in '
              '`initState` of a page where you want to lock the volume, and '
              '`VolumeButtonDisabler().enable()` in `dispose` to restore it.',
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
150
points
96
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter plugin to enable/disable hardware volume buttons on Android while your app is in the foreground.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on volume_button_disabler

Packages that implement volume_button_disabler