Skip to content

Instantly share code, notes, and snippets.

@khatthaphone
Last active March 11, 2023 08:40
Show Gist options
  • Save khatthaphone/dacdbca88ac7eac4c64ea6f79da7d4e4 to your computer and use it in GitHub Desktop.
Save khatthaphone/dacdbca88ac7eac4c64ea6f79da7d4e4 to your computer and use it in GitHub Desktop.
Flutter STEM Lab
// To parse this JSON data, do
//
// final products = productsFromJson(jsonString);
import 'dart:convert';
ProductsModel productsFromJson(String str) => ProductsModel.fromJson(json.decode(str));
String productsToJson(ProductsModel data) => json.encode(data.toJson());
class ProductsModel {
ProductsModel({
this.products,
});
List<ProductModel>? products;
factory ProductsModel.fromJson(Map<String, dynamic> json) => ProductsModel(
products: List<ProductModel>.from(json["products"].map((x) => ProductModel.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"products": List<dynamic>.from(products!.map((x) => x.toJson())),
};
}
class ProductModel {
ProductModel({
this.id,
this.title,
this.description,
this.price,
this.discountPercentage,
this.rating,
this.stock,
this.brand,
this.category,
this.thumbnail,
this.images,
});
int? id;
String? title;
String? description;
int? price;
double? discountPercentage;
double? rating;
int? stock;
String? brand;
String? category;
String? thumbnail;
List<String>? images;
factory ProductModel.fromJson(Map<String, dynamic> json) => ProductModel(
id: json["id"],
title: json["title"],
description: json["description"],
price: json["price"],
discountPercentage: json["discountPercentage"].toDouble(),
rating: json["rating"].toDouble(),
stock: json["stock"],
brand: json["brand"],
category: json["category"],
thumbnail: json["thumbnail"],
images: List<String>.from(json["images"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"description": description,
"price": price,
"discountPercentage": discountPercentage,
"rating": rating,
"stock": stock,
"brand": brand,
"category": category,
"thumbnail": thumbnail,
"images": List<dynamic>.from(images!.map((x) => x)),
};
}
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:stem_flutter/dio_client.dart';
class ProductsPage extends StatefulWidget {
const ProductsPage({super.key});
@override
State<ProductsPage> createState() => _ProductsPageState();
}
class _ProductsPageState extends State<ProductsPage> {
final _dio = DioClient();
@override
void initState() {
_dio.getProducts().then((value) {
print('Products data: $value');
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder(
future: _dio.getProducts(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data != null) {
// return Text('${snapshot.data['products']}');
final products = snapshot.data['products'];
return ListView.builder(
padding: const EdgeInsets.all(4),
itemCount: products.length,
itemBuilder: (context, index) {
final product = products[index];
return Card(
elevation: 4,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Column(
children: [
// Text('${product}'),
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: CachedNetworkImage(
imageUrl: '${product['thumbnail']}',
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${product['title']}',
style: const TextStyle(fontWeight: FontWeight.bold),
),
Text(
'${product['brand']}',
style: const TextStyle(color: Colors.grey),
),
],
),
const Spacer(),
Text(
'(-${product['discountPercentage']}%) ',
style: const TextStyle(
color: Colors.red,
fontSize: 12,
),
),
Text(
'\$${product['price']}',
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
],
),
),
],
),
);
},
);
}
return const Center(child: CircularProgressIndicator());
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment