flutter ListViewBuilder内的扩展小部件给出“ParentDataWidget使用不正确”错误

ctzwtxfj  于 2022-12-30  发布在  Flutter
关注(0)|答案(2)|浏览(126)

我有一个容器内的列表视图构建器,我希望这个容器的大小是动态的,根据容器的孩子(这里textwidget)的大小,所以我使用扩展的小部件的容器,但它给出了一个错误:
ParentDataWidget使用不正确。

List<String> values = [
    "short text",
    "long textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong text"
  ];
body: ListView.builder(
      shrinkWrap: true,
      itemCount: values.length,
      itemBuilder: (context, index) {
        return Expanded(
          child: Container(
              margin: EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
                boxShadow: const [
                  BoxShadow(
                    color: Colors.grey,
                    blurRadius: 2.0,
                    spreadRadius: 0.0,
                    offset: Offset(2.0, 2.0),
                  ),
                ],
              ),
              child: Text(values[index])),
        );
      }),

我希望容器根据字符串列表中文本的大小进行扩展。

vecaoik1

vecaoik11#

试试这个:

List<String> values = [
    "short text",
    "long textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong textlong text"
  ];
 body: ListView.builder(
          shrinkWrap: true,
          itemCount: values.length,
          itemBuilder: (context, index) {
            return Container(
                  margin: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20),
                    boxShadow: const [
                      BoxShadow(
                        color: Colors.grey,
                        blurRadius: 2.0,
                        spreadRadius: 0.0,
                        offset: Offset(2.0, 2.0),
                      ),
                    ],
                  ),
                  child: Text(values[index]));
          }),
mklgxw1f

mklgxw1f2#

只是删除扩展,并给予一些填充的容器。像下面

ListView.builder(
    shrinkWrap: true,
    itemCount: values.length,
    itemBuilder: (context, index) {
      return Container(
          margin: EdgeInsets.all(10),
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20),
            boxShadow: const [
              BoxShadow(
                color: Colors.grey,
                blurRadius: 2.0,
                spreadRadius: 0.0,
                offset: Offset(2.0, 2.0),
              ),
            ],
          ),
          child: Text(values[index]));
    }),

相关问题