简体   繁体   中英

Flutter: ModalBottomSheet with ListView.separated() size to content

I'm using ListView.separated() inside in the builder of showModalBottomSheet() . I would like for the BottomSheet to resize based on the number of ListTile widgets displayed.

Here's my code:

showModalBottomSheet(
              context: context,
              backgroundColor: Colors.blueGrey,
              isScrollControlled: false,
              builder: (context) => Wrap(
                children: [
                  ListView.separated(
                    itemCount: lists.length,
                    itemBuilder: (BuildContext context, int index) =>
                        ListTile(
                      title: Text(lists[index].listName),
                    ),
                    separatorBuilder: (BuildContext context, int index) =>
                        Divider(),
                  ),
                ],
              ),
            );

I've tried wrapping the ListView inside a Wrap widget, but I got an error because the child of Wrap doesn't have a set height. Any tips?

You can do this by using the shrinkWrap parameter in the ListView widget.

在此处输入图像描述

showModalBottomSheet(
            context: context,
            builder: (context) => ListView.separated(
              shrinkWrap: true,
              itemCount: 4,
              itemBuilder: (BuildContext context, int index) => ListTile(
                title: Text('item $index'),
              ),
              separatorBuilder: (BuildContext context, int index) => Divider(),
            ),
          );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM