与颜色相关的颤动图像

Posted

技术标签:

【中文标题】与颜色相关的颤动图像【英文标题】:Flutter image associated with color 【发布时间】:2019-12-15 21:32:14 【问题描述】:

我正在制作一个 API 来显示产品图片和颜色, 每当用户按下颜色按钮时,与该颜色关联的图像应使用 PageView.builder 显示。 如果这是我的 json,我如何为 api 制作模型工厂?

[
    
        "id": 7,
        "name": "1",
        "descriptions": "1",
        "price": 1,
        "discount": 1,
        "category": "new",
        "quantity": 1,
        "brand": "1",
        "image": "",
        "created_at": "2019-08-04 09:07:25",
        "updated_at": "2019-08-04 09:07:25",
        "images": 
            "url":"1564909645iKiw2LkoEcQIIhB4MTZJTUfwTREleWH4wEuvmRPd.png",
            "color":"#fffddd"
        ,
        "tags": [
            "large"
        ],
        "sizes": []
    
]

我的模型和工厂:

class Response1 
  final String createdAt;
  final int id;
  final int quantity;
  final double price;
  final String name;
  final String descriptions;
  final String updatedAt;
  final String image;
  final int weight;
  final List images;
  final List tags;
  final List sizes;
  Response1(
      this.createdAt,
        this.id,
        this.quantity,
        this.price,
        this.name,
        this.descriptions,
        this.updatedAt,
        this.image,
        this.weight,
        this.images,
        this.tags,
        this.sizes);

  factory Response1.fromJson(Map<String, dynamic> json) 
    return Response1(
      createdAt: json['created_at'] as String,
      id: json['id'] as int,
      quantity: json['quantity'] as int,
      price: _toDouble(json['price']),
      name: json['name'] as String,
      updatedAt: json['updated_at'] as String,
      image: json['image'] as String,
      descriptions: json['descriptions'] as String,
      weight: json['weight'] as int,
      images: json['images'] as List,
      tags: json['tags'] as List,
      sizes: json['sizes'] as List,
    );
  

  Map<String, dynamic> toJson() 
    return 
      'created_at': createdAt,
      'id': id,
      'quantity': quantity,
      'price': price,
      'name': name,
      'updated_at': updatedAt,
      'image': image,
      'weight': weight,
      'images': images,
      'tags': tags,
      'sizes': sizes,
    ;
  

我该怎么做?还有其他方法吗? 谢谢

【问题讨论】:

您正在寻找图像列表中目前只有一个对象? 【参考方案1】:

您可以使用https://app.quicktype.io/ 来帮助您简化此操作 你需要的代码请看下面

// To parse this JSON data, do
//
//     final response1 = response1FromJson(jsonString);

import 'dart:convert';

List<Response1> response1FromJson(String str) => new List<Response1>.from(json.decode(str).map((x) => Response1.fromJson(x)));

String response1ToJson(List<Response1> data) => json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));

class Response1 
    int id;
    String name;
    String descriptions;
    int price;
    int discount;
    String category;
    int quantity;
    String brand;
    String image;
    DateTime createdAt;
    DateTime updatedAt;
    Images images;
    List<String> tags;
    List<dynamic> sizes;

    Response1(
        this.id,
        this.name,
        this.descriptions,
        this.price,
        this.discount,
        this.category,
        this.quantity,
        this.brand,
        this.image,
        this.createdAt,
        this.updatedAt,
        this.images,
        this.tags,
        this.sizes,
    );

    factory Response1.fromJson(Map<String, dynamic> json) => new Response1(
        id: json["id"],
        name: json["name"],
        descriptions: json["descriptions"],
        price: json["price"],
        discount: json["discount"],
        category: json["category"],
        quantity: json["quantity"],
        brand: json["brand"],
        image: json["image"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        images: Images.fromJson(json["images"]),
        tags: new List<String>.from(json["tags"].map((x) => x)),
        sizes: new List<dynamic>.from(json["sizes"].map((x) => x)),
    );

    Map<String, dynamic> toJson() => 
        "id": id,
        "name": name,
        "descriptions": descriptions,
        "price": price,
        "discount": discount,
        "category": category,
        "quantity": quantity,
        "brand": brand,
        "image": image,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "images": images.toJson(),
        "tags": new List<dynamic>.from(tags.map((x) => x)),
        "sizes": new List<dynamic>.from(sizes.map((x) => x)),
    ;


class Images 
    String url;
    String color;

    Images(
        this.url,
        this.color,
    );

    factory Images.fromJson(Map<String, dynamic> json) => new Images(
        url: json["url"],
        color: json["color"],
    );

    Map<String, dynamic> toJson() => 
        "url": url,
        "color": color,
    ;

【讨论】:

以上是关于与颜色相关的颤动图像的主要内容,如果未能解决你的问题,请参考以下文章

如何在颤动中显示具有顶部和底部渐变阴影的图像?

如何在飞镖颤动中转换图像 dpi(每英寸点数)?

无法在颤动中使用 dio 将图像上传到 api

电影口碑与海报图像的相关性分析

如何将图像放在图像中颤动

在颤动中将图像从图像选择器保存到firebase存储