Flutter GridView项目wrap_content高度

问题描述 投票:0回答:4

我是一个Android开发人员,并且是新手。

我想用GridView项目高度创建一个wrap content(我在屏幕截图中用笔绘制它)。但我尝试使用以下代码,它只给了我正方形网格项。我想如何获得高度包装内容网格项目,我不知道,也无法找到如何获得它。请帮忙。谢谢。

enter image description here

class CategoryItem extends StatelessWidget {
  final Category category;
  CategoryItem({Key key, @required this.category})
      : assert(category != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Text(
        category.name,
        style: TextStyle(fontSize: 34.0, fontWeight: FontWeight.bold),
      ),
      color: Colors.amberAccent,
    );
  }
}


class CategoryGrid extends StatefulWidget {

  final List<Category> items;

  const CategoryGrid({Key key, this.items}) : super(key: key);

  @override
  _CategoryGridState createState() => _CategoryGridState();
}

class _CategoryGridState extends State<CategoryGrid> {

  @override
  Widget build(BuildContext context) {
    final Orientation orientation = MediaQuery.of(context).orientation;
    return Column(
      children: <Widget>[
        Expanded(
          child: SafeArea(
            top: false,
            bottom: false,
            child: GridView.builder(
              itemCount: widget.items.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: (orientation == Orientation.portrait) ? 2 : 3,),
              itemBuilder: (BuildContext context, int index) {
                return CategoryItem(category: widget.items[index],);
              },
            ),
          ),
        ),
      ],
    );
  }
}
flutter flutter-gridview
4个回答
2
投票

对于身高,你可以使用“childAspectRatio”

例如-

GridView.count(
    childAspectRatio: 4.0,
    crossAxisCount: 2,

    padding: EdgeInsets.all(5.0),
    children: <Widget>[
        Container(
            margin: EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0),
            child: Text(
                '10:00 AM - 12:00 PM',
                style: new TextStyle( color: Colors.black87, fontSize: 14.0, 
                    fontWeight: FontWeight.normal,
                ),
            ), 
        );
    ],
    shrinkWrap: true,
    // todo comment this out and check the result
    physics: ClampingScrollPhysics(),
)

0
投票

要包装内容网格项,可以使用gridview的childAspectRatio属性

防爆。

GridView.builder(
  itemCount: widget.items.length,
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: (orientation == Orientation.portrait) ? 2 : 3, childAspectRatio:(MediaQuery.of(context).size.height * 0.006)),
  itemBuilder: (BuildContext context, int index) {
     return CategoryItem(category: widget.items[index],);
  },
) 

你可以设置childAspectRatio 0.006而不是根据你的内容大小


0
投票

您需要设置childAspectRatio委托的SliverGridDelegateWithFixedCrossAxisCount属性来控制相对于网格项宽度的高度。

如果你只是想“缩小”文本小部件的高度(以及类似qazxsw poi的宽度)将它包裹在qazxsw poi,qazxsw poi和match_parent这样的

Column

0
投票

在GridView中使用此行

Row
© www.soinside.com 2019 - 2024. All rights reserved.