Skip to content

Commit f3f1253

Browse files
committed
implement retrying the query, visual overhaul
1 parent 20da566 commit f3f1253

File tree

5 files changed

+289
-69
lines changed

5 files changed

+289
-69
lines changed

lib/components/data_processor.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DataResponse {
1212
class DataProcessor {
1313
final String apiUrl = 'https://humanova.space/api/price';
1414

15-
Future<DataResponse> getPrices(String imageBase64) async {
15+
Future<DataResponse> getPricesByImage(String imageBase64) async {
1616
try {
1717
final response = await http.post(
1818
apiUrl,
@@ -30,9 +30,28 @@ class DataProcessor {
3030
}
3131
}
3232

33+
Future<DataResponse> getPricesByQuery(String query) async {
34+
try {
35+
final response = await http.post(
36+
apiUrl,
37+
headers: {
38+
'Content-Type': 'application/json',
39+
},
40+
body: json.encode({'text': query}),
41+
);
42+
43+
return DataResponse(
44+
isSuccess: response.statusCode == 200,
45+
data: json.decode(utf8.decode(response.bodyBytes)));
46+
} catch (e) {
47+
return DataResponse(isSuccess: false, data: e.toString());
48+
}
49+
}
50+
3351
Product parseProduct(dynamic jsonData) {
3452
final String name = jsonData['name'];
3553
final String image = jsonData['image'];
54+
final String query = jsonData['query'];
3655
final List<dynamic> pricesData = jsonData['prices'];
3756

3857
final List<Price> prices = pricesData
@@ -42,6 +61,6 @@ class DataProcessor {
4261
))
4362
.toList();
4463

45-
return Product(name: name, image: image, prices: prices);
64+
return Product(name: name, image: image, query: query, prices: prices);
4665
}
4766
}

lib/models/product.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
class Product {
22
final String name;
3-
final String image; //image url
3+
final String image;
4+
final String query;
45
final List<Price> prices;
56

6-
Product({this.name, this.image, this.prices});
7+
Product({this.name, this.image, this.query, this.prices});
78

89
factory Product.fromJson(Map<String, dynamic> json) {
910
var priceList = json['prices'] as List;
10-
List<Price> prices =
11-
priceList.map((priceJson) => Price.fromJson(priceJson)).toList();
11+
List<Price> prices = priceList.map((priceJson) => Price.fromJson(priceJson)).toList();
1212

1313
return Product(
1414
name: json['name'] as String,
1515
image: json['image'] as String,
16+
query: json['query'] as String,
1617
prices: prices,
1718
);
1819
}
@@ -21,6 +22,7 @@ class Product {
2122
final Map<String, dynamic> data = new Map<String, dynamic>();
2223
data['name'] = this.name;
2324
data['image'] = this.image;
25+
data['query'] = this.query;
2426
data['prices'] = this.prices.map((price) => price.toJson()).toList();
2527
return data;
2628
}
@@ -32,10 +34,10 @@ class Price {
3234

3335
Price({this.store, this.price});
3436

35-
factory Price.fromJson(Map<String, dynamic> json) {
37+
factory Price.fromJson(Map<String, dynamic> json) {
3638
return Price(
37-
store: json['store'],
38-
price: json['price'].toDouble(),
39+
store: json['store'] as String,
40+
price: json['price'] as double,
3941
);
4042
}
4143

@@ -44,5 +46,5 @@ class Price {
4446
'store': store,
4547
'price': price,
4648
};
47-
}
49+
}
4850
}

lib/screens/home_screen.dart

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,93 @@ import 'package:prc_app/screens/image_screen.dart';
55
class HomeScreen extends StatelessWidget {
66
@override
77
Widget build(BuildContext context) {
8+
final screenHeight = MediaQuery.of(context).size.height;
9+
final screenWidth = MediaQuery.of(context).size.width;
10+
final buttonSize = screenHeight * 0.3;
11+
812
return Scaffold(
913
appBar: AppBar(
10-
title: Text('PRC'),
14+
title: Text('PRC: Shop Smartly!'),
1115
),
1216
body: Center(
1317
child: Column(
1418
mainAxisAlignment: MainAxisAlignment.center,
1519
children: <Widget>[
16-
RaisedButton(
17-
onPressed: () {
18-
Navigator.push(
19-
context,
20-
MaterialPageRoute(
21-
builder: (context) => SearchHistoryScreen()),
22-
);
23-
},
24-
child: Text('View Search History'),
25-
),
26-
SizedBox(height: 20),
27-
RaisedButton(
28-
onPressed: () {
29-
Navigator.push(
30-
context,
31-
MaterialPageRoute(builder: (context) => ImageScreen()),
32-
);
33-
},
34-
child: Text('Find Prices'),
20+
Container(
21+
width: screenWidth * 0.8,
22+
child: Column(
23+
children: [
24+
GestureDetector(
25+
onTap: () {
26+
Navigator.push(
27+
context,
28+
MaterialPageRoute(builder: (context) => ImageScreen()),
29+
);
30+
},
31+
child: Container(
32+
width: double.infinity,
33+
height: buttonSize,
34+
decoration: BoxDecoration(
35+
color: Colors.orange,
36+
borderRadius: BorderRadius.circular(10.0),
37+
boxShadow: [
38+
BoxShadow(
39+
color: Colors.black.withOpacity(0.2),
40+
blurRadius: 10.0,
41+
spreadRadius: 2.0,
42+
),
43+
],
44+
),
45+
child: Center(
46+
child: Row(
47+
mainAxisAlignment: MainAxisAlignment.center,
48+
children: [
49+
Icon(
50+
Icons.search,
51+
size: buttonSize * 0.7,
52+
color: Colors.white,
53+
),
54+
55+
],
56+
),
57+
),
58+
),
59+
),
60+
SizedBox(height: screenHeight * 0.05),
61+
GestureDetector(
62+
onTap: () {
63+
Navigator.push(
64+
context,
65+
MaterialPageRoute(
66+
builder: (context) => SearchHistoryScreen(),
67+
),
68+
);
69+
},
70+
child: Container(
71+
width: double.infinity,
72+
height: buttonSize,
73+
decoration: BoxDecoration(
74+
color: Colors.blue,
75+
borderRadius: BorderRadius.circular(10.0),
76+
boxShadow: [
77+
BoxShadow(
78+
color: Colors.black.withOpacity(0.2),
79+
blurRadius: 10.0,
80+
spreadRadius: 2.0,
81+
),
82+
],
83+
),
84+
child: Center(
85+
child: Icon(
86+
Icons.history,
87+
size: buttonSize * 0.6,
88+
color: Colors.white,
89+
),
90+
),
91+
),
92+
),
93+
],
94+
),
3595
),
3696
],
3797
),

0 commit comments

Comments
 (0)