Home > Mobile >  Something wrong with my ListVierw.builder
Something wrong with my ListVierw.builder

Time:07-03

My listView show an output like this, I already try to add padding: EdgeInsets.zero, in the list view but it didn't work. Here is my coding

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Seller Home'),
        centerTitle: true,
        actions: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ElevatedButton(onPressed: () =>  Navigator.pop(context),
                style:  ElevatedButton.styleFrom(
                    primary: Color.fromRGBO(228, 144, 56,1),
                    elevation: 5,  // Elevation
                    shadowColor: Colors.grey,
                    padding: EdgeInsets.all(10),
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))// Shadow Color
                ),
                child: Text("Switch to Buyer"),
              ),
              SizedBox(width: 5,)
            ],
          ),
        ],
      ),
      // Using StreamBuilder to display all products from Firestore in real-time
      body: StreamBuilder(
        stream: ref.snapshots(),
        builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
          if (streamSnapshot.hasData) {
            return ListView.builder(
              padding: EdgeInsets.zero,
              itemCount: streamSnapshot.data!.docs.length,
              itemBuilder: (context, index) {
                final DocumentSnapshot documentSnapshot =
                (streamSnapshot.data!.docs[index]);
                DateTime dt = (documentSnapshot['endDateTime'] as Timestamp).toDate();
                    if(documentSnapshot['userid'] == CurrentUserId()){
                      //DateTime dt = (documentSnapshot['endDateTime'] as Timestamp).toDate();
                      return Padding(
                          padding: const EdgeInsets.symmetric(vertical: 6),
                            child: GestureDetector(
                              onTap: (){
                                Navigator.of(context).push(MaterialPageRoute(builder: (context)=>ProductDetail(id: documentSnapshot['id'])));
                              },
                              child: Container(
                                padding:
                                EdgeInsets.all(8),
                                decoration: BoxDecoration(
                                  boxShadow: [
                                    BoxShadow(
                                      color: Colors.grey.withOpacity(0.5),
                                      spreadRadius: 5,
                                      blurRadius: 7,
                                      offset: Offset(0, 3), // changes position of shadow
                                    ),
                                  ],
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(15),
                                ),
                                child: Row(
                                  children: [
                                    SizedBox(
                                      width: 88,
                                      child: AspectRatio(aspectRatio: 0.88,
                                        child: ImageAvatar(documentSnapshot['imageURL'][0]),
                                      ),
                                    ),
                                    SizedBox(width: 20),
                                    Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          documentSnapshot['nameP'],
                                          style: const TextStyle(
                                              color: Colors.black, fontSize: 16),
                                          maxLines: 2,
                                        ),
                                        SizedBox(height: 10),
                                        Text.rich(
                                          TextSpan(
                                            text: "Category "  
                                                (documentSnapshot['categoryP'])
                                                    .toString(),
                                            style: const TextStyle(
                                                fontWeight: FontWeight.w600,
                                                fontSize: 18
                                            ),
                                          ),
                                        ),
                                        Container(
                                            padding: EdgeInsets.all(5),
                                            child: CountDownText(due: dt,
                                              finishedText: "The Auction has Ended",
                                              showLabel: true,
                                              longDateName: false,
                                              daysTextShort: " D ",
                                              hoursTextShort: " H ",
                                              minutesTextShort: " M ",
                                              secondsTextShort: " S ",
                                              style: TextStyle(color: Colors.red,fontWeight: FontWeight.w600,
                                                  fontSize: 18),
                                            )
                                        ),
                                      ],
                                    ),
                                    const Spacer(),
                                    Column(
                                      children: const [
                                        SizedBox(
                                          height: 30,
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                            ),

                      );
                    }
                return Card();

              },
            );

          }


          return const Center(
            child: CircularProgressIndicator(),
          );
        },
      ),
    );
  }

And here is my ImageAvatr widget and downloadURL

Future<String> downloadURL(String images) async{
    url = await st.ref('products/$images').getDownloadURL();
    return url;
  }

Widget ImageAvatar(String image) => FutureBuilder( future: downloadURL(image) ,
    builder: (BuildContext context, AsyncSnapshot snapshot){
    if(snapshot.hasData){
      return Image.network(snapshot.data);
    }
    return const Center(
      child: CircularProgressIndicator(),
    );
    }
);

CodePudding user response:

If there is no data then return a return SizedBox.shrink(); instead of return Card();

Now if there is no data it is returning a Card which has a default size, but if you use SizedBox.shrink then it will not take any extra space

  • Related