如何根据用户手机的方向向左右移动我的Flutter应用程序?

时间:2019-09-19 05:09:29

标签: android flutter uiinterfaceorientation

使用

时,我使我的应用程序进入横向模式
SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
]);

但是我不能根据用户握住手机的方式来使其向右或向左转。

我不想从肖像变成风景,我只是想即使在禁用自动旋转的情况下也能够转到风景的两个不同侧面。

1 个答案:

答案 0 :(得分:0)

在pubsec.yaml文件中添加以下依赖项

    dependencies:
  native_device_orientation: ^0.2.0

运行以下命令或终端以获取依赖包

flutter pub get

将以下类导入为

import 'package:native_device_orientation/native_device_orientation.dart';

这是示例代码的其余部分。

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

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

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

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
          appBar: new AppBar(
            title: new Text('Native Orientation example app'),
            actions: <Widget>[Switch(value: useSensor, onChanged: (val) => setState(() => useSensor = val))],
          ),
// encapsulating your Screen UI with NativeDeviceOrientationReader is must.
              body: NativeDeviceOrientationReader(
                builder: (context) {
                  NativeDeviceOrientation orientation = NativeDeviceOrientationReader.orientation(context);
                  print("Received new orientation: $orientation");
                  return Center(child: Text('Native Orientation: $orientation\n'));
                },
                useSensor: useSensor,
              ),
              floatingActionButton: Builder(
                builder: (context) {
                  return Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      FloatingActionButton(
                        child: Text("Sensor"),
                        onPressed: () async {
                          NativeDeviceOrientation orientation =
                          await NativeDeviceOrientationCommunicator().orientation(useSensor: true);
                          Scaffold.of(context).showSnackBar(
                            SnackBar(
                              content: Text("Native Orientation read: $orientation"),
                              duration: Duration(milliseconds: 500),
                            ),
                          );
                        },
                      ),
                      SizedBox(height: 10),
                      FloatingActionButton(
                        child: Text("UI"),
                        onPressed: () async {
                          NativeDeviceOrientation orientation =
                              await NativeDeviceOrientationCommunicator().orientation(useSensor: false);
                          Scaffold.of(context).showSnackBar(
                            SnackBar(
                              content: Text("Native Orientation read: $orientation"),
                              duration: Duration(milliseconds: 500),
                            ),
                          );
                        },
                      ),
                    ],
                  );
                },
              )),
        );
      }
    }