Skip to content

Commit 681e0ec

Browse files
committed
MinPrice und MaxPrice Implementierung
1 parent 0cbb501 commit 681e0ec

File tree

5 files changed

+174
-26
lines changed

5 files changed

+174
-26
lines changed

lib/api_connection.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import 'dart:convert';
2-
import 'dart:io';
32

43
import 'package:ka_client/product.dart';
54
import 'package:http/http.dart' as http;
5+
import 'package:shared_preferences/shared_preferences.dart';
66

77
class APIConnection {
88
// Replace localhost with 10.0.2.2 for Android Emulator
99
String baseUrl =
10-
Platform.isAndroid ? 'http://10.0.2.2:8000' : 'http://127.0.0.1:8000';
10+
'http://127.0.0.1:8000';
1111
late Future<List<String>> categories;
1212
List<Product> products = [];
1313
String selectedCategory = '';
14+
int minPrice = 0;
15+
int maxPrice = 1000000;
1416

1517
APIConnection() {
1618
categories = fetchCategories();
@@ -44,6 +46,12 @@ class APIConnection {
4446
// Convert to codeUnits to fix Umlauts
4547
products.add(Product.fromJson(productString));
4648
}
49+
50+
SharedPreferences prefs = await SharedPreferences.getInstance();
51+
minPrice = prefs.getInt('minPrice') ?? 0;
52+
maxPrice = prefs.getInt('maxPrice') ?? 1000000;
53+
products = products.where((element) => element.price >= minPrice && element.price <= maxPrice).toList();
54+
4755
return products;
4856
} else {
4957
throw Exception('Failed to load product');

lib/style_components/nav_drawer.dart

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
3+
import 'package:shared_preferences/shared_preferences.dart';
4+
35

46
class NavDrawer extends StatelessWidget {
57
final Function(String page) updateCurrentPage;
@@ -48,29 +50,44 @@ class NavDrawer extends StatelessWidget {
4850
_formKey.currentState!.save();
4951
}
5052
},
51-
child: Column(
52-
children: [
53-
TextFormField(
54-
keyboardType: TextInputType.number,
55-
inputFormatters: [
56-
FilteringTextInputFormatter.digitsOnly,
57-
],
58-
decoration: const InputDecoration(
59-
labelText: 'min price',
60-
icon: Icon(Icons.search),
61-
),
62-
),
63-
TextFormField(
64-
keyboardType: TextInputType.number,
65-
inputFormatters: [
66-
FilteringTextInputFormatter.digitsOnly,
53+
child: FutureBuilder(
54+
future: SharedPreferences.getInstance(),
55+
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
56+
if(snapshot.connectionState == ConnectionState.waiting) {
57+
return const CircularProgressIndicator();
58+
}
59+
SharedPreferences prefs = snapshot.data;
60+
return Column(
61+
children: [
62+
TextFormField(
63+
keyboardType: TextInputType.number,
64+
initialValue: () {
65+
return prefs.getInt('minPrice') == null ? '' : prefs.getInt('minPrice').toString();
66+
}(),
67+
onChanged: (value) {
68+
prefs.setInt('minPrice', int.parse(value));
69+
},
70+
decoration: const InputDecoration(
71+
labelText: 'min price',
72+
icon: Icon(Icons.search),
73+
),
74+
),
75+
TextFormField(
76+
keyboardType: TextInputType.number,
77+
initialValue: () {
78+
return prefs.getInt('maxPrice') == null ? '' : prefs.getInt('maxPrice').toString();
79+
}(),
80+
onChanged: (value) {
81+
prefs.setInt('maxPrice', int.parse(value));
82+
},
83+
decoration: const InputDecoration(
84+
labelText: 'max price',
85+
icon: Icon(Icons.search),
86+
),
87+
),
6788
],
68-
decoration: const InputDecoration(
69-
labelText: 'max price',
70-
icon: Icon(Icons.search),
71-
),
72-
),
73-
],
89+
);
90+
},
7491
),
7592
),
7693
),

macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import FlutterMacOS
66
import Foundation
77

8+
import shared_preferences_foundation
89
import url_launcher_macos
910

1011
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
12+
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
1113
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
1214
}

