0

I am trying to implement a horizontal Listview which shows my images pulled from the Flutter image_picker plugin.

its work fine if I do not use a Listview and only display a single image. however I am trying to use multiple images and as soon as I place within the Listview the widgets just shows up as black. my code for the Listview is as follows:

          @override
 Widget build(BuildContext context) {
return WillPopScope(

  onWillPop: () async {
    
    Navigator.pushReplacementNamed(context, 'inventory');
    return false;
  },
  child: Scaffold(
    appBar: AppBar(
      backgroundColor: Colors.green[700],
      title: Text(
        'MyApp',
        textAlign: TextAlign.center,
      ),
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back),
        onPressed: () {
          Navigator.pushReplacementNamed(
              context,
              'inventory');
        },
      ),

      actions: <Widget>[
        Padding(
          padding: EdgeInsets.only(right: 10.0),
          child: Container(

            width: 50,
            height: 50,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.white, //remove this when you add image.
            ),

            child: GestureDetector(
              onTap: (){
                Navigator.pushNamed(context,'profile');

              },
              child: Image(
                image:NetworkImage("imageUrl goes here"),
                width: 120,
                height: 120,
                fit: BoxFit.cover,

              ),
            ),
          ),
        )
      ],

    ),
    body: SafeArea(

      child: Column(

        children: [

          pickedFile == null ?
          GestureDetector(
            onTap: () {

              _showMyDialog();

              setState(() {


              });
            },

            child: Container(

              width: 200,
              height: 200,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('Click to add a Photo',textAlign: 
TextAlign.center,style: TextStyle(fontSize: 24),),
                  SizedBox(height: 20,),
                  Icon(

                    Icons.add_circle_outline,
                    color: Colors.grey[700],
                    size: 30,

                  ),

                ],
              ),
              margin: EdgeInsets.all(20.0),
              decoration: BoxDecoration(

                  border: Border.all(width: 2,color: Colors.grey),

                  shape: BoxShape.circle
              ),

            ),
          )
              :
          GestureDetector(
              onTap: () {

                _showMyDialog();

                setState(() {


                });
              },


              child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: _imageFileList!.length,
                  itemBuilder: (BuildContext context, int index) {
                   
 return kIsWeb

                        ? Image.network(_imageFileList![index].path)
                        : Image.file(File(_imageFileList! 
  [index].path));

                  }
              )

  ],
      ),

  ),),
  );
  }
}

I am not showing the whole code as the page is too long however the rest of the page works fine. if i remove the Listview builder and instead test using just the below it works fine. what am i doing wrong?

            child: Row(
              children: [
                        kIsWeb
                          ? Container(
                          child: Image.network(_imageFileList![index].path))
                          : Container(
                          child: Image.file(File(_imageFileList![index].path)));

                    }
                ),
              ],
            ),

Please help.

**Edited the above code with my full widget

Waseem Ahmed
  • 379
  • 1
  • 5
  • 17

2 Answers2

1

Either use Row or use ListView, not both.

You can use Row with List.generate:

Row(
  children: List.generate(_imageFileList!.length, 
    (i) => kIsWeb
         ? Container(child: Image.network(_imageFileList![index].path))
         : Container(child: Image.file(File(_imageFileList![index].path))
),

Or ListView exactly how you have it without the Row. ListView is probably the widget you’re wanting to use. Row will overflow if the content is larger than the width while ListView will act as its own ScrollView in that situation.

I’d also double-check that you need that Expanded widget. That’s typically for use inside a Row (or similar widgets).

JediBurrell
  • 1,069
  • 10
  • 24
  • I tried your suggestion and removed both the expanded and row and the listview still comes back black. i have updated my question to include the full scaffold and removed the unnecessary bits. – Waseem Ahmed Aug 01 '21 at 18:36
  • Managed to find the issue. it was a height issue. i had to add a container around the Listview.builder. Found it in another article. will paste a link to the comment below. – Waseem Ahmed Aug 01 '21 at 19:15
1

Managed to solve my issue. it was related to a height constraint which needed to be added. found the solution in this link

How to add a ListView to a Column in Flutter?

Waseem Ahmed
  • 379
  • 1
  • 5
  • 17