Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Created December 27, 2023 19:21
Show Gist options
  • Save slightfoot/a0f22a98e2b70ff267a29210069c4c25 to your computer and use it in GitHub Desktop.
Save slightfoot/a0f22a98e2b70ff267a29210069c4c25 to your computer and use it in GitHub Desktop.
Get Device Orientation including upside down - by Simon Lightfoot - Humpday Q&A :: 27th December 2023 #Flutter #Dart
// MIT License
//
// Copyright (c) 2023 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jni/jni.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(DeviceOrientation.values);
runApp(const MaterialApp(home: Home()));
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
final rotationManager = RotationManager();
late DeviceOrientation deviceOrientation;
@override
void dispose() {
rotationManager.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final orientation = MediaQuery.orientationOf(context);
deviceOrientation = rotationManager.getDeviceOrientation();
return Material(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'$deviceOrientation\n\n$orientation',
style: const TextStyle(fontSize: 20.0),
textAlign: TextAlign.center,
),
],
),
);
}
}
class RotationManager {
RotationManager() {
JObject.fromRef(Jni.getCurrentActivity()) //
.use((activity) {
_display = activity.callMethodByName<JObject>(
'getDisplay',
'()Landroid/view/Display;',
[],
);
});
}
late final JObject _display;
DeviceOrientation getDeviceOrientation() {
final rotation = _display.callMethodByName<int>(
'getRotation', '()I', [], JniCallType.intType);
return switch (rotation) {
0 => DeviceOrientation.portraitUp, // Surface.ROTATION_0
1 => DeviceOrientation.landscapeLeft, // Surface.ROTATION_90
2 => DeviceOrientation.portraitDown, // Surface.ROTATION_180
3 => DeviceOrientation.landscapeRight, // Surface.ROTATION_270
_ => throw 'Bad rotation value: $rotation',
};
}
void dispose() {
_display.release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment