【问题标题】:Flutter: Displaying Images List from Device GalleryFlutter:显示设备库中的图像列表
【发布时间】:2020-10-08 15:59:04
【问题描述】:

我有照片的 ListView,我试图从图库中获取照片并将它们显示在屏幕上,但问题是当我按下添加新照片时,整张照片都会更改为新照片。

我想单独更改每张照片而不更改其他照片。

File _image;
final picker = ImagePicker();

Future getImage() async {
  final image = await picker.getImage(source: ImageSource.gallery);

  setState(() {
    _image = File(image.path);
    print((image.path));
  });
}

 child: ListView.builder(
    scrollDirection: Axis.horizontal,
    itemCount: 1,
      itemBuilder: (BuildContext context, int index){
      return Row(
        children: <Widget>[
          GestureDetector(
            onTap: (){
              getImage();

            },
            child: Padding(
              padding: EdgeInsets.only(left:5.0, right:5,top: 10),
              child: Container(
                height: 100.0,
                width: 100.0,
                decoration: BoxDecoration(
                  border: Border.all(),
                  image: DecorationImage(
                    image: _image == null ? AssetImage(
                        'assets/images/add_pic.jpg') : FileImage(_image),
                    fit: BoxFit.fill,
                  ),
                  shape: BoxShape.rectangle,
                ),
              ),
            ),
          ),

【问题讨论】:

    标签: image listview flutter dart


    【解决方案1】:

    从这段代码开始:

    List<File> _images = [];
    final picker = ImagePicker();
    
    Future getImage(int index) async {
      final image = await picker.getImage(source: ImageSource.gallery);
    
      setState(() {
        _images[index] = File(image.path);
        print((image.path));
      });
    }
    
     child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: _images.length + 1,
          itemBuilder: (BuildContext context, int index){
          if(index == _images.length){
            return GestureDetector(
                onTap: (){
                  getImage(index);
                },
                child: Padding(
                  padding: EdgeInsets.only(left:5.0, right:5,top: 10),
                  child: Container(
                    height: 100.0,
                    width: 100.0,
                    decoration: BoxDecoration(
                      border: Border.all(),
                      image: DecorationImage(
                        image: AssetImage('assets/images/add_pic.jpg'),
                        fit: BoxFit.fill,
                      ),
                      shape: BoxShape.rectangle,
                    ),
                  ),
                ),
             );
          }
          return Row(
            children: <Widget>[
              GestureDetector(
                onTap: (){
                  getImage();
                },
                child: Padding(
                  padding: EdgeInsets.only(left:5.0, right:5,top: 10),
                  child: Container(
                    height: 100.0,
                    width: 100.0,
                    decoration: BoxDecoration(
                      border: Border.all(),
                      image: DecorationImage(
                        image: FileImage(_image[index]),
                        fit: BoxFit.fill,
                      ),
                      shape: BoxShape.rectangle,
                    ),
                  ),
                ),
              ),
    

    【讨论】:

    • 它对你有用吗?因为我在应用它时收到此错误:错误:无法将元素类型“Set”分配给列表类型“Widget”。
    • 您的代码中是否使用了Set?您可以分享完整的错误吗?
    • 代码中没有设置,这是完整的错误,后面跟着这些错误:错误:意外的文本“返回”。 ([hpxksa] lib/Screens/ad_images_scroll_view.dart:46 处的 unexpected_token)错误:未定义名称“_image”。 ([hpxksa] lib/Screens/ad_images_scroll_view.dart:58 处的 undefined_identifier)错误:未定义名称“_image”。 ([hpxksa] lib/Screens/ad_images_scroll_view.dart:59 处的 undefined_identifier)错误:应找到“}”。 (expected_token at [hpxksa] lib/Screens/ad_images_scroll_view.dart:66) 错误:预期有 1 个位置参数,但找到了 0 个。
    • 只需按照错误中的行号(46、58、59和66)并解决它们;其中之一是将行image: FileImage(_image[index]),替换为image: FileImage(_images[index]),,区别是's' ...等等
    • 那是因为我在浏览器中编写代码,而不是在 IDE 中
    猜你喜欢
    • 2018-11-06
    • 2023-03-08
    • 2021-11-04
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    相关资源
    最近更新 更多