【问题标题】:Add image to list Flutter将图像添加到列表 Flutter
【发布时间】:2020-01-06 13:35:14
【问题描述】:

我想做这样的事情。首先在listView 中有一个按钮。单击按钮时,它将允许用户从图库中选择图像。

选择后,选中的图片将被放置到ListView,按钮将移动到下图的另一个项目。

这是我的输出。

之前

之后

我的代码

 Container(
                  child: StreamBuilder<List<Asset>>(
                      stream: _bloc.uploadImageStream,
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          return Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              images == null
                                  ? new Container(
                                      height: 300.0,
                                      width: 300.0,
                                      child: new Icon(
                                        Icons.image,
                                        size: 250.0,
                                        color: Theme.of(context).primaryColor,
                                      ),
                                    )
                                  : new SizedBox(
                                      height: 200.0,
                                      width: double.infinity,
                                      child: new ListView.builder(
                                        scrollDirection: Axis.horizontal,
                                        itemCount: snapshot.data.length,
                                        itemBuilder: (BuildContext context,
                                                int index) =>
                                            new Padding(
                                                padding:
                                                    const EdgeInsets.all(5.0),
                                                child: AssetThumb(
                                                  height: 200,
                                                  width: 200,
                                                  asset: snapshot.data[index],
                                                )),
                                      ),
                                    ),
                              IconButton(
                                  icon: Center(
                                    child: Icon(
                                      Icons.camera,
                                      size: 30.0,
                                    ),
                                  ),
                                  onPressed: () {
                                    _bloc.getImage();
                                  })
                            ],
                          );
                        } else {
                          return IconButton(
                              icon: Center(
                                child: Icon(
                                  Icons.camera,
                                  size: 30.0,
                                ),
                              ),
                              onPressed: () {
                                _bloc.getImage();
                              });
                        }
                      }))

【问题讨论】:

    标签: listview flutter dart


    【解决方案1】:

    我按照您使用的包中的一些说明进行了操作,该包来自 docs

    我通过编辑一些文档代码找到了解决方案。

    在我编辑之前

    Widget buildGridView() {
        return GridView.count(
          crossAxisCount: 3,
          children: List.generate(images.length, (index) {
            Asset asset = images[index];
            return AssetThumb(
              asset: asset,
              width: 300,
              height: 300,
            );
          }),
        );
      }
    

    经过我的修改

    Widget buildGridView() {
        return GridView.count(
          crossAxisCount: 3,
          children: List.generate(images.length + 1, (index) {
            if (index < images.length) {
              Asset asset = images[index];
              return AssetThumb(
                asset: asset,
                width: 300,
                height: 300,
              );
            } else {
              return GestureDetector(
                child: Image.asset(
                  "assets/images/add.jpg",
                  width: 300,
                  height: 300,
                ),
                onTap: loadAssets,
              );
            }
          }),
        );
      }
    

    所以根据你的代码,它将是

     new SizedBox(
          height: 200.0,
          width: double.infinity,
          child: new ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: snapshot.data.length + 1,
            itemBuilder: (BuildContext context, int index) => new Padding(
                padding: const EdgeInsets.all(5.0),
                child: (index < snapshot.data.length) ? AssetThumb(
                  height: 200,
                  width: 200,
                  asset: snapshot.data[index],
                ) : GestureDetector(
                child: Image.asset(
                  "assets/images/add.jpg",
                  width: 200,
                  height: 200,
                ),
                onTap: loadAssets,
              )),
          ),
    

    我希望它对你有用。

    【讨论】:

    • 好的,我确实加入了。
    猜你喜欢
    • 2021-06-08
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多