2025年3月25日 星期二 甲辰(龙)年 月廿四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 软件应用 > 开发工具(IDE)

Flutter 判断横竖屏(含尺寸、方向改变时触发)

时间:04-12来源:作者:点击数:35

判断横竖屏,拿heightwidth比较下就可以了,比如这样:

  • MediaQuery.of(context).size.width >
  • MediaQuery.of(context).size.height // 还可以 * 1.2 之类的(根据场景自己看着办[滑稽])
  • ? "横屏"
  • : "竖屏"

如果说要在横竖屏、应用显示尺寸改变时触发,那可以套一个OrientationBuilder,比如这样(新建了个 Flutter 项目用来示例,所以以下例子和使用场景其实不太匹配 [笑哭]):

  • OrientationBuilder(
  • builder: (context, orientation) {
  • return Center(
  • child: Column(
  • mainAxisAlignment: MainAxisAlignment.center,
  • children: <Widget>[
  • Text(
  • MediaQuery.of(context).size.width >
  • MediaQuery.of(context).size.height * 1.2 // height 这边多留了点,不需要的话可以看下一个例子(直接用 orientation)
  • ? "横屏"
  • : "竖屏",
  • ),
  • Text(
  • "${MediaQuery.of(context).size.width} × ${MediaQuery.of(context).size.height}",
  • style: Theme.of(context).textTheme.headline4,
  • ),
  • ],
  • ),
  • );
  • },
  • );

或者这样(直接用OrientationBuilderorientation):

  • OrientationBuilder(
  • builder: (context, orientation) {
  • return Center(
  • child: Column(
  • mainAxisAlignment: MainAxisAlignment.center,
  • children: <Widget>[
  • Text(
  • orientation == Orientation.landscape ? "横屏" : "竖屏",
  • style: Theme.of(context).textTheme.headline4,
  • ),
  • ],
  • ),
  • );
  • },
  • );
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门