Skip to content

Commit 7814d0a

Browse files
committed
use getIt for the api connection and add changing pages with the drawer
1 parent 877f86f commit 7814d0a

File tree

10 files changed

+116
-60
lines changed

10 files changed

+116
-60
lines changed

lib/api_connection.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:convert';
2+
3+
import 'package:ka_client/product.dart';
4+
import 'package:http/http.dart' as http;
5+
6+
class APIConnection {
7+
String url = 'http://127.0.0.1:8000/products';
8+
late Future<List<Product>> products;
9+
10+
APIConnection() {
11+
products = fetchProducts();
12+
}
13+
14+
Future<List<Product>> fetchProducts() async {
15+
final response = await http.get(Uri.parse(url));
16+
17+
if (response.statusCode == 200) {
18+
List<Product> products = []; // TODO: Write sleak oneliner
19+
for (Map<String, dynamic> productString
20+
in jsonDecode(utf8.decode(response.body.codeUnits))) {
21+
// Convert to codeUnits to fix Umlauts
22+
products.add(Product.fromJson(productString));
23+
}
24+
return products;
25+
} else {
26+
throw Exception('Failed to load product');
27+
}
28+
}
29+
30+
31+
}

lib/main.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import 'package:flutter/material.dart';
2+
import 'package:get_it/get_it.dart';
3+
import 'package:ka_client/api_connection.dart';
24
import 'package:ka_client/style_components/app_scaffold.dart';
35

46
void main() {
5-
runApp(const MyApp());
7+
runApp(MyApp());
68
}
79

810
class MyApp extends StatelessWidget {
9-
const MyApp({super.key});
11+
MyApp({super.key}) {
12+
_initAPI();
13+
}
14+
15+
void _initAPI() => GetIt.I.registerSingleton<APIConnection>(APIConnection());
1016

1117
@override
1218
Widget build(BuildContext context) {
@@ -19,7 +25,7 @@ class MyApp extends StatelessWidget {
1925
),
2026
useMaterial3: true,
2127
),
22-
home: const AppScaffold(title: "KA Scraper"),
28+
home: const AppScaffold(),
2329
);
2430
}
2531
}

lib/overview.dart

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
import 'dart:convert';
2-
31
import 'package:flutter/material.dart';
4-
import 'package:http/http.dart' as http;
5-
import 'package:ka_client/price_chart.dart';
2+
import 'package:get_it/get_it.dart';
63
import 'package:ka_client/product.dart';
74

8-
Future<List<Product>> fetchProducts() async {
9-
final response = await http.get(Uri.parse('http://127.0.0.1:8000/products'));
10-
11-
if (response.statusCode == 200) {
12-
List<Product> products = []; // TODO: Write sleak oneliner
13-
for (Map<String, dynamic> productString
14-
in jsonDecode(utf8.decode(response.body.codeUnits))) {
15-
// Convert to codeUnits to fix Umlauts
16-
products.add(Product.fromJson(productString));
17-
}
18-
return products;
19-
} else {
20-
throw Exception('Failed to load product');
21-
}
22-
}
5+
import 'api_connection.dart';
236

