我试图在扑动中使用OrientationBuilder
,但我的代码中的第一个if语句始终为真。
OrientationBuilder(
builder: (context, orientation) {
if (orientation == Orientation.portrait) {
return _portraitMode();
} else {
return _landscapeMode();
}
},
),
我正在尝试显示两种不同的东西,这取决于我的手机的屏幕方向。问题是,第一个if语句总是为真。
要获取设备的当前方向,您可以使用MediaQuery类的帮助并获取方向,如:-
var currOrientation = MediaQuery.of(context).orientation
并像这样优化你的代码,
return currOrientation == Orientation.potrait ? _potraitMode() : _landscapeMode();
希望这对你有帮助!