翩翩起舞 获取真实的设备方向

问题描述 投票:0回答:2

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

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

我想知道设备的真实方向。例如,我想知道设备是横向向右还是横向向左,纵向向上还是纵向向下。

谁能帮我解决这个问题?先谢谢你。

flutter device-orientation
2个回答
0
投票

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

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

                );
           }
          )
        )
      );
  }
}

希望能帮到你。


0
投票

有一个小工具 定向生成器 可以帮助你的

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作为padding(如果屏幕允许的话,它会用56.0的Step来扩大它的大小)。你可以用你自己的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

© www.soinside.com 2019 - 2024. All rights reserved.