pubspec.lock

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ packages:
6565
url: "https://pub.dev"
6666
source: hosted
6767
version: "1.3.1"
68+
ffi:
69+
dependency: transitive
70+
description:
71+
name: ffi
72+
sha256: "493f37e7df1804778ff3a53bd691d8692ddf69702cf4c1c1096a2e41b4779e21"
73+
url: "https://pub.dev"
74+
source: hosted
75+
version: "2.1.2"
76+
file:
77+
dependency: transitive
78+
description:
79+
name: file
80+
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
81+
url: "https://pub.dev"
82+
source: hosted
83+
version: "7.0.0"
6884
fl_chart:
6985
dependency: "direct main"
7086
description:
@@ -192,6 +208,38 @@ packages:
192208
url: "https://pub.dev"
193209
source: hosted
194210
version: "1.9.0"
211+
path_provider_linux:
212+
dependency: transitive
213+
description:
214+
name: path_provider_linux
215+
sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279
216+
url: "https://pub.dev"
217+
source: hosted
218+
version: "2.2.1"
219+
path_provider_platform_interface:
220+
dependency: transitive
221+
description:
222+
name: path_provider_platform_interface
223+
sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334"
224+
url: "https://pub.dev"
225+
source: hosted
226+
version: "2.1.2"
227+
path_provider_windows:
228+
dependency: transitive
229+
description:
230+
name: path_provider_windows
231+
sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170"
232+
url: "https://pub.dev"
233+
source: hosted
234+
version: "2.2.1"
235+
platform:
236+
dependency: transitive
237+
description:
238+
name: platform
239+
sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec"
240+
url: "https://pub.dev"
241+
source: hosted
242+
version: "3.1.4"
195243
plugin_platform_interface:
196244
dependency: transitive
197245
description:
@@ -200,6 +248,62 @@ packages:
200248
url: "https://pub.dev"
201249
source: hosted
202250
version: "2.1.8"
251+
shared_preferences:
252+
dependency: "direct main"
253+
description:
254+
name: shared_preferences
255+
sha256: "81429e4481e1ccfb51ede496e916348668fd0921627779233bd24cc3ff6abd02"
256+
url: "https://pub.dev"
257+
source: hosted
258+
version: "2.2.2"
259+
shared_preferences_android:
260+
dependency: transitive
261+
description:
262+
name: shared_preferences_android
263+
sha256: "8568a389334b6e83415b6aae55378e158fbc2314e074983362d20c562780fb06"
264+
url: "https://pub.dev"
265+
source: hosted
266+
version: "2.2.1"
267+
shared_preferences_foundation:
268+
dependency: transitive
269+
description:
270+
name: shared_preferences_foundation
271+
sha256: "7708d83064f38060c7b39db12aefe449cb8cdc031d6062280087bc4cdb988f5c"
272+
url: "https://pub.dev"
273+
source: hosted
274+
version: "2.3.5"
275+
shared_preferences_linux:
276+
dependency: transitive
277+
description:
278+
name: shared_preferences_linux
279+
sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa"
280+
url: "https://pub.dev"
281+
source: hosted
282+
version: "2.3.2"
283+
shared_preferences_platform_interface:
284+
dependency: transitive
285+
description:
286+
name: shared_preferences_platform_interface
287+
sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
288+
url: "https://pub.dev"
289+
source: hosted
290+
version: "2.3.2"
291+
shared_preferences_web:
292+
dependency: transitive
293+
description:
294+
name: shared_preferences_web
295+
sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a"
296+
url: "https://pub.dev"
297+
source: hosted
298+
version: "2.3.0"
299+
shared_preferences_windows:
300+
dependency: transitive
301+
description:
302+
name: shared_preferences_windows
303+
sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59"
304+
url: "https://pub.dev"
305+
source: hosted
306+
version: "2.3.2"
203307
sky_engine:
204308
dependency: transitive
205309
description: flutter
@@ -353,10 +457,26 @@ packages:
353457
dependency: transitive
354458
description:
355459
name: web
356-
sha256: "1d9158c616048c38f712a6646e317a3426da10e884447626167240d45209cbad"
460+
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
461+
url: "https://pub.dev"
462+
source: hosted
463+
version: "0.5.1"
464+
win32:
465+
dependency: transitive
466+
description:
467+
name: win32
468+
sha256: "464f5674532865248444b4c3daca12bd9bf2d7c47f759ce2617986e7229494a8"
469+
url: "https://pub.dev"
470+
source: hosted
471+
version: "5.2.0"
472+
xdg_directories:
473+
dependency: transitive
474+
description:
475+
name: xdg_directories
476+
sha256: faea9dee56b520b55a566385b84f2e8de55e7496104adada9962e0bd11bcff1d
357477
url: "https://pub.dev"
358478
source: hosted
359-
version: "0.5.0"
479+
version: "1.0.4"
360480
sdks:
361481
dart: ">=3.3.0 <4.0.0"
362482
flutter: ">=3.19.0"

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ environment:
3030
dependencies:
3131
flutter:
3232
sdk: flutter
33+
shared_preferences: ^2.2.2
3334

3435

3536
# The following adds the Cupertino Icons font to your application.

0 commit comments

Comments
 (0)