Flutter删除GridView行之间的空间 [英] Flutter remove space between GridView row
问题描述
我正在尝试创建一个凸起按钮的gridview,但是在网格的各行之间有很大的空间,如下图所示:
我正在使用页面底部的代码实现GridView:
如您所见,我设置了:
SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 150, mainAxisSpacing: 4, crossAxisSpacing: 4),
我希望设置主轴间距和横轴间距会删除这些间距.
我还尝试过在一个大小框中返回gridview并将其更改为SliverGridWithFixedCount,但似乎没有任何改变.
我不确定我在布局中做错了什么吗?
感谢您的帮助
body: Column(
children: <Widget>[
Flexible(
child: filtersGridView(),
),
],
),
);
}
}
class filtersGridView extends StatefulWidget {
@override
_filtersGridViewState createState() => _filtersGridViewState();
}
class _filtersGridViewState extends State<filtersGridView> {
final List <DocumentSnapshot> documents;
_filtersGridViewState({this.documents});
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection('categories').snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: const Text('Loading events...'));
}
return GridView.builder(
gridDelegate:
SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 150, mainAxisSpacing: 4, crossAxisSpacing: 4),
itemBuilder: (BuildContext context, int index) {
return Column(children: <Widget>[
InkWell(
onTap: () {
},
child: SizedBox(
height: 30,
child: RaisedButton(
color: Colors.white,
child: Row(
children: <Widget>[
Text(snapshot.data.documents[index]['name'].toString(), textAlign: TextAlign.center, style: TextStyle(fontSize: 15, color: Colors.black,),),
],
),
如果您担心按钮内部的填充-发生这种情况的原因是将材质按钮的最小宽度设置设置为88
.>
此外,根据我的经验,我发现实质性按钮周围有一些奇怪的边缘.我用materialTapTargetSize: MaterialTapTargetSize.shrinkWrap
解决了.
ButtonTheme(
minWidth: 0,
height: 30,
child: RaisedButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
// ...
)
)
如果您希望按钮填充整个高度maxCrossAxisExtent
-使用 RawMaterialButton
,并分配了自定义约束.
已更新
我认为问题出在按钮之内,但我发现实际上是关于GridView委托的.
SliverGridDelegateWithMaxCrossAxisExtent
按照Flutter文档的工作方式:>
This delegate creates grids with equally sized and spaced tiles.
委托的childAspectRatio
属性的默认值为1.0
,即-正方形.您将获得完美的逻辑布局显示-1:1块的网格.
要解决此问题,您需要提供正确的childAspectRatio
值.
某些伪代码:cellHeight = cellWidth / childAspectRatio
.
例如
childAspectRatio: 2
将显示大小如下的单元格:
2
-----------------
| |
| | 1
| |
-----------------
请帮助我.
I am trying to create a gridview of raised buttons but there is a large amount of space between the rows of of the grid like this picture:
I am implementing the GridView with the code at the bottom of the page:
As you can see I set:
SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 150, mainAxisSpacing: 4, crossAxisSpacing: 4),
I would have expected that setting the main axis spacing and cross axis spacing should remove those spaces.
I also tried returning the gridview in a sized box and changing it to SliverGridWithFixedCount, but nothing seems to be changing it.
I am not sure what I have done incorrectly in the layout?
Thanks for your help
body: Column(
children: <Widget>[
Flexible(
child: filtersGridView(),
),
],
),
);
}
}
class filtersGridView extends StatefulWidget {
@override
_filtersGridViewState createState() => _filtersGridViewState();
}
class _filtersGridViewState extends State<filtersGridView> {
final List <DocumentSnapshot> documents;
_filtersGridViewState({this.documents});
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection('categories').snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: const Text('Loading events...'));
}
return GridView.builder(
gridDelegate:
SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 150, mainAxisSpacing: 4, crossAxisSpacing: 4),
itemBuilder: (BuildContext context, int index) {
return Column(children: <Widget>[
InkWell(
onTap: () {
},
child: SizedBox(
height: 30,
child: RaisedButton(
color: Colors.white,
child: Row(
children: <Widget>[
Text(snapshot.data.documents[index]['name'].toString(), textAlign: TextAlign.center, style: TextStyle(fontSize: 15, color: Colors.black,),),
],
),
If you are concerned about the padding inside of the buttons - it happens due to the minimum width setting of the material buttons being set to 88
.
Also, in my experience I noticed that material buttons have some weird margin around them. I solved that with materialTapTargetSize: MaterialTapTargetSize.shrinkWrap
.
ButtonTheme(
minWidth: 0,
height: 30,
child: RaisedButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
// ...
)
)
If you want the buttons to fill the entire maxCrossAxisExtent
in height - use RawMaterialButton
with custom constraints assigned.
Updated
I assumed the problem is within the buttons, but it just occurred to me that it is in fact about the GridView Delegate.
How SliverGridDelegateWithMaxCrossAxisExtent
works as per Flutter docs:
This delegate creates grids with equally sized and spaced tiles.
The default value for childAspectRatio
property of the delegate is 1.0
, i.e. - a square. You are getting a perfectly logical layout displayed - grid of 1:1 blocks.
To solve this you need to come up with the right childAspectRatio
value.
Some pseudocode: cellHeight = cellWidth / childAspectRatio
.
e.g.
childAspectRatio: 2
will display cells sized as following:
2
-----------------
| |
| | 1
| |
-----------------
Please let me know if this helped.
这篇关于Flutter删除GridView行之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!