在Flutter中的GridView中显示图像?

时间:2019-11-18 05:59:26

标签: flutter dart

我想在GridView中显示图像列表。而且我无法做到这一点。我想知道资产中图像列表的数据类型,以及在这种情况下构造函数将如何提供帮助。我刚刚开始学习颤振,我不知道如何使用它。

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 个答案:

答案 0 :(得分:0)

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: () {},
      )