Skip to content

Instantly share code, notes, and snippets.

@roipeker
Created June 29, 2022 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roipeker/c475b15edd7cdae544687ce85c27543e to your computer and use it in GitHub Desktop.
Save roipeker/c475b15edd7cdae544687ce85c27543e to your computer and use it in GitHub Desktop.
sample for nir orientation
/// in puspec dependency: native_device_orientation: ^1.1.4
// sample for nir.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_device_orientation/native_device_orientation.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool landscape = false;
int quarterTurns = 0;
final turnsByOrientation = <NativeDeviceOrientation, int>{
NativeDeviceOrientation.landscapeRight: 3,
NativeDeviceOrientation.landscapeLeft: 1,
NativeDeviceOrientation.portraitDown: 2,
NativeDeviceOrientation.portraitUp: 0,
NativeDeviceOrientation.unknown: 0,
};
final deviceOrientator = NativeDeviceOrientationCommunicator();
late StreamSubscription sub;
@override
void initState() {
sub = deviceOrientator.onOrientationChanged(useSensor: true).listen((data) {
quarterTurns = turnsByOrientation[data] ?? 0;
print('Orientation: $data');
setState(() {});
});
super.initState();
}
@override
void dispose() {
sub.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return RealHome(
turns: quarterTurns,
);
}
}
class RealHome extends StatefulWidget {
final int turns;
const RealHome({Key? key, required this.turns}) : super(key: key);
@override
State<RealHome> createState() => _RealHomeState();
}
class _RealHomeState extends State<RealHome> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('is Landscape: ${widget.turns}'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
RotatedBox(
quarterTurns: widget.turns,
child: Text('Text always straight'),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment