如何在扩展图块中显示 ListView.builder 项

how to show ListView.builder items in expansion tile

我试图在 ``` 扩展图块中使用 ListView.builder 显示项目列表,但它没有显示任何内容,也没有给出任何错误。

这里是扩展tile的代码

customExpansionTile(context, "Additional discount", true,
                Icon(Icons.add_task, color: HexColor("#5344ed")), <Widget>[
              Column(
              mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    width: MediaQuery.of(context).size.width * 0.9,
                    child: Row(
                      children: [
                        SizedBox(
                          width: MediaQuery.of(context).size.width * 0.022,
                        ),
                        textformfieldCustomwithouticon(
                            context,
                            TextInputType.number,
                            MediaQuery.of(context).size.width * 0.4,
                            quantity,
                            "Enter the quantity",
                            "Quantity ",
                            55.0),
                        SizedBox(
                          width: MediaQuery.of(context).size.width * 0.03,
                        ),
                        textformfieldCustomwithouticon(
                            context,
                            TextInputType.number,
                            MediaQuery.of(context).size.width * 0.42,
                            discountPercentage,
                            "Enter the discount",
                            "Discount",
                            55.0),
                      ],
                    ),
                  ),
               
              SizedBox20(),
              customButton(context, "Add", () {},
                  MediaQuery.of(context).size.width * 0.85, 55.0),
              SizedBox20(),

// here i'm using Listview.builder
              Flexible(                   
                  fit: FlexFit.loose,
                child: Container(
                  width:MediaQuery.of(context).size.width * 0.9,
                  height:MediaQuery.of(context).size.height*0.5,
                 child: ListView.builder(
                  itemCount: books.length,
                  itemBuilder: (context, index) {
                    final book = books[index];
                
                    return buildDiscount(book);
                  },
                ),
                )),
                    
               ],
              ),
              SizedBox10()
            ]),

buildDiscount 代码:

 Widget buildDiscount(Book book) => Card(
      elevation: 0.0,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(10),
        ),
      ),
      child: ListTile(
        trailing: SizedBox(
          height: 100,
          child: Column(
            children: [
              InkWell(
                onTap: () {
                  _getVariantRowInfo(book.id, book.id, book.id);
                },
                child: Icon(Icons.edit, color: HexColor("#7367f0")),
              ),
              SizedBox(
                height: 5,
              ),
              InkWell(
                onTap: () {},
                child: Icon(Icons.delete, color: HexColor("#7367f0")),
              ),
            ],
          ),
        ),
        title: Padding(
          padding: const EdgeInsets.fromLTRB(0, 10, 15, 0),
          child: Text(
            "  Quantity:  90",
            style: GoogleFonts.montserrat(
                fontSize: 15, fontWeight: FontWeight.bold,color: Colors.red),
          ),
        ),
        horizontalTitleGap: 10,
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "  Discount %:  " + "10%",
              style: GoogleFonts.montserrat(fontSize: 15),
            ),
            Text(
              "  Discounted Price:  " + "90",
              style: GoogleFonts.montserrat(fontSize: 15),
            ),
            SizedBox(
              height: 10,
            )
          ],
        ),
      ));

输出

请帮忙,我哪里做错了。

customExpansionTile

Widget customExpansionTile(context, text,initiallyExpanded,leading,childern) {
  return GestureDetector(
   
    child: Container(
      width:MediaQuery.of(context).size.width*0.9,
    // height: 400,
    decoration: BoxDecoration(
      border: Border.all(color: HexColor("#6e6b7b")),
      color: Colors.white,
      borderRadius: BorderRadius.all(
        Radius.circular(10),
      ),
    ),
      child: ExpansionTile(
      title: Text(
          text,
          style: GoogleFonts.montserrat(
              fontSize: 18.0,
              fontWeight: FontWeight.bold,
              color: HexColor("#5344ed")),
        ),
        initiallyExpanded:initiallyExpanded,
        leading:leading,
        children: childern,
     ),
    ),
  );
}

如果您使用具有宽度和高度的 Container,您不需要像 ExpandedFlexible 这样的灵活小部件在父级,删除 Flexible,您也可以将 shrinkWrap: true 添加到 ListView.builder

Flexible(   /// <---  remove this parent or remove Container if you get flexible ListView.builder               
                  fit: FlexFit.loose,
                child: Container(
                  width:MediaQuery.of(context).size.width * 0.9,
                  height:MediaQuery.of(context).size.height*0.5,
                 child: ListView.builder(
                  itemCount: books.length,
                  itemBuilder: (context, index) {
                    final book = books[index];
                
                    return buildDiscount(book);
                  },
                ),
                )),

尝试使用Expanded类似

的东西
   Expanded: 
      child customExpansionTile(context, "Additional discount", true,
              Icon(Icons.add_task, color: HexColor("#5344ed")), [
              Expanded: child: 
                Column(
                  mainAxisSize: MainAxisSize.min,
                  .....