flutter_device_orientation 0.2.0
flutter_device_orientation: ^0.2.0 copied to clipboard
Calculate the direction of a phone's rear camera using accelerometer and magnetometer sensors.
import 'package:flutter_device_orientation/flutter_device_orientation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: OrientationDebugScreen(),
),
);
}
class OrientationDebugScreen extends StatefulWidget {
const OrientationDebugScreen({super.key});
@override
State<OrientationDebugScreen> createState() => _OrientationDebugScreenState();
}
class _OrientationDebugScreenState extends State<OrientationDebugScreen> {
late final DeviceOrientation orientation;
OrientationData data = OrientationData.zero;
@override
void initState() {
super.initState();
orientation = DeviceOrientation();
orientation.stream.listen((value) {
if (!mounted) return;
setState(() {
data = value;
});
});
orientation.start();
}
@override
void dispose() {
orientation.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'DEVICE ORIENTATION',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 40),
_Value(name: 'YAW', value: data.yaw),
_Value(name: 'PITCH', value: data.pitch),
_Value(name: 'ROLL', value: data.roll),
],
),
),
);
}
}
class _Value extends StatelessWidget {
final String name;
final double value;
const _Value({required this.name, required this.value});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10),
child: Text(
'$name: ${value.toStringAsFixed(1)}°',
style: const TextStyle(color: Colors.white, fontSize: 22),
),
);
}
}