简体   繁体   English

如何处理 flutter 中小部件的唯一性?

[英]How to handle the uniqueness of widget in flutter?

I have a project.我有一个项目。 I am trying to make a scanner.我正在尝试制作扫描仪。 Everything is going well except for previews of the images.除了图像的预览,一切都很顺利。

在此处输入图像描述

This how preview should like this.预览应该是这样的。 I manage the make the view of this screenshot.我管理这个屏幕截图的视图。 But the problem is that I couldn't manage to make unique for every title.但问题是我无法让每个标题都独一无二。 I hold my titles in location table and my images in image table.我将标题保存在位置表中,将图像保存在图像表中。 Image table has location id so that in every location has different images from each other.图像表具有位置 ID,因此每个位置都有彼此不同的图像。 But my code is overriding all the locations so they are showing same images of one location.但是我的代码覆盖了所有位置,因此它们显示了一个位置的相同图像。 Like this:像这样: 在此处输入图像描述 : Only first one has a picture but my code show like they have all the same image. :只有第一个有图片,但我的代码显示它们都具有相同的图像。

This is my part of the homescreen code:这是我的主屏幕代码的一部分:

                   future: future,
                   builder: (ctx, snapshot) =>
                       snapshot.connectionState == ConnectionState.waiting
                           ? Center(
                               child: CircularProgressIndicator(),
                             )
                           : Consumer<Titles>(
                               builder: (ctx, titles, ch) => Expanded(
                                 child: ListView.builder(
                                     controller: ScrollController(),
                                     scrollDirection: Axis.vertical,
                                     shrinkWrap: true,
                                     itemCount: titles.items.length,
                                     itemBuilder: (ctx, i) { if (searchString=="") {
                                       getimages(titles.items[i].id);
   
                                       print(list_of_images);
                                       print("yukarıda");
   
                                       return TitleList(titleList:titles.items[i],images:list_of_images);
                                     }
                                     getimages(titles.items[i].id);    
                                     return titles.items[i].title!.contains(searchString) ?  TitleList(titleList:titles.items[i],images:list_of_images) :Container(); 
}

I am sending the title and images to TitleList widget:我将标题和图像发送到 TitleList 小部件:

I am getting the images like this:我得到这样的图像:

    List<File> list_of_images=[];
    void getimages(id)    {
        print("fark ne kadar");
      Future<List<Map<String, dynamic>>> futureTasks = DBHelper.selectImageforlist(id); //first make the querylist_of_images
       futureTasks.then((data){
        print("heyoo");
        print(data);
        for(int i = 0; i < data.length; i++) {
          list_of_images.add(File(data[i]['Image']));
        }
      });
    }

And this is the part of the TitleListWidget code:这是 TitleListWidget 代码的一部分:

var titleList;
  List<File> images;

  TitleList({required this.titleList,required this.images});

return GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(
          ImageScreen.routeName,
          arguments: {'id': titleList.id, 'name': titleList.title},
        );
      },
      child: Row(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                padding: EdgeInsets.only(top: 15, left: 12),
                child: Row(
                  children: [
                    Container(
                      child: Text(
                        truncate(titleList.title, length: 7),
                        style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            color: Colors.white),
                      ),
                    ),
                    Container(
                        padding: EdgeInsets.only(left: 15),
                        child: Text(
                          getdate(titleList.date),
                          style: TextStyle(
                              fontWeight: FontWeight.normal,
                              color: Colors.white),
                        )),
                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.only(left: 10),
                child: Row(children: [
                  Stack(
                    overflow: Overflow.visible,
                    children: makephoto(
                        images.length, images),
                  ),

                ]),
              )
            ],
          ),

I used map.我使用了 map。 I took every image to Map.我把每张图片都拍到了 Map。 The key of the image is the FileId.图像的键是 FileId。

 Future<Map> getimages() async   {
    print("fark ne kadar");
  Future<List<Map<String, dynamic>>> futureTasks = DBHelper.selectImageforlist(); //first make the querylist_of_images
   futureTasks.then((data){
    print("heyoo");
    print(data);
    for(int i = 0; i < data.length; i++) {
      if (identifier.containsKey(data[i]['fileId'])){
        identifier[data[i]['fileId']].add(File(data[i]['Image']));


      }
      else {
        identifier[data[i]['fileId']]=[File(data[i]['Image'])];
      }
    }
  });
   return identifier;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM