Weather service #8

Merged
adsu7578 merged 4 commits from venue-page into master 2022-05-16 17:30:57 +02:00
2 changed files with 123 additions and 10 deletions

1
.gitignore vendored
View File

@ -270,3 +270,4 @@ Temporary Items
Thumbs.db Thumbs.db
tramp tramp
xcuserdata/ xcuserdata/
.flutter-plugins-dependencies

View File

@ -1,13 +1,79 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
// Color _backgroundColor = const Color(0xffac7b84); // Color _backgroundColor = const Color(0xffac7b84);
class VenuePage extends StatelessWidget { class WeatherData {
final String imageLink = ''; final int weatherValue;
final int temperature;
WeatherData(this.weatherValue, this.temperature);
factory WeatherData.fromJson(Map<String, dynamic> json) {
var value = json.values;
var tempWeatherData;
var tempTemperature;
if (value.first['wsymb2'] is int) {
tempWeatherData = value.first['wsymb2'];
}
if (value.first['temp'] is double) {
tempTemperature = value.first['temp'];
}
if (tempWeatherData != null && tempTemperature != null) {
return WeatherData(tempWeatherData, tempTemperature.round());
} else {
return WeatherData(0, 0);
}
}
int getCurrentTemperature() {
return temperature;
}
Widget getCurrentWeatherIcon() {
switch (weatherValue) {
case 1:
return const Icon(Icons.sunny);
case 2:
return const Icon(Icons.wb_sunny_outlined);
case 3:
return const Icon(Icons.sunny);
case 4:
return const Icon(Icons.sunny);
case 5:
return const Icon(Icons.cloud);
case 6:
return const Icon(Icons.cloud);
case 7:
return const Icon(Icons.sunny);
case 8:
return const Icon(Icons.sunny);
case 9:
return const Icon(Icons.sunny);
default:
return const Icon(Icons.not_accessible);
}
}
}
class VenuePage extends StatefulWidget {
const VenuePage({Key? key}) : super(key: key); const VenuePage({Key? key}) : super(key: key);
@override
State<VenuePage> createState() => _VenuePageState();
}
class _VenuePageState extends State<VenuePage> {
late WeatherData currentWeather;
final String imageLink = '';
validateAndGetImageLink() { validateAndGetImageLink() {
if (imageLink == '') { if (imageLink == '') {
return 'https://live.staticflickr.com/6205/6081773215_19444220b6_b.jpg'; return 'https://live.staticflickr.com/6205/6081773215_19444220b6_b.jpg';
@ -16,6 +82,34 @@ class VenuePage extends StatelessWidget {
} }
} }
@override
void initState() {
refreshWeather();
}
Future refreshWeather() async {
WeatherData tempWeather = WeatherData(0, 0);
currentWeather = tempWeather;
Uri weatherDataURI = Uri.parse(
'https://group-4-75.pvt.dsv.su.se/target/weather-0.0.2-SNAPSHOT.war/weather');
final responce = await http.get(weatherDataURI);
if (responce.statusCode == 200) {
var data = json.decode(responce.body);
tempWeather = WeatherData.fromJson(data);
print(data);
setState(() {
currentWeather = tempWeather; //Could be a widget instead??
});
} else {
throw const HttpException("Problem fetching the weather data");
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -39,13 +133,31 @@ class VenuePage extends StatelessWidget {
child: Image.network(validateAndGetImageLink()), child: Image.network(validateAndGetImageLink()),
), ),
]), ]),
Row( // Row(
// children: const [
// Text(
// 'Placeholder for image',
// ),
// ],
// ),
Row(children: [
Expanded(
child: Column(
children: const [ children: const [
Text( Text('This is the name'),
'Placeholder for image', Text('This is the address'),
),
], ],
), )),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
currentWeather.getCurrentWeatherIcon(),
Text(currentWeather.getCurrentTemperature().toString() +
'\u2103'),
]),
)
]),
const AboutTheSpotTable(), const AboutTheSpotTable(),
GridView.count( GridView.count(
crossAxisCount: 2, crossAxisCount: 2,