more detailed flood station info

This commit is contained in:
2025-01-24 23:06:53 +01:00
parent bedd9c7d88
commit 08c266bc95
3 changed files with 57 additions and 13 deletions

View File

@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';
import '../model/flood_station.dart';
class FloodStationListView extends StatelessWidget {
const FloodStationListView({super.key, required List<FloodStation> stations})
: _stations = stations;
final List<FloodStation> _stations;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
final item = _stations.elementAt(index);
return ListTile(
title: Text(item.label),
subtitle: Text(item.town),
);
},
itemCount: _stations.length,
);
}
}

View File

@@ -1,17 +1,41 @@
class FloodStation {
final String id;
final String town;
final double? latestReading;
final double lat;
final double long;
final DateTime? dateOpened;
final String catchmentName;
final String label;
FloodStation({
required this.id,
required this.town,
this.latestReading,
required this.lat,
required this.long,
this.dateOpened,
required this.catchmentName,
required this.label,
});
factory FloodStation.fromMap(Map<String, dynamic> json) => FloodStation(
id: json['@id'] ?? '',
id: json['wiskiID'] ?? '',
town: json['town'] ?? '',
latestReading: double.tryParse(json['latestReading']?.toString() ?? ''),
lat: parseDoubleValue(json['lat']),
long: parseDoubleValue(json['long']),
dateOpened: DateTime.tryParse(json['dateOpened'] ?? ''),
catchmentName: parseStringValue(json['catchmentName']),
label: parseStringValue(json['label']),
);
static double parseDoubleValue(dynamic value) {
if (value is double) return value;
if (value is String) return double.parse(value);
return 0;
}
static String parseStringValue(dynamic value) {
if (value is String) return value;
if (value is List<dynamic>) return value[0];
return '';
}
}

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import '../Widgets/flood_station_list_view.dart';
import '../model/flood_station.dart';
import '../services/api.dart';
@@ -13,16 +14,11 @@ class LandingPage extends StatelessWidget {
if (!snapshot.hasData) {
return CircularProgressIndicator();
} else if (snapshot.hasData) {
return ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: Text(snapshot.data!.elementAt(index).town),
);
},
itemCount: snapshot.data!.length,
);
return FloodStationListView(stations: snapshot.data!);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return Placeholder();
return Text('An unknown error occured.');
}
}