7

我是一名 android 开发人员,并且是 Flutter 的新手。

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

在此处输入图像描述

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],);
              },
            ),
          ),
        ),
      ],
    );
  }
}
4

4 回答 4

8

对于高度,您可以使用“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(),
)
于 2019-04-30T10:44:56.750 回答
0

在 GridView 中使用这一行

the childAspectRatio as per yor need 
childAspectRatio: double,
于 2019-04-26T05:33:35.203 回答
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而不是根据您的内容大小

于 2019-04-26T05:12:57.490 回答
0

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

如果您只想“缩小”match_parent文本小部件的高度(以及类似宽度的东西),请将其包裹在 a 周围,Column就像这样RowExpanded

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 Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Expanded(
              child: Card(
                child: Text(
                  category.name,
                  style: TextStyle(fontSize: 34.0, fontWeight: FontWeight.bold),
                ),
                color: Colors.amberAccent,
              ),
            ),
          ],
        ),
      ],
    );
  }
}
于 2019-04-26T05:14:48.897 回答