Skip to content

Commit

Permalink
chapter 27:finish
Browse files Browse the repository at this point in the history
  • Loading branch information
yagnik2411 committed Jun 2, 2023
1 parent 21ca0e0 commit dfd6c3e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
16 changes: 3 additions & 13 deletions lib/models/catalog.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
class CatalogModel {
static List<Item> items = [];


static List<Item> items = [
Item(
id: 1,
name: "iPhone 12 Pro",
desc: "Apple iPhone 12th generation",
price: 999,
color: "#33505a",
image:
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRISJ6msIu4AU9_M9ZnJVQVFmfuhfyJjEtbUm3ZK11_8IV9TV25-1uM5wHjiFNwKy99w0mR5Hk&usqp=CAc")
];
//get item by id
Item getById(int id) =>
Item getById(int id) =>
items.firstWhere((element) => element.id == id, orElse: null);

//get item by position
Item getByPosition(int pos) => items[pos];
Item getByPosition(int pos) => items[pos];
}

class Item {
Expand Down
37 changes: 29 additions & 8 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_catalog/core/mystore.dart';
import 'package:flutter_catalog/models/cart.dart';
import 'package:flutter_catalog/pages/homepage_widgets/catalog_list.dart';
import 'package:http/http.dart';
import 'package:velocity_x/velocity_x.dart';

import 'package:flutter_catalog/models/catalog.dart';
Expand All @@ -23,6 +26,8 @@ class HomePage extends StatefulWidget {

class _HomePageState extends State<HomePage> {
final String days = "3rd";
// final url = "https://api.jsonbin.io/b/604dbddb683e7e079c4eefd3";
// final url = Uri.http("api.jsonbin.io", "/b/604dbddb683e7e079c4eefd3", {'q': '{http}'});

@override
void initState() {
Expand All @@ -33,6 +38,9 @@ class _HomePageState extends State<HomePage> {
loadData() async {
final catalogJson =
await rootBundle.loadString("assets/files/catalog.json");

// final Response = await get(url);
// final catalogJson = Response.body;
final decodedData = jsonDecode(catalogJson);
var productsData = decodedData["products"];
CatalogModel.items = List.from(productsData)
Expand All @@ -43,15 +51,28 @@ class _HomePageState extends State<HomePage> {

@override
Widget build(BuildContext context) {
final CartModel cart = (VxState.store as MyStore).cart;
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, MyRoutes.cartRoute);
},
backgroundColor: context.theme.colorScheme.primary,
child: Icon(
CupertinoIcons.cart,
color: Colors.white,
floatingActionButton: VxConsumer(
notifications: {},
mutations: {AddMutation, RemoveMutation},
builder: (context, store, status) => FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, MyRoutes.cartRoute);
},
backgroundColor: context.theme.colorScheme.primary,
child: Icon(
CupertinoIcons.cart,
color: Colors.white,
),
).badge(
color: Colors.redAccent,
size: 22,
count: cart.items.length,
textStyle: TextStyle(
color: context.theme.colorScheme.primary,
fontSize: 15,
fontWeight: FontWeight.w600),
),
),
backgroundColor: context.canvasColor,
Expand Down
10 changes: 5 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ packages:
dependency: "direct main"
description:
name: google_fonts
sha256: "2776c66b3e97c6cdd58d1bd3281548b074b64f1fd5c8f82391f7456e38849567"
sha256: db5efba8106bd784a92c96cfd81716f4e06baab54e37de858488e9a00a764cad
url: "https://pub.dev"
source: hosted
version: "4.0.5"
version: "5.0.0"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
sha256: "4c3f04bfb64d3efd508d06b41b825542f08122d30bda4933fb95c069d22a4fa3"
Expand Down Expand Up @@ -337,10 +337,10 @@ packages:
dependency: "direct main"
description:
name: velocity_x
sha256: "3b60374a8c56fea3f53fba61440ebb9c753a39952a15d2e65bb2910873ebcac9"
sha256: "16afc758004143b2f5f6955c39ac4a7381c367067ee9f005764182a4624a44cb"
url: "https://pub.dev"
source: hosted
version: "4.0.0"
version: "4.1.0"
vxstate:
dependency: transitive
description:
Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
google_fonts: ^4.0.5
velocity_x: ^4.0.0
google_fonts: ^5.0.0
velocity_x: ^4.1.0
http: ^1.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit dfd6c3e

Please sign in to comment.