Add map to flutter application

This commit is contained in:
Reimar 2024-08-20 11:00:10 +02:00
parent f1327571e6
commit d2bfcc4692
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
5 changed files with 119 additions and 214 deletions

4
Mobile/.gitignore vendored
View File

@ -29,9 +29,7 @@ migrate_working_dir/
.flutter-plugins-dependencies
.pub-cache/
.pub/
#/build/e514a05cd739d52d8e7038a04d724576
#/build/flutter_assets
#/build/2794971a5855e7decd7bb368de5d49d1.cache.dill.track.dill
build/
# Symbolication related
app.*.symbols

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'weather.dart';
import 'package:flutter_map/flutter_map.dart';
import "package:latlong2/latlong.dart";
void main() {
runApp(const MyApp());
@ -69,13 +70,6 @@ class _MyHomePageState extends State<MyHomePage> {
});
}
void _navigateToWeatherForecastPage() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => WeatherForecastPage()),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -83,20 +77,15 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have clicked the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
body: FlutterMap(
options: const MapOptions(initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
)
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
@ -105,12 +94,6 @@ class _MyHomePageState extends State<MyHomePage> {
tooltip: 'Increment',
child: const Icon(Icons.add),
),
SizedBox(width: 10), // Optional: Adds space between buttons
FloatingActionButton(
onPressed: _navigateToWeatherForecastPage,
tooltip: 'Show Weather',
child: const Icon(Icons.star),
),
],
),
);

View File

@ -1,150 +0,0 @@
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:fl_chart/fl_chart.dart';
class WeatherForecastPage extends StatefulWidget {
@override
_WeatherForecastPageState createState() => _WeatherForecastPageState();
}
class _WeatherForecastPageState extends State<WeatherForecastPage> {
List<dynamic> forecasts = [];
List<double> temperaturesC = [];
List<String> dates = [];
Future<void> fetchForecasts() async {
final response =
await http.get(Uri.parse('https://h4api.onrender.com/WeatherForecast'));
if (response.statusCode == 200) {
setState(() {
temperaturesC.clear();
dates.clear();
forecasts = json.decode(response.body).map((f) {
temperaturesC.add(f['temperatureC'].toDouble());
dates.add(f['date']);
return f;
}).toList();
});
} else {
throw Exception('Failed to load forecasts');
}
}
@override
void initState() {
super.initState();
fetchForecasts();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Weather Forecast Graph'),
actions: [
IconButton(
icon: Icon(Icons.refresh),
onPressed: fetchForecasts,
),
],
),
body: forecasts.isEmpty
? Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Expanded(
child: LineChart(
LineChartData(
minY:
(temperaturesC.reduce((a, b) => a < b ? a : b) - 10)
.toDouble(),
maxY:
(temperaturesC.reduce((a, b) => a > b ? a : b) + 20)
.toDouble(),
gridData: FlGridData(show: true), // Grid
titlesData: FlTitlesData(
leftTitles: SideTitles(
showTitles: true,
reservedSize: 40,
getTextStyles: (context, value) => const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 12,
),
interval: 10,
getTitles: (value) {
return '${value.toInt()}°C';
},
),
bottomTitles: SideTitles(
showTitles: true,
reservedSize: 40,
getTextStyles: (context, value) => const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 12,
),
getTitles: (value) {
int index = value.toInt();
if (index >= 0 && index < dates.length) {
return dates[index].substring(5); // MM-DD
}
return '';
},
),
),
borderData: FlBorderData(
show: true,
border: Border.all(color: Colors.black, width: 1),
),
lineBarsData: [
LineChartBarData(
spots: temperaturesC.asMap().entries.map((e) {
return FlSpot(e.key.toDouble(), e.value);
}).toList(),
isCurved: true,
colors: [Colors.blue],
barWidth: 4,
dotData: FlDotData(show: true),
belowBarData: BarAreaData(show: false),
),
],
),
),
),
SizedBox(height: 20),
Text(
'Weather Summaries',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
Expanded(
child: ListView.builder(
itemCount: forecasts.length,
itemBuilder: (context, index) {
final forecast = forecasts[index];
return ListTile(
title: Text(
'${forecast['date']}: ${forecast['summary']}',
),
);
},
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: fetchForecasts,
child: Text('Refresh Data'),
),
],
),
),
);
}
}

View File

@ -49,14 +49,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.8"
equatable:
dart_earcut:
dependency: transitive
description:
name: equatable
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
name: dart_earcut
sha256: "41b493147e30a051efb2da1e3acb7f38fe0db60afba24ac1ea5684cee272721e"
url: "https://pub.dev"
source: hosted
version: "2.0.5"
version: "1.1.0"
fake_async:
dependency: transitive
description:
@ -65,14 +65,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
fl_chart:
dependency: "direct main"
description:
name: fl_chart
sha256: "0c8baa9d4db70817d27099efb99e40e4164448bb86f73217216ef65b3976bc4f"
url: "https://pub.dev"
source: hosted
version: "0.36.4"
flutter:
dependency: "direct main"
description: flutter
@ -86,6 +78,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
flutter_map:
dependency: "direct main"
description:
name: flutter_map
sha256: "2ecb34619a4be19df6f40c2f8dce1591675b4eff7a6857bd8f533706977385da"
url: "https://pub.dev"
source: hosted
version: "7.0.2"
flutter_test:
dependency: "direct dev"
description: flutter
@ -95,10 +95,10 @@ packages:
dependency: "direct main"
description:
name: http
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
url: "https://pub.dev"
source: hosted
version: "0.13.6"
version: "1.2.2"
http_parser:
dependency: transitive
description:
@ -107,30 +107,46 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
intl:
dependency: transitive
description:
name: intl
sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
url: "https://pub.dev"
source: hosted
version: "0.19.0"
latlong2:
dependency: "direct main"
description:
name: latlong2
sha256: "98227922caf49e6056f91b6c56945ea1c7b166f28ffcd5fb8e72fc0b453cc8fe"
url: "https://pub.dev"
source: hosted
version: "0.9.1"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.1"
lints:
dependency: transitive
description:
@ -139,6 +155,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
lists:
dependency: transitive
description:
name: lists
sha256: "4ca5c19ae4350de036a7e996cdd1ee39c93ac0a2b840f4915459b7d0a7d4ab27"
url: "https://pub.dev"
source: hosted
version: "1.0.1"
logger:
dependency: transitive
description:
name: logger
sha256: "697d067c60c20999686a0add96cf6aba723b3aa1f83ecf806a8097231529ec32"
url: "https://pub.dev"
source: hosted
version: "2.4.0"
matcher:
dependency: transitive
description:
@ -151,18 +183,26 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.15.0"
mgrs_dart:
dependency: transitive
description:
name: mgrs_dart
sha256: fb89ae62f05fa0bb90f70c31fc870bcbcfd516c843fb554452ab3396f78586f7
url: "https://pub.dev"
source: hosted
version: "2.0.0"
path:
dependency: transitive
description:
@ -171,14 +211,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.0"
pedantic:
polylabel:
dependency: transitive
description:
name: pedantic
sha256: "67fc27ed9639506c856c840ccce7594d0bdcd91bc8d53d6e52359449a1d50602"
name: polylabel
sha256: "41b9099afb2aa6c1730bdd8a0fab1400d287694ec7615dd8516935fa3144214b"
url: "https://pub.dev"
source: hosted
version: "1.11.1"
version: "1.0.1"
proj4dart:
dependency: transitive
description:
name: proj4dart
sha256: c8a659ac9b6864aa47c171e78d41bbe6f5e1d7bd790a5814249e6b68bc44324e
url: "https://pub.dev"
source: hosted
version: "2.1.0"
sky_engine:
dependency: transitive
description: flutter
@ -228,10 +276,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.7.2"
typed_data:
dependency: transitive
description:
@ -240,6 +288,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.2"
unicode:
dependency: transitive
description:
name: unicode
sha256: "0f69e46593d65245774d4f17125c6084d2c20b4e473a983f6e21b7d7762218f1"
url: "https://pub.dev"
source: hosted
version: "0.3.1"
vector_math:
dependency: transitive
description:
@ -252,9 +308,26 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
url: "https://pub.dev"
source: hosted
version: "13.0.0"
version: "14.2.4"
web:
dependency: transitive
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
wkt_parser:
dependency: transitive
description:
name: wkt_parser
sha256: "8a555fc60de3116c00aad67891bcab20f81a958e4219cc106e3c037aa3937f13"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
sdks:
dart: ">=3.3.4 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"

View File

@ -30,8 +30,9 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^0.13.3
fl_chart: ^0.36.0
http: ^1.2.1
flutter_map: ^7.0.2
latlong2: ^0.9.1
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.