Created
June 12, 2019 15:37
-
-
Save Klerith/fd0d5434dea51a94e26583a70be07531 to your computer and use it in GitHub Desktop.
BeatsApp Parte 2: Provider y modelo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To parse this JSON data, do | |
// | |
// final productoModel = productoModelFromJson(jsonString); | |
import 'dart:convert'; | |
ProductoModel productoModelFromJson(String str) => ProductoModel.fromJson(json.decode(str)); | |
String productoModelToJson(ProductoModel data) => json.encode(data.toJson()); | |
class ProductoModel { | |
String nombre; | |
String url; | |
double precio; | |
bool favorito; | |
String titulo; | |
String subtitulo; | |
int bateria; | |
int color; | |
ProductoModel({ | |
this.nombre, | |
this.url, | |
this.precio, | |
this.favorito, | |
this.titulo, | |
this.subtitulo, | |
this.bateria, | |
this.color, | |
}); | |
factory ProductoModel.fromJson(Map<String, dynamic> json) => new ProductoModel( | |
nombre: json["nombre"], | |
url: json["url"], | |
precio: json["precio"], | |
favorito: json["favorito"], | |
titulo: json["titulo"], | |
subtitulo: json["subtitulo"], | |
bateria: json["bateria"], | |
color: json["color"], | |
); | |
Map<String, dynamic> toJson() => { | |
"nombre": nombre, | |
"url": url, | |
"precio": precio, | |
"favorito": favorito, | |
"titulo": titulo, | |
"subtitulo": subtitulo, | |
"bateria": bateria, | |
"color": color, | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:beats/src/models/producto_model.dart'; | |
class ProductosProvider extends ChangeNotifier{ | |
List<ProductoModel> _productos = new List(); | |
ProductosProvider() { | |
_cargarProductos(); | |
} | |
List<ProductoModel> get productos => _productos; | |
_cargarProductos() { | |
final prod1 = new ProductoModel( | |
nombre: 'Beats Studio3 Wireless', | |
titulo: 'Warriors', | |
subtitulo: 'Royal Blue', | |
precio: 349.95, | |
bateria: 45, | |
color: 0xff08399B, | |
favorito: false, | |
url: 'blue.png' | |
); | |
final prod2 = new ProductoModel( | |
nombre: 'Studio3 Wireless', | |
titulo: 'Warriors', | |
subtitulo: 'Royal Red', | |
precio: 300.95, | |
bateria: 30, | |
color: 0xff922325, | |
favorito: false, | |
url: 'black-red.png' | |
); | |
final prod3 = new ProductoModel( | |
nombre: 'Beats Wireless', | |
titulo: 'Fighter', | |
subtitulo: 'Space Grey', | |
precio: 449.95, | |
bateria: 50, | |
color: 0xff252525, | |
favorito: false, | |
url: 'black.png' | |
); | |
final prod4 = new ProductoModel( | |
nombre: 'Beats Studio3 Wireless', | |
titulo: 'Finest', | |
subtitulo: 'Royal Gold', | |
precio: 239.95, | |
bateria: 25, | |
color: 0xffC7B29B, | |
favorito: false, | |
url: 'gold.png' | |
); | |
final prod5 = new ProductoModel( | |
nombre: 'Beats Studio3', | |
titulo: 'Warriors', | |
subtitulo: 'Royal Red', | |
precio: 349.95, | |
bateria: 45, | |
color: 0xffD84C3B, | |
favorito: false, | |
url: 'red.png' | |
); | |
final prod6 = new ProductoModel( | |
nombre: 'Beats Studio3', | |
titulo: 'Warriors', | |
subtitulo: 'Royal Teal', | |
precio: 349.95, | |
bateria: 45, | |
color: 0xff23526D, | |
favorito: false, | |
url: 'teal.png' | |
); | |
this._productos.addAll([ | |
prod1, | |
prod2, | |
prod3, | |
prod4, | |
prod5, | |
prod6, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment