flutter - Flutter GridView.count 元素之间不需要的填充/空间
问题描述
我有一个 GridView.builder 来生成 2 个“列”元素
这是我的代码:
ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: screenWidth * 0.05),
child: Text(
"MyText",
style: TextStyle(color: Colors.white, fontSize: 22),
),
),
GridView.builder(
padding: EdgeInsets.only(
left: screenWidth * 0.05, right: screenWidth * 0.05),
shrinkWrap: true,
primary: false,
itemCount: 12,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (context, index) {
return Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
height: screenWidth / 4.1 + screenWidth / 11,
child: Column(
children: <Widget>[
Container(
height: screenWidth / 4.1,
width: screenWidth / 2.5,
color: Colors.white,
),
Container(
height: screenWidth / 11,
width: screenWidth / 2.5,
color: Colors.blue,
),
],
),
),
),
);
},
),
],
),
正如您在图像中看到的那样,标题“MyText”和 GridView 的元素没有垂直对齐。(即使它们具有相同的填充)。
是否有可能使它们具有相同的对齐方式?
谢谢!
解决方案
可能有更好的方法来解决这个问题,但这可能需要重新设计小部件的布局方式。我为你制定了一个快速解决方案。下面的代码
ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: screenWidth * 0.05),
child: Text(
"MyText",
style: TextStyle(color: Colors.white, fontSize: 22),
),
),
GridView.builder(
shrinkWrap: true,
primary: false,
itemCount: 12,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (context, index) {
return Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
height: screenWidth / 4.1 + screenWidth / 11,
child: Column(
children: <Widget>[
Container(
height: screenWidth / 4.1,
width: screenWidth / 2.5,
color: Colors.white,
),
Container(
height: screenWidth / 11,
width: screenWidth / 2.5,
color: Colors.blue,
),
],
),
),
),
);
},
),
],
),
您可以使用 'Flutter Inspector' - 'Show Debug Paint' 查看布局
推荐阅读
- kotlin - 在 Kotlin 中使用 Set 时 assertEquals 无法正常工作
- java - 为什么 PagedResourceAssembler 会清除我的模型链接?[春天仇恨]
- javascript - 使用 js 从按钮触发 onclick 事件(跟踪 google adwords 转换)
- python - 访问格式标记内的字典元素:为什么不引用键?
- python - Pandas 有效地创建和填充新数据框(?)
- node.js - 如何在 Postman 中使用坐标和地理数据类型发布 json / geojson
- javascript - javascript中用户输入的字段总和
- spring - 如何正确验证您的课程
- assembly - 内存释放:Assembly NASM x86
- javascript - 如何确定对象上可为空的属性的类型?