简体   繁体   中英

Flutter Dynamic Height Horizontal Listivew inside Vertical Listview

I'm facing some difficulties in assembling a layout that is quite common in apps.

I have a ListView.builder, involved in an Expanded inside a Column. For each item, I have a new ListView.builder (horizontal), but it only works if I put a fixed height.

Is there any way to make this horizontal (daughter) list dynamic, with several different sizes, without the need for a fixed height?

Expanded(
          child: ListView.builder(
            shrinkWrap: true,
            itemCount: 10,
            itemBuilder: (context, index) {
              return SizedBox(
                height: 30, //Only works with this height
                width: MediaQuery.of(context).size.width,
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: 2,
                  itemBuilder: (context, index) {
                    return Container(
                      width: MediaQuery.of(context).size.width,
                      color: index == 0 ? Colors.blue : Colors.red,
                      child: Text("Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem"),
                    );
                },),
              );
          },),
        )

PS.: I've tried Expanded, Flexible, InstrinctHeight and nothing worked.

如果这是您想要实现的目标,我可以这样做

  return Scaffold(
          body: ListView.builder(
            shrinkWrap: true,
            itemCount: 10,
            itemBuilder: (context, index) {
              return SizedBox(
                height: 30, //Only works with this height
                width: MediaQuery.of(context).size.width,
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: 2,
                  itemBuilder: (context, index) {
                    return Container(
                      width: MediaQuery.of(context).size.width,
                      color: index == 0 ? Colors.blue : Colors.red,
                      child: Text(
                          "Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem"),
                    );
                  },
                ),
              );
            },
          ),
        );

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