How to make flutter card auto adjust its height depend on content

前端 未结 3 750
名媛妹妹
名媛妹妹 2021-02-14 17:41

In the project im using image and text inside the flutter card, but the card return a fixed height. and then i also tried just using a card with an empty value, but it still ret

3条回答
  •  星月不相逢
    2021-02-14 18:01

    Try flutter_staggered_grid_view package.

    In the pubspec.yaml , add the following dependency:

    dependencies:
      flutter_staggered_grid_view: any
    

    In your library , add the following import:

    import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
    

    Example:

      StaggeredGridView.countBuilder(
      crossAxisCount: 4,
      itemCount: 8,
      itemBuilder: (BuildContext context, int index) => new Container(
          color: Colors.green,
          child: new Center(
            child: new CircleAvatar(
              backgroundColor: Colors.white,
              child: new Text('$index'),
            ),
          )),
      staggeredTileBuilder: (int index) =>
          new StaggeredTile.count(2, index.isEven ? 2 : 1),
      mainAxisSpacing: 4.0,
      crossAxisSpacing: 4.0,
    ),
    

    Use it like GridView

    Output:


    Constructors :

    The StaggeredGridView follow the same constructors convention than the GridView. There are two more constructors: countBuilder and extentBuilder. These constructors allow you to define a builder for the layout and a builder for the children.

    Tiles :

    A StaggeredGridView needs to know how to display each tile, and what widget is associated with a tile.

    A tile needs to have a fixed number of cell to occupy in the cross axis. For the extent in the main axis you have 03 options:

    1. You want a fixed number of cells => use StaggeredTile.count.
    2. You want a fixed extent => use StaggeredTile.extent.
    3. You want a variable extent, defined by the content of the tile itself => use StaggeredTile.fit.

提交回复
热议问题