native_device_orientation 2.0.3 copy "native_device_orientation: ^2.0.3" to clipboard
native_device_orientation: ^2.0.3 copied to clipboard

A Flutter plugin for reading device's native orientation, either from UI orientation or from sensors

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:native_device_orientation/native_device_orientation.dart';

void main() => runApp(const MyApp());

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

  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  bool useSensor = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Native Orientation Example'),
            actions: <Widget>[
              const Center(child: Text('Sensor:')),
              Switch(value: useSensor, onChanged: (val) => setState(() => useSensor = val)),
            ],
          ),
          body: NativeDeviceOrientedWidget(
            landscape: (context) {
              return const Center(child: Text('Native Orientation: Landscape (Unknown Side)\n'));
            },
            landscapeLeft: (context) {
              return const Center(child: Text('Native Orientation: Landscape Left\n'));
            },
            landscapeRight: (context) {
              return const Center(child: Text('Native Orientation: Landscape Right\n'));
            },
            portrait: (context) {
              return const Center(child: Text('Native Orientation: Portrait (Unknown Side)\n'));
            },
            portraitUp: (context) {
              return const Center(child: Text('Native Orientation: Portrait Up\n'));
            },
            portraitDown: (context) {
              return const Center(child: Text('Native Orientation: Portrait Down\n'));
            },
            fallback: (context) {
              return const Center(child: Text('Native Orientation: Unknown\n'));
            },
            useSensor: useSensor,
          ),
          floatingActionButton: Builder(
            builder: (context) {
              return Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  FloatingActionButton(
                    child: const Text('Sensor'),
                    onPressed: () async {
                      final orientation = await NativeDeviceOrientationCommunicator().orientation(useSensor: true);
                      if (!context.mounted) return;
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(
                          content: Text('Native Orientation read: $orientation'),
                          duration: const Duration(milliseconds: 500),
                        ),
                      );
                    },
                  ),
                  const SizedBox(height: 10),
                  FloatingActionButton(
                    child: const Text('UI'),
                    onPressed: () async {
                      final orientation = await NativeDeviceOrientationCommunicator().orientation(useSensor: false);
                      if (!context.mounted) return;
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(
                          content: Text('Native Orientation read: $orientation'),
                          duration: const Duration(milliseconds: 500),
                        ),
                      );
                    },
                  ),
                ],
              );
            },
          )),
    );
  }
}
135
likes
150
pub points
98%
popularity

Publisher

unverified uploader

A Flutter plugin for reading device's native orientation, either from UI orientation or from sensors

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on native_device_orientation