0

假设我有一个这样的gridview:

    class ImagesGrid extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final size = MediaQuery.of(context).size;
        final imagesData = Provider.of<Pictures>(context);
        final images = imagesData.items;
        return GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            childAspectRatio: size.width / size.height ,
          ),
          padding: const EdgeInsets.all(10.0),
          itemCount: images.length + 1,
          itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
            value: images[i],
            child: images.length == 0  ? Text('No images'):  ImageWidget(),
          ),
        );
      }
    }

现在,假设我希望在其中添加一个FlatButton作为GridView最后一项,它将用于添加另一个数据。我该怎么做?

4

1 回答 1

1
class ImagesGrid extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final imagesData = Provider.of<Pictures>(context);
    final images = imagesData.items;
    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        childAspectRatio: size.width / size.height ,
      ),
      padding: const EdgeInsets.all(10.0),
      itemCount: images.length + 1,
      itemBuilder: (ctx, i) {
        if(i==images.length) return FlatButton( // Add child and onPressed params );
        return ChangeNotifierProvider.value(
            value: images[i],
            child: images.length == 0  ? Text('No images'):  ImageWidget(),
        );
      },
    );
  }
}

希望我可能对你有所帮助。

于 2020-05-23T23:07:57.293 回答