hardware_buttons 0.2.0 hardware_buttons: ^0.2.0 copied to clipboard
A Flutter plugin for iOS and Android for detecting various hardware buttons such as volume and home button.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:hardware_buttons/hardware_buttons.dart' as HardwareButtons;
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _latestHardwareButtonEvent;
StreamSubscription<HardwareButtons.VolumeButtonEvent> _volumeButtonSubscription;
StreamSubscription<HardwareButtons.HomeButtonEvent> _homeButtonSubscription;
StreamSubscription<HardwareButtons.LockButtonEvent> _lockButtonSubscription;
@override
void initState() {
super.initState();
_volumeButtonSubscription = HardwareButtons.volumeButtonEvents.listen((event) {
setState(() {
_latestHardwareButtonEvent = event.toString();
});
});
_homeButtonSubscription = HardwareButtons.homeButtonEvents.listen((event) {
setState(() {
_latestHardwareButtonEvent = 'HOME_BUTTON';
});
});
_lockButtonSubscription = HardwareButtons.lockButtonEvents.listen((event) {
setState(() {
_latestHardwareButtonEvent = 'LOCK_BUTTON';
});
});
}
@override
void dispose() {
super.dispose();
_volumeButtonSubscription?.cancel();
_homeButtonSubscription?.cancel();
_lockButtonSubscription?.cancel();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Value: $_latestHardwareButtonEvent\n'),
],
),
),
),
);
}
}