247
class Overview extends StatefulWidget {
258
const Overview({super.key});
@@ -29,35 +12,32 @@ class Overview extends StatefulWidget {
2912
}
3013

3114
class _OverviewState extends State<Overview> {
32-
late Future<List<Product>> products;
33-
34-
@override
35-
void initState() {
36-
super.initState();
37-
products = fetchProducts();
38-
}
15+
Future<List<Product>> products = GetIt.I<APIConnection>().products;
3916

4017
@override
4118
Widget build(BuildContext context) {
42-
return Scaffold(
43-
appBar: AppBar(
44-
title: const Text('Overview'),
45-
backgroundColor: Theme.of(context).colorScheme.primary,
46-
),
47-
body: FutureBuilder(
48-
builder: (context, snapshot) {
49-
if (snapshot.hasData && snapshot.data != null) {
50-
List<Product> products = snapshot.data!; // Can't be null
51-
return PriceChart(prices: products.map((e) => e.price).toList());
52-
} else if (snapshot.hasData && snapshot.data == null) {
53-
return const Center(child: Text('No data'));
54-
} else if (snapshot.hasError) {
55-
return Center(child: Text('${snapshot.error}'));
56-
}
57-
return const Center(child: CircularProgressIndicator());
58-
},
59-
future: products,
60-
),
19+
return FutureBuilder(
20+
builder: (context, snapshot) {
21+
if (snapshot.hasData && snapshot.data != null) {
22+
List<Product> products = snapshot.data!; // Can't be null
23+
return ListView.builder(
24+
itemCount: products.length,
25+
itemBuilder: (context, index) {
26+
return ListTile(
27+
title: Text(products[index].title),
28+
subtitle: Text(products[index].location),
29+
trailing: Text(products[index].price.toString()),
30+
);
31+
},
32+
);
33+
} else if (snapshot.hasData && snapshot.data == null) {
34+
return const Center(child: Text('No data'));
35+
} else if (snapshot.hasError) {
36+
return Center(child: Text('${snapshot.error}'));
37+
}
38+
return const Center(child: CircularProgressIndicator());
39+
},
40+
future: products,
6141
);
6242
}
6343
}

lib/price_chart_page.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:get_it/get_it.dart';
3+
import 'package:ka_client/product.dart';
4+
import 'package:ka_client/style_components/price_chart.dart';
5+
6+
import 'api_connection.dart';
7+
8+
class PriceChartPage extends StatelessWidget {
9+
final Future<List<Product>> products = GetIt.I<APIConnection>().products;
10+
11+
PriceChartPage({super.key});
12+
13+
@override
14+
Widget build(BuildContext context) {
15+
return FutureBuilder(
16+
builder: (context, snapshot) {
17+
if (snapshot.hasData && snapshot.data != null) {
18+
List<Product> products = snapshot.data!; // Can't be null
19+
return PriceChart(prices: products.map((e) => e.price).toList());
20+
} else if (snapshot.hasData && snapshot.data == null) {
21+
return const Center(child: Text('No data'));
22+
} else if (snapshot.hasError) {
23+
return Center(child: Text('${snapshot.error}'));
24+
}
25+
return const Center(child: CircularProgressIndicator());
26+
},
27+
future: products,
28+
);
29+
}
30+
}

lib/style_components/app_scaffold.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import 'package:flutter/material.dart';
2+
import 'package:ka_client/price_chart_page.dart';
23
import 'package:ka_client/style_components/nav_drawer.dart';
34

45
import '../overview.dart';
5-
import '../price_chart.dart';
66

77
class AppScaffold extends StatefulWidget {
8-
final String title;
98

10-
const AppScaffold({super.key, required this.title});
9+
const AppScaffold({super.key});
1110

1211
@override
1312
State<AppScaffold> createState() => _AppScaffoldState();
@@ -16,9 +15,7 @@ class AppScaffold extends StatefulWidget {
1615
class _AppScaffoldState extends State<AppScaffold> {
1716
final _pages = {
1817
"Overview": const Overview(),
19-
"Price Chart": const PriceChart(
20-
prices: [1, 2, 3, 4, 5, 6],
21-
),
18+
"Price Chart": PriceChartPage(),
2219
};
2320

2421
String _currentPage = "";
@@ -46,7 +43,7 @@ class _AppScaffoldState extends State<AppScaffold> {
4643
},
4744
),
4845
appBar: AppBar(
49-
title: Text(widget.title),
46+
title: Text(_currentPage),
5047
),
5148
body: _pages[_currentPage],
5249
),

lib/style_components/nav_drawer.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ class NavDrawer extends StatelessWidget {
2929
Navigator.pop(context);
3030
},
3131
),
32-
const ListTile(
33-
title: Text('Price Graph'),
34-
leading: Icon(Icons.show_chart),
32+
ListTile(
33+
title: const Text('Price Chart'),
34+
leading: const Icon(Icons.show_chart),
35+
onTap: () {
36+
updateCurrentPage("Price Chart");
37+
Navigator.pop(context);
38+
},
3539
),
3640
],
3741
),

lib/price_chart.dart renamed to lib/style_components/price_chart.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
33

44
class PriceChart extends StatelessWidget {
55
final List<int> prices;
6-
// add prices to constructor
76
const PriceChart({super.key, required this.prices});
87

98
@override

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ packages:
9191
description: flutter
9292
source: sdk
9393
version: "0.0.0"
94+
get_it:
95+
dependency: "direct main"
96+
description:
97+
name: get_it
98+
sha256: e6017ce7fdeaf218dc51a100344d8cb70134b80e28b760f8bb23c242437bafd7
99+
url: "https://pub.dev"
100+
source: hosted
101+
version: "7.6.7"
94102
http:
95103
dependency: "direct main"
96104
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies:
3737
cupertino_icons: ^1.0.2
3838
http:
3939
fl_chart:
40+
get_it: ^7.6.7
4041

4142
dev_dependencies:
4243
flutter_test:

test/widget_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:ka_client/main.dart';
1313
void main() {
1414
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
1515
// Build our app and trigger a frame.
16-
await tester.pumpWidget(const MyApp());
16+
await tester.pumpWidget(MyApp());
1717

1818
// Verify that our counter starts at 0.
1919
expect(find.text('0'), findsOneWidget);

0 commit comments

Comments
 (0)