有一个小部件 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 来根据方向做一些数学运算,让它看起来像你想要的那样