flutter 无法在可滚动列表中使用溢出框

vjrehmav  于 2023-05-23  发布在  Flutter
关注(0)|答案(2)|浏览(160)

我想在ListView中使用OverflowBox,但它在可滚动内容的情况下无法正常工作,但在这些情况下它可以完美工作:
1.如果我用不可滚动的小部件(如Column)替换ListView。
1.如果ListView内容不需要滚动。

ListView(
      children: [
        SizedBox(
          height: MediaQuery.of(context).size.height * .45,
          child: OverflowBox(
            maxHeight: MediaQuery.of(context).size.height * .45,
            maxWidth: MediaQuery.of(context).size.width,
            child: HorizontalList(images: data?.pictures ?? []),
          ),
        ),
      ],
    )
tzdcorbm

tzdcorbm1#

将溢流盒放入固定大小的容器中。

krcsximq

krcsximq2#

如果你想保持HorizontalList滚动,你应该像这样使用shrinkWrap,假设HorizontalList是一个ListView:

class HorizontalList extends StatelessWidget {
  final List<String> images;
  const HorizontalList({Key? key, required this.images}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        shrinkWrap: true,
        itemCount: 100,
        itemBuilder: (context, index){
      return Text("item: $index");
    });
  }
}

将itemCount更改为try。
对于测试,我将所有内容放在一个列中,看起来像这样:

Expanded(
                child: ListView(
                  children: [
                    Text("before Text"),
                    SizedBox(
                      height: MediaQuery.of(context).size.height * .45,
                      child: OverflowBox(
                        maxHeight: MediaQuery.of(context).size.height * .45,
                        maxWidth: MediaQuery.of(context).size.width,
                        child: HorizontalList(images: ["1","2","2"]),
                      ),
                    ),
                    Text("after Text")
                  ],
                ),
              )

相关问题