颤动:ListView.builder 中的自动垂直高度

Flutter: Auto Vertical Height in ListView.builder

我正在尝试展示一些带有 ListView.builder
的卡片 我想将每张卡片的高度自动设置为其子内容的高度

class HomePage extends StatelessWidget {
 Widget _buildListItems(BuildContext context, DocumentSnapshot document) {
  return Center(
   child: Card(
    child: Column(
      children: <Widget>[
        ListTile(
          title: Text(document['title']),
          subtitle: Text(document['subtitle']),
        ),
        Expanded(
          child: Container(
            padding: EdgeInsets.all(20.0),
            child: Text(
              document['summary'],
              softWrap: true,
            ),
          ),
        )
      ],
    ),
  ),
);
}


@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Title'),
    centerTitle: true,
  ),
  body: StreamBuilder(
    stream: Firestore.instance.collection('randomDB').snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) return const Text('Loading...');
      return ListView.builder(
        itemExtent: 225.0,
        itemCount: snapshot.data.documents.length,
        itemBuilder: (context, index) =>
            _buildListItems(context, snapshot.data.documents[index]),
      );
    },
  ),
}

当文档['summary'] 太长时,它会使文本通过卡片小部件溢出。这次我所做的就是在 ListView.Builder

中增加 itemExtent

有没有办法动态设置ListView的高度?

我相信该专栏有一个叫做 mainAxisSize 属性

的东西
Column(
  mainAxisSize: MainAxisSize.min,
)

MainAxisSize.min 将使列大小拉伸到子项的最小大小。 与它相反的是MainAxisSize.max,它将拉伸到它可以达到的最大尺寸。

不过我还没有测试过。希望对你有帮助

你可以使用我的实现

Widget adapter(List<String> strings){
    List<Widget> widgets= List<Widget>();
    for(int i=0;i<strings.length;i++){
      widgets.add(Text(strings[i]));
    }

    Row row=Row(
            children: <Widget>[
              Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children:widgets
              ),
            ],
          );
    return row;
  }

此示例显示字符串列表

只是不要指定 itemExtent 参数。

或者只给它一个 null 值,而不是 225.0

这样,child 将在 ListView

中获得其所需的确切高度

创建自定义ListView itemBuilder代码示例如下:

 Container(
  child: ListView.builder(
    padding: EdgeInsets.all(5),
    itemCount: snapshot.data.length,
    physics: BouncingScrollPhysics(),
    itemBuilder: (context, index) {
    return generateColum(snapshot.data[index]);
   }),
);

  Widget generateColum(HouseholderModel item) => Card(
  child: InkWell(
    onTap: () {
    },
    child: Stack(
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.only(left: 5.0, top: 5.0, right: 5.0, bottom: 5.0),
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: 70,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                CircleAvatar(
                  radius: 25,
                  foregroundColor: Colors.transparent,
                  backgroundColor: appThemeColor.withOpacity(0.5),
                  child: CachedNetworkImage(
                    imageUrl: testImage,
                  )
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(8.0, 5.0, 0.0, 5.0),
                  width: MediaQuery.of(context).size.width * .60,
                  color: Colors.transparent,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        "777 Brockton Avenue",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: CustomTextStyle.textSubtitle2BlackShades(context),
                      ),
                      Text(
                        'Michael Smith',
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: CustomTextStyle.textSubtitle1blueShades(context),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        Positioned(
          bottom: 0.1,
          right: 0.1,
          child: Container(
            padding: EdgeInsets.all(2),

            decoration: BoxDecoration(
                boxShadow: [BoxShadow(
                  color: Colors.grey,
                  blurRadius: 2.0,
                ),],
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(3.0),
                  bottomRight: Radius.circular(3.0)),
              color: appThemeColor,
            ),
            height: 15,
            width: MediaQuery.of(context).size.width * .20,
            child: Container(
              alignment: Alignment.center,
              child: FittedBox(fit:BoxFit.fitWidth,
                child: Text('Vice-President', style: CustomTextStyle.textSubtitle1(context).copyWith(color: Colors.white),),)

            ),
          ),
        ),
      ],
    ),
  ),
  );

只需添加 shrinkWrap: true.

&删除 itemExtent: 225.0,

ListView.builder(
  shrinkWrap: true,
  itemCount:  snapshot.data.documents.length,
  itemBuilder: (context, index) =>
        _buildListItems(context, snapshot.data.documents[index]),
 },
),

尝试

class HomePage extends StatelessWidget {
 Widget _buildListItems(BuildContext context, DocumentSnapshot document) {
  return Center(
   child: Card(
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Flexible(
          ListTile(
          title: Text(document['title']),
          subtitle: Text(document['subtitle']),
        ),
        ),
        Expanded(
          child: Container(
            padding: EdgeInsets.all(20.0),
            child: Text(
              document['summary'],
              softWrap: true,
            ),
          ),
        )
      ],
    ),
  ),
);
}