Cached Http-Request
und gefixxt: tastur überlagert sich mit eingabefeld
This commit is contained in:
@@ -1,10 +1,32 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class FetchMenu {
|
||||
static var lastFetch = DateTime.now();
|
||||
static var fetched = false;
|
||||
|
||||
static Future<List<String>> get() async {
|
||||
List<String> menus = [];
|
||||
|
||||
// TODO: Proto:
|
||||
if (!fetched || DateTime.now().difference(lastFetch).inHours >= 2) {
|
||||
menus = await fromRequest();
|
||||
fetched = true;
|
||||
} else {
|
||||
menus = await fromCache();
|
||||
}
|
||||
|
||||
return menus;
|
||||
}
|
||||
|
||||
static Future<List<String>> fromCache() async {
|
||||
return await loadCache();
|
||||
}
|
||||
|
||||
static Future<List<String>> fromRequest() async {
|
||||
List<String> menus = [];
|
||||
http.Response resp = await http.get(Uri.parse(
|
||||
"https://www.swfr.de/essen/mensen-cafes-speiseplaene/mensa-loerrach"));
|
||||
var content = resp.body.split("<h3>");
|
||||
@@ -15,7 +37,38 @@ class FetchMenu {
|
||||
menus.add(name);
|
||||
}
|
||||
}
|
||||
// End-Proto
|
||||
|
||||
await storeCache(menus);
|
||||
|
||||
lastFetch = DateTime.now();
|
||||
|
||||
return menus;
|
||||
}
|
||||
|
||||
static Future<void> storeCache(List<String> menus) async {
|
||||
var prefs = await SharedPreferences.getInstance();
|
||||
|
||||
for (var i = 0; i < 4; i++) {
|
||||
prefs.setString("menuCache$i", "");
|
||||
if (i < menus.length) {
|
||||
prefs.setString("menuCache$i", menus[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static Future<List<String>> loadCache() async {
|
||||
var prefs = await SharedPreferences.getInstance();
|
||||
|
||||
List<String> menus = [];
|
||||
|
||||
for (var i = 0; i < 4; i++) {
|
||||
var curr = prefs.getString("menuCache$i");
|
||||
if (curr != "") {
|
||||
menus.add(curr!);
|
||||
}
|
||||
}
|
||||
|
||||
return menus;
|
||||
}
|
||||
|
||||
+104
-101
@@ -13,117 +13,120 @@ class PageEssen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Stack(
|
||||
children: [
|
||||
// Hintergrundbild
|
||||
Container(
|
||||
width: 1600,
|
||||
height: 750,
|
||||
decoration: BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage("assets/backgrounds/Essen.jpg"),
|
||||
fit: BoxFit.cover, // Deckt den gesamten Bildschirm ab
|
||||
colorFilter: ColorFilter.mode(
|
||||
Colors.white.withOpacity(0.2),
|
||||
BlendMode.dstATop,
|
||||
body: SingleChildScrollView(
|
||||
child: Stack(
|
||||
children: [
|
||||
// Hintergrundbild
|
||||
Container(
|
||||
width: 1600,
|
||||
height: 750,
|
||||
decoration: BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage("assets/backgrounds/Essen.jpg"),
|
||||
fit: BoxFit.cover, // Deckt den gesamten Bildschirm ab
|
||||
colorFilter: ColorFilter.mode(
|
||||
Colors.white.withOpacity(0.2),
|
||||
BlendMode.dstATop,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
// Vordergrundinhalte
|
||||
// Vordergrundinhalte
|
||||
|
||||
Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Consumer<PageProvider>(
|
||||
builder: (context, pageProvider, child) {
|
||||
return Text(pageProvider.selectedEssenName, style: const TextStyle(fontSize: 40),);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 30,),
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(0.8),
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(80),
|
||||
bottomRight: Radius.circular(80))),
|
||||
height: 400,
|
||||
width: 600,
|
||||
padding: EdgeInsets.symmetric(horizontal: 20),
|
||||
Center(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
"Geschmack*",
|
||||
style: TextStyle(fontSize: 35),
|
||||
),
|
||||
Consumer<EssenProvider>(
|
||||
builder: (context, essenProvider, child) {
|
||||
return RatingStars(
|
||||
onRate: (value) {
|
||||
essenProvider.setRatingGeschmack(value);
|
||||
},
|
||||
initRating: essenProvider.ratingGeschmack,
|
||||
);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
"Qualität*",
|
||||
style: TextStyle(fontSize: 35),
|
||||
),
|
||||
Consumer<EssenProvider>(
|
||||
builder: (context, essenProvider, child) {
|
||||
return RatingStars(
|
||||
onRate: (value) {
|
||||
essenProvider.setRatingQuali(value);
|
||||
},
|
||||
initRating: essenProvider.ratingQuali,
|
||||
);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 50,
|
||||
),
|
||||
Consumer<EssenProvider>(
|
||||
builder: (context, essenProvider, child) {
|
||||
return TextFormField(
|
||||
initialValue: essenProvider.freiText,
|
||||
onChanged: (value) {
|
||||
essenProvider.setFreiText(value);
|
||||
},
|
||||
autofocus: false,
|
||||
maxLength: 500,
|
||||
maxLines: 4,
|
||||
decoration: InputDecoration(
|
||||
labelText: "Dein Feedback (optional)",
|
||||
labelStyle: const TextStyle(fontSize: 20),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
),
|
||||
),
|
||||
style: TextStyle(fontSize: 20),
|
||||
);
|
||||
SizedBox(height: 200,),
|
||||
Consumer<PageProvider>(
|
||||
builder: (context, pageProvider, child) {
|
||||
return Text(pageProvider.selectedEssenName, style: const TextStyle(fontSize: 40),);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 30,),
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(0.8),
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(80),
|
||||
bottomRight: Radius.circular(80))),
|
||||
height: 400,
|
||||
width: 600,
|
||||
padding: EdgeInsets.symmetric(horizontal: 20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
"Geschmack*",
|
||||
style: TextStyle(fontSize: 35),
|
||||
),
|
||||
Consumer<EssenProvider>(
|
||||
builder: (context, essenProvider, child) {
|
||||
return RatingStars(
|
||||
onRate: (value) {
|
||||
essenProvider.setRatingGeschmack(value);
|
||||
},
|
||||
initRating: essenProvider.ratingGeschmack,
|
||||
);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
"Qualität*",
|
||||
style: TextStyle(fontSize: 35),
|
||||
),
|
||||
Consumer<EssenProvider>(
|
||||
builder: (context, essenProvider, child) {
|
||||
return RatingStars(
|
||||
onRate: (value) {
|
||||
essenProvider.setRatingQuali(value);
|
||||
},
|
||||
initRating: essenProvider.ratingQuali,
|
||||
);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 50,
|
||||
),
|
||||
Consumer<EssenProvider>(
|
||||
builder: (context, essenProvider, child) {
|
||||
return TextFormField(
|
||||
initialValue: essenProvider.freiText,
|
||||
onChanged: (value) {
|
||||
essenProvider.setFreiText(value);
|
||||
},
|
||||
autofocus: false,
|
||||
maxLength: 500,
|
||||
maxLines: 4,
|
||||
decoration: InputDecoration(
|
||||
labelText: "Dein Feedback (optional)",
|
||||
labelStyle: const TextStyle(fontSize: 20),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
),
|
||||
),
|
||||
style: TextStyle(fontSize: 20),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
)),
|
||||
],
|
||||
),
|
||||
)),
|
||||
],
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user