more detailed flood station info
This commit is contained in:
24
lib/Widgets/flood_station_list_view.dart
Normal file
24
lib/Widgets/flood_station_list_view.dart
Normal 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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,41 @@
|
|||||||
class FloodStation {
|
class FloodStation {
|
||||||
final String id;
|
final String id;
|
||||||
final String town;
|
final String town;
|
||||||
final double? latestReading;
|
final double lat;
|
||||||
|
final double long;
|
||||||
|
final DateTime? dateOpened;
|
||||||
|
final String catchmentName;
|
||||||
|
final String label;
|
||||||
|
|
||||||
FloodStation({
|
FloodStation({
|
||||||
required this.id,
|
required this.id,
|
||||||
required this.town,
|
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(
|
factory FloodStation.fromMap(Map<String, dynamic> json) => FloodStation(
|
||||||
id: json['@id'] ?? '',
|
id: json['wiskiID'] ?? '',
|
||||||
town: json['town'] ?? '',
|
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 '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import '../Widgets/flood_station_list_view.dart';
|
||||||
import '../model/flood_station.dart';
|
import '../model/flood_station.dart';
|
||||||
import '../services/api.dart';
|
import '../services/api.dart';
|
||||||
|
|
||||||
@@ -13,16 +14,11 @@ class LandingPage extends StatelessWidget {
|
|||||||
if (!snapshot.hasData) {
|
if (!snapshot.hasData) {
|
||||||
return CircularProgressIndicator();
|
return CircularProgressIndicator();
|
||||||
} else if (snapshot.hasData) {
|
} else if (snapshot.hasData) {
|
||||||
return ListView.builder(
|
return FloodStationListView(stations: snapshot.data!);
|
||||||
itemBuilder: (context, index) {
|
} else if (snapshot.hasError) {
|
||||||
return ListTile(
|
return Text(snapshot.error.toString());
|
||||||
title: Text(snapshot.data!.elementAt(index).town),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
itemCount: snapshot.data!.length,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
return Placeholder();
|
return Text('An unknown error occured.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user