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

时间:2019-01-03 06:45:05

标签: flutter flutter-layout

我正在尝试使用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]),
      );
    },
  ),
}

当document ['summary']太长时,它将使文本通过卡片小部件溢出。这次,我要做的就是增加ListView.Builder中的itemExtent

是否可以动态设置ListView的高度?

5 个答案:

答案 0 :(得分:3)

最简单的解决方案

为您shrinkWrap: true添加ListView.builder

自动调整高度取决于Card小部件中的内容

ListView.builder(
  shrinkWrap: true,
  physics: NeverScrollableScrollPhysics(),
  itemCount: snapshot.data['transactions'].length,
  itemBuilder: (context, i) {
  return transfers(
  snapshot.data['transactions'][i]);
 },
),

答案 1 :(得分:1)

您可以使用我的实现

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;
  }

此示例显示字符串列表

答案 2 :(得分:0)

我认为该列具有mainAxisSize属性

Column(
  mainAxisSize: MainAxisSize.min,
)

MainAxisSize.min将使Column的大小延伸到子代的最小大小。 与它相对的是MainAxisSize.max,它将拉伸到可以达到的最大大小。

但是我还没有测试过。希望对您有帮助

答案 3 :(得分:0)

只需不指定itemExtent参数。

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

这样,孩子将进入ListView

答案 4 :(得分:0)

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

My Output

 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),),)

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