tablet-app/lib/Screens/MainView/weather_view.dart
2024-02-27 17:37:20 +01:00

294 lines
15 KiB
Dart

import 'package:auto_size_text/auto_size_text.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tablet_app/Models/WeatherData.dart';
import 'package:tablet_app/app_context.dart';
import 'package:tablet_app/constants.dart';
import 'package:intl/intl.dart';
class WeatherView extends StatelessWidget {
final WeatherData weatherData;
WeatherView({required this.weatherData});
int nbrNextHours = 5;
String formatTimestamp(int timestamp, AppContext appContext, bool isHourOnly, bool isDateOnly) {
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
// Determine the date format based on the application language
String dateFormat = appContext.getContext().language.toString().toUpperCase() == "EN" ?
'MM/dd/yyyy HH:mm'
: 'dd/MM/yyyy HH:mm';
if(isHourOnly) {
dateFormat = 'HH:mm';
}
if(isDateOnly) {
dateFormat = dateFormat.replaceAll(' HH:mm', '');
}
String formattedDate = DateFormat(dateFormat).format(dateTime);
return formattedDate;
}
List<WeatherForecast> getNextFiveDaysForecast(List<WeatherForecast> allForecasts) {
List<WeatherForecast> nextFiveDaysForecast = [];
DateTime today = DateTime.now();
List<WeatherForecast> nextDay1All = allForecasts.where((af) => (DateTime.fromMillisecondsSinceEpoch(af.dt! * 1000)).day == (today.add(Duration(days: 1))).day).toList();
List<WeatherForecast> nextDay2All = allForecasts.where((af) => (DateTime.fromMillisecondsSinceEpoch(af.dt! * 1000)).day == (today.add(Duration(days: 2))).day).toList();
List<WeatherForecast> nextDay3All = allForecasts.where((af) => (DateTime.fromMillisecondsSinceEpoch(af.dt! * 1000)).day == (today.add(Duration(days: 3))).day).toList();
List<WeatherForecast> nextDay4All = allForecasts.where((af) => (DateTime.fromMillisecondsSinceEpoch(af.dt! * 1000)).day == (today.add(Duration(days: 4))).day).toList();
List<WeatherForecast> nextDay5All = allForecasts.where((af) => (DateTime.fromMillisecondsSinceEpoch(af.dt! * 1000)).day == (today.add(Duration(days: 5))).day).toList();
var nextDay1MiddayTest = nextDay1All.where((nd) => (DateTime.fromMillisecondsSinceEpoch(nd.dt! * 1000)).hour == 12).firstOrNull;
WeatherForecast nextDay1AllSummary = nextDay1MiddayTest != null ? nextDay1MiddayTest : nextDay1All.last;
nextFiveDaysForecast.add(nextDay1AllSummary);
var nextDay2MiddayTest = nextDay2All.where((nd) => (DateTime.fromMillisecondsSinceEpoch(nd.dt! * 1000)).hour == 12).firstOrNull;
WeatherForecast nextDay2Midday = nextDay2MiddayTest != null ? nextDay2MiddayTest : nextDay2All.last;
nextFiveDaysForecast.add(nextDay2Midday);
var nextDay3MiddayTest = nextDay3All.where((nd) => (DateTime.fromMillisecondsSinceEpoch(nd.dt! * 1000)).hour == 12).firstOrNull;
WeatherForecast nextDay3Midday = nextDay3MiddayTest != null ? nextDay3MiddayTest : nextDay3All.last;
nextFiveDaysForecast.add(nextDay3Midday);
var nextDay4MiddayTest = nextDay4All.where((nd) => (DateTime.fromMillisecondsSinceEpoch(nd.dt! * 1000)).hour == 12).firstOrNull;
WeatherForecast nextDay4Midday = nextDay4MiddayTest != null ? nextDay4MiddayTest : nextDay4All.last;
nextFiveDaysForecast.add(nextDay4Midday);
var nextDay5MiddayTest = nextDay5All.where((nd) => (DateTime.fromMillisecondsSinceEpoch(nd.dt! * 1000)).hour == 12).firstOrNull;
WeatherForecast nextDay5Midday = nextDay5MiddayTest != null ? nextDay5MiddayTest : nextDay5All.last;
nextFiveDaysForecast.add(nextDay5Midday);
return nextFiveDaysForecast;
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
final appContext = Provider.of<AppContext>(context);
return InkWell(
onTap: () {
print(weatherData.list!.first.weather!.first.main);
print(weatherData.list!.first.weather!.first.description);
print(weatherData.list!.first.weather!.first.icon!);
print(weatherData.list!.first.dtTxt);
print(weatherData.list!.first.dt);
String formattedDate = formatTimestamp(weatherData.list!.first.dt!, appContext, false, false);
getNextFiveDaysForecast(weatherData.list!);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))
),
contentPadding: EdgeInsets.zero,
// title: Text(eventAgenda.name!),
content: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: kBackgroundLight,
),
height: size.height * 0.9,
width: size.width * 0.7,
child: Stack(
children: [
Positioned(
right: 5,
top: 5,
child: InkWell(
onTap: () {
Navigator.of(context).pop();
},
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: kMainGrey,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: kMainGrey,
spreadRadius: 0.5,
blurRadius: 1.1,
offset: Offset(0, 1.1), // changes position of shadow
),
],
),
child: Icon(
Icons.close,
size: 25,
color: Colors.white,
),
),
),
),
SizedBox(
//height: 300,
//width: 300,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: Center(child: Text(weatherData.city!.name!, style: TextStyle(fontSize: kSectionTitleDetailSize, fontWeight: FontWeight.w400),)),
),
SizedBox(
height: 125,
width: 125,
child: Container(
color: Colors.red, child: Center(child: CachedNetworkImage(imageUrl: "https://openweathermap.org/img/wn/${weatherData.list!.first.weather!.first.icon!}@4x.png"))),
),
/*Center(
child: Text("${formattedDate}", style: TextStyle(fontSize: kSectionDescriptionDetailSize, fontWeight: FontWeight.w400))
),*/
Container(
color: Colors.green,
width: size.width * 0.4,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Icon(Icons.water_drop_outlined),
Text("${weatherData.list!.first.pop!.round().toString()}%", style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.w400)),
],
),
),
Text("${weatherData.list!.first.main!.temp!.round().toString()}°", style: TextStyle(fontSize: 40.0, fontWeight: FontWeight.w400)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Icon(Icons.air),
Text("${(weatherData.list!.first.wind!.speed! * 3.6).toStringAsFixed(1)}km/h", style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.w400)),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: size.height * 0.2,
width: size.width * 0.5,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: Colors.grey,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: List.generate(
nbrNextHours,
(index) {
final weatherForecast = weatherData.list![index];
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: size.height * 0.25,
width: size.width * 0.08,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: kBackgroundGrey,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(child: CachedNetworkImage(imageUrl: "https://openweathermap.org/img/wn/${weatherForecast.weather!.first.icon!}.png")),
Text('${weatherForecast.main!.temp!.round().toString()} °C', style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600)),
Text('${formatTimestamp(weatherForecast.dt!, appContext, true, false)}', style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w400)),
],
),
),
);
},
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: size.height * 0.25,
width: size.width * 0.75,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: Colors.amber,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: List.generate(
nbrNextHours,
(index) {
final weatherForecastNextDay = getNextFiveDaysForecast(weatherData.list!)[index];
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: size.height * 0.2,
width: size.width * 0.125,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: kBackgroundGrey,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(child: CachedNetworkImage(imageUrl: "https://openweathermap.org/img/wn/${weatherForecastNextDay.weather!.first.icon!}@2x.png")),
Text('${weatherForecastNextDay.main!.temp!.round().toString()} °C', style: TextStyle(fontSize: 25.0, fontWeight: FontWeight.w600)),
Text('${formatTimestamp(weatherForecastNextDay.dt!, appContext, false, true)}', style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.w400)),
],
),
),
);
},
),
),
),
)
],
),
),
],
),
),
);
},
);
},
child: Container(
//color: Colors.amber,
width: 70,
child: Column(
children: [
CachedNetworkImage(imageUrl: "https://openweathermap.org/img/wn/${weatherData.list!.first.weather!.first.icon!}.png"),
Container(child: AutoSizeText("${weatherData.list!.first.main!.temp!.round().toString()} °C")),
//AutoSizeText(weatherData.city!.name!),
],
),
),
);
}
}
//_webView