Flutter 如何监控横竖屏切换 ?针对横竖屏,提供对应的页面布局
a widget called OrientationBuilder
OrientationBuilder is a widget
which builds a layout or part of a layout
on an orientation change.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: OrientationBuilder(
builder: (context, orientation) {
return orientation == Orientation.portrait
? _buildVerticalLayout()
: _buildHorizontalLayout();
},
),
);
}
The OrientationBuilder has a builder function to build our layout.
The builder function is called when the orientation changes.
屏幕横竖屏切换的时候会进行回调
The possible values being Orientation.portrait or Orientation.landscape.
检查目前横竖屏状态 :
MediaQuery.of(context).orientation
MediaQuery.of(context).orientation == Orientation.landscape
如果觉得文章有用,帮忙点个喜欢❤️ ,😘😘😘 赠人玫瑰,手留余香