颤振获取真实的设备方向

时间:2020-06-12 12:46:35

标签: flutter device-orientation

我正在使用以下几行来获取设备方向

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

我想获得真正的设备方向。例如,我想知道设备是landscapeRight还是landscapeLeft,portraitUp或portraitDown。

有人可以帮我吗?预先感谢。

2 个答案:

答案 0 :(得分:0)

有一个小部件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和Step 56.0进行填充(它将展开如果屏幕允许,则以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来基于方向进行一些数学运算,并使其看起来像您想要的

enter image description here

答案 1 :(得分:-1)

这是我以前解决问题的方式

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() 

                );
           }
          )
        )
      );
  }
}

希望它会对您有所帮助。