flutter - Flutter:具有不同高度约束的水平 ListView 视图
问题描述
我有一个固定高度为 100 的水平列表栏(在 Column 内使用)。高度 100 适合 iPhone8。但由于 100 高度限制,iPhone-X 中的像素溢出。如果我做到140,两者都很好。但是在 iphone8 中该栏看起来并不好。所以我想要一台设备的高度为 120,而另一台设备的高度为 140。我想我可以使用 ConstrainedBox 来实现它,如下所示:
ConstrainedBox(constraints: BoxConstraints(minHeight: 100, maxHeight: 140), child: ListView(...))
但是 listview 一直以 140 高度运行。那么如何实现不同设备的动态高度约束呢?
示例小部件:
class MyHorizontalBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SizedBox(
height: 100, // 140
child: ListView(
scrollDirection: Axis.horizontal,
children: [
_myCard(),
_myCard(),
_myCard(),
],
),
);
}
Widget _myCard(){
return Container(
width: 115,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Top', style: TextStyle(fontSize: 12)),
Text('Middle', style: TextStyle(fontSize: 25)),
Text('Bottom', style: TextStyle(fontSize: 18)),
],
),
);
}
}
解决方案
尝试使用 MediaQuery.of(context).size.height 或 width 代替恒定的高度和宽度。
推荐阅读
- hadoop - 备用资源管理器在接管控制权时如何计算节点及其各自的节点标签详细信息?
- gps - NMEA输出中的ublox GPS奇怪字符
- android - Flutter 应用恢复;当活动被系统杀死时如何保存应用程序状态?
- mysql - MySQL GROUP BY,同时按列内容保留某些行
- mobile - 创建我的 CodeIgniter 网站的移动版本
- java - 将新字符[]语句中的字符[]数组作为括号中的参数传递,并将字符保存到新对象中
- c++ - 递减 std::vector::begin 是否未定义,即使从未使用过?
- r - 跨列加速计算
- html - 在表格html中将文本与右侧对齐
- excel - DAX 在获取仅包含数字的单元格时计算列的平均值