【问题标题】:Flutter Get True DeviceOrientationFlutter 获得真正的设备方向
【发布时间】:2020-06-12 12:46:35
【问题描述】:

我正在使用下面的行来获取设备方向

if (MediaQuery.of(context).orientation == Orientation.landscape) // Landscape
  {
    // Do Something
  }
else // Portrait
  {    
    // Do Something Else
  }

我想获得真正的设备方向。例如,我想知道设备是 LandscapeRight 还是 LandscapeLeft、portraitUp 还是 PortraitDown。

有人可以帮我解决这个问题吗?提前致谢。

【问题讨论】:

  • 只是出于好奇,你为什么想要那个?
  • @Er1 我在将 AlertDialog 居中时遇到问题,当它是 LandscapeRight 和 LandscapeLeft 时,填充会发生变化。我在这里问了这个问题:stackoverflow.com/questions/62339482/… 如果我能知道正确的设备方向,我就能解决 AlertDialog 居中问题
  • @SamiHaddad 我去看看,谢谢=D

标签: flutter device-orientation


【解决方案1】:

有一个小部件 OrientationBuilder 可以帮助您解决此问题

OrientationBuilder(
  builder: (context, orientation) {
    return GridView.count(
      // Create a grid with 2 columns in portrait mode,
      // or 3 columns in landscape mode.
      crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
    );
  },
);

我看到您正在尝试将它与对话框一起使用以使其居中,如果您查看对话框的代码,您会看到它使用 ConstraninedBox 和 56.0 的 Step 进行填充(它将展开如果屏幕允许,其大小为 56.0)。您可以用自己的 ConstrainedBox 包装 AlertDialog 的内容,并计算您的最小和最大尺寸,使其看起来居中、正方形、高矩形等。

final size = MediaQuery.of(context).size;
            double actionHeight = 16.0 + 36.0; //The size of the action widget, 8 padding top and bottom (16), and if ButtonBarTheme.buttonHeight == null it defaults to 36 minHeight
return AlertDialog(
   scrollable: true,
   title: Text('Title'),
   content: ConstrainedBox(
     constraints: BoxConstraints(
       minWidth: (size.width / 2) - actionHeight, //do the math you want here
       maxWidth: (size.width / 2) - actionHeight, //do the math you want here
       minHeight: (size.height/ 2) - actionHeight, //do the math you want here
       maxHeight: (size.height/ 2) - actionHeight //do the math you want here
     ),
     child: SingleChildScrollView(
       child: Column(
         children: [
           for(int i = 0; i < 4; i++)
             ListTile(
               title: Text('Text $i'),
               trailing: i % 2 == 0 ? 
                 Icon(Icons.check_box) : Icon(Icons.check_box_outline_blank)
             )
           ],
         )
       )
     ),
   actions: [
      FlatButton(child: Text('Cancel'), onPressed: () => Navigator.pop(context)),
      FlatButton(child: Text('Ok'), onPressed: () => Navigator.pop(context))
   ],
);

你可以结合 OrientationBuilder 和 ConstrainedBox 来根据方向做一些数学运算,让它看起来像你想要的那样

【讨论】:

  • 这没有回答这个问题——“我想获得真正的设备方向。例如,我想知道设备是横向右还是横向左、纵向向上还是纵向向下。”
【解决方案2】:

为了重申这个问题,我们需要一种方法来知道设备方向是什么时候:

  • 纵向向上
  • 纵向向下
  • 风景左
  • 风景右

基于这个issue,Flutter 自带的 OrientationBuilder 小部件并没有给你这个信息。相反,它会为您提供父小部件的方向(纵向或横向)。

欢迎使用native_device_orientation 包。这个包有一个 NativeDeviceOrientationReader 小部件,其作用类似于 OrientationBuilder。

NativeDeviceOrientationReader(
  builder: (context) {
     NativeDeviceOrientation orientation = NativeDeviceOrientationReader.orientation(context);

     return Center(child: Text(orientation.toString()))
  },
),

【讨论】:

    【解决方案3】:

    这是我之前解决问题的方法

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
    
      Widget _portraitView(){
    
        // Return Your Widget View Here Which you want to Load on Portrait Orientation.
        return Container(
       width: 300.00,
        color: Colors.green,
        padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
        child: Text(' Portrait View Detected. ',
                textAlign: TextAlign.center,  
                style: TextStyle(fontSize: 24, color: Colors.white)));
      } 
    
      Widget _landscapeView(){
    
        // // Return Your Widget View Here Which you want to Load on Landscape Orientation.
        return Container(
        width: 300.00,
        color: Colors.pink,
        padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
        child: Text(' Landscape View Detected.',
                textAlign: TextAlign.center,  
                style: TextStyle(fontSize: 24, color: Colors.white)));
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            home: Scaffold(
            appBar: AppBar(
            title: Text('Detect Device Screen Orientation')),
            body: OrientationBuilder(
              builder: (context, orientation) {
              return Center(
    
                child: orientation == Orientation.portrait 
                ? _portraitView() 
                : _landscapeView() 
    
                    );
               }
              )
            )
          );
      }
    }
    

    希望对你有帮助。

    【讨论】:

    • 对不起,但它似乎没有解决这个问题。问题是如何区分 landscapeRightlandscapeLeftportraitUpportraitDown
    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 2019-01-09
    • 2020-07-24
    • 2019-12-23
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    相关资源
    最近更新 更多