volume_button_disabler 0.1.0
volume_button_disabler: ^0.1.0 copied to clipboard
Flutter plugin to enable/disable hardware volume buttons on Android while your app is in the foreground.
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.',
),
],
),
),
);
}
}