0

I want to display a list of images in GridView. And I'm not able to do that. I want to know the data type for a list of images from the assets and how a constructor would help in this case. I've just started to learn flutter and I don't know how to use it.

class Home extends StatefulWidget {
  final String title;
  Home({this.title}) : super();
  @override
  _HomeState createState() => _HomeState();
  }
  @override
  Widget build(BuildContext context) {
var gridView = new GridView.builder(
    itemCount: 20,
    gridDelegate:
        new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
    itemBuilder: (BuildContext context, int index) {
      return new GestureDetector(
        child: new Card(
          elevation: 10,
          child: new Container(
                // child: *Image list as child*, but don't know about the list datatype hence not created it!
            alignment: Alignment.center,
          ),
        ),
        onTap: () {},
      );
    });
return new Scaffold(
  appBar: new AppBar(
    backgroundColor: Colors.red,
    title: new Text("Flutter TabBar"),
  ),
  body: Container(
    padding: EdgeInsets.all(10),
    child: gridView,
  ),
);
}
}
1

1 Answer 1

2
GestureDetector(
        child: new Card(
          elevation: 10,
          child: new Container(
            // child: *Image list as child*, but don't know about the list datatype hence not created it!
            alignment: Alignment.center,
            child:GridView.builder(
            itemCount: items.length,
            gridDelegate:

            // crossAxisCount stands for number of columns you want for displaying
            SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
            itemBuilder: (BuildContext context, int index) {

              // return your grid widget here, like how your images will be displayed
              return Image.network('https://picsum.photos/250?image=9',);
              }),
           ),
        ),
        onTap: () {},
      )
Sign up to request clarification or add additional context in comments.

2 Comments

The thing is that I have 23 images to display in "Cards", I need to make a List of Images so that I can use it directly into the child property of the Card. But I don't know the data type for the list of images. So if you guys can help in this case?
Just create a list of String and all the image paths in string. To load an image from server(network path) you can use Image.network() widget, To load image from a file you can use Image.file() widget and To load image from assets you can use Image.asset() widget.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.