Dart 如何检测颤振布局中的方向变化?
如何确定方向是纵向的还是横向的Dart 如何检测颤振布局中的方向变化?,dart,orientation,flutter,flutter-layout,Dart,Orientation,Flutter,Flutter Layout,如何确定方向是纵向的还是横向的 if(portrait){ return ListView.builder() }else{ return GridView.count() } 您可以使用MediaQuery检查方向: var isPortrait = MediaQuery.of(context).orientation == Orientation.portrait 为了确定屏幕的方向,我们可以使用OrientationBuilder小部件。方向生成器将确定当前方向,并在方向更改时
if(portrait){
return ListView.builder()
}else{
return GridView.count()
}
您可以使用
MediaQuery
检查方向:
var isPortrait = MediaQuery.of(context).orientation == Orientation.portrait
为了确定屏幕的方向,我们可以使用
OrientationBuilder
小部件。方向生成器将确定当前方向,并在方向更改时重建
new OrientationBuilder(
builder: (context, orientation) {
return new GridView.count(
// Create a grid with 2 columns in portrait mode, or 3 columns in
// landscape mode.
crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
);
},
);
您可以在此处找到完整的示例:
这很容易
if (MediaQuery.of(context).orientation == Orientation.portrait){
// is portrait
}else{
// is landscape
}
为了完整性起见,我想添加另一种方法来检测颤振中的方向。答案中已经提到了两种检测方法。是的
Layout Builder
。以下是简短的片段:
return Padding(
padding: _padding,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
if(constraints.maxHeight > constraints.maxWidth) {
return _getPortraitLayout();
}
else {
return _getLandscapeLayout();
}
},
),
);
Mediaquery不能在
initState()
中使用,而OrientationBuilder需要一个小部件,因此我创建了以下类,可以在项目中的任何地方使用
if(IsPortrait.isPortrait){
}else{
}
IsPortrait.class
以下功能可用于更改方向
return ValueListenableBuilder(
valueListenable: IS_PORTRAIT,
builder: (BuildContext context, value, Widget child) {
return Container(
color: Colors.green[100],
child: Container(),
height: (IS_PORTRAIT.value)? 150: 80,
);
},
);
仅纵向模式
/// blocks rotation; sets orientation to: portrait
void _portraitModeOnly() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
/// blocks rotation; sets orientation to: landscape
void _landscapeModeOnly() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
仅横向模式
/// blocks rotation; sets orientation to: portrait
void _portraitModeOnly() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
/// blocks rotation; sets orientation to: landscape
void _landscapeModeOnly() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
启用纵向和横向
void _enableRotation() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
我的变种
1)在全局作用域集中设置全局值通知程序:
final ValueNotifier<bool> IS_PORTRAIT = ValueNotifier<bool>(true);
3)任何地方都可以收听当前方向
return ValueListenableBuilder(
valueListenable: IS_PORTRAIT,
builder: (BuildContext context, value, Widget child) {
return Container(
color: Colors.green[100],
child: Container(),
height: (IS_PORTRAIT.value)? 150: 80,
);
},
);
@AdarshVijayanP您不能在随机代码位置使用它。它应该在
build
或didChangeDependencies
@GünterZöchbauer在build
中编写代码,然后传递到我的小部件,谢谢,现在它工作得很好。我在Android上使用过它,当设备的自动旋转功能启用时,它工作得很好,但大多数人在禁用自动旋转功能的情况下使用手机(被迫画像)而且方向生成器不会触发景观。我们是否可以检测自动旋转状态的方向?您认为如何?此选项的缺点是您可以区分方向,如landscapeLeft和landscapeRight。方向生成器似乎只在脚手架级别的建筑中起作用。它似乎不起作用o当我构建一个有状态的小部件时,在树的下一步工作。我想知道这是否是已知的。这只适用于在无约束布局中使用布局生成器(例如在屏幕小部件树的顶部)。如果将LayoutBuilder放置在高度为50、宽度为100的容器中,则您的方法将始终告诉用户他处于横向模式
return ValueListenableBuilder(
valueListenable: IS_PORTRAIT,
builder: (BuildContext context, value, Widget child) {
return Container(
color: Colors.green[100],
child: Container(),
height: (IS_PORTRAIT.value)? 150: 80,
);
},
);