hardware_button_listener 0.0.1 copy "hardware_button_listener: ^0.0.1" to clipboard
hardware_button_listener: ^0.0.1 copied to clipboard

PlatformAndroid

A Flutter package that allows you to listen to physical hardware button presses on Android devices, providing key information like button name and key code through a reactive stream-based API.

Hardware Button Listener #

Hardware Button Listener is a Flutter package that allows you to listen to physical hardware button presses on Android devices, such as volume buttons or other device-specific keys like devices with attached QRCODE read button.

Features #

  • Detect physical hardware button presses.
  • Provides key information, such as button name and key code.
  • Easy-to-use API with a reactive stream-based approach.

Installation #

Add the following dependency to your pubspec.yaml file:

dependencies:
  hardware_button_listener: latest_version

Run the command to fetch the package:

flutter pub get

Usage #

Here's an example of how to use the Hardware Button Listener package:

Full Example #


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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _lastButtonPressed = 'No button pressed yet';

  final _hardwareButtonListener = HardwareButtonListener();
  late StreamSubscription<HardwareButton> _buttonSubscription;

  @override
  void initState() {
    super.initState();
    // Start listening for hardware button events
    startListeningToHardwareButtons();
  }

  @override
  void dispose() {
    // Cancel the subscription to free up resources
    _buttonSubscription.cancel();
    super.dispose();
  }

  // Listen for hardware button events and update the UI
  void startListeningToHardwareButtons() {
    _buttonSubscription = _hardwareButtonListener.listen((event) {
      log(event.buttonKey.toString());
      log(event.buttonName.toString());
      setState(() {
        _lastButtonPressed = event.buttonKey.toString();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Hardware Button Listener Example'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const SizedBox(height: 20),
              const Text(
                'Last button pressed:',
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 10),
              Text(
                _lastButtonPressed,
                style: const TextStyle(fontSize: 16, color: Colors.blueAccent),
                textAlign: TextAlign.center,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Key Concepts #

  • HardwareButtonListener: The main class that provides a stream for listening to hardware button events.
  • HardwareButton: A model representing the button press event, containing properties like buttonKey and buttonName.

Handling Events #

Use the listen method of HardwareButtonListener to subscribe to button press events and handle them as needed.

_hardwareButtonListener.listen((event) {
  print('Button Key: ${event.buttonKey}');
  print('Button Name: ${event.buttonName}');
  if(event.buttonKey == 120){
  doSomething();
  }
});

Important Notes #

  • This package currently supports Android only.
  • Ensure proper permissions and configurations are set if required by your specific device.

Contributions #

Contributions are welcome! Feel free to open issues or submit pull requests on the GitHub repository.

2
likes
150
points
55
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package that allows you to listen to physical hardware button presses on Android devices, providing key information like button name and key code through a reactive stream-based API.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on hardware_button_listener