simple detail view of flood station and readings
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../model/flood_station.dart';
|
||||
import '../pages/flood_station_page.dart';
|
||||
|
||||
class FloodStationListView extends StatelessWidget {
|
||||
const FloodStationListView({super.key, required List<FloodStation> stations})
|
||||
@@ -14,6 +15,9 @@ class FloodStationListView extends StatelessWidget {
|
||||
itemBuilder: (context, index) {
|
||||
final item = _stations.elementAt(index);
|
||||
return ListTile(
|
||||
onTap: () => Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (context) => FloodStationPage(floodStation: item),
|
||||
)),
|
||||
title: Text(item.label),
|
||||
subtitle: Text(item.town),
|
||||
);
|
||||
|
||||
11
lib/model/reading.dart
Normal file
11
lib/model/reading.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
class Reading {
|
||||
final DateTime dateTime;
|
||||
final double value;
|
||||
|
||||
factory Reading.fromMap(Map<String, dynamic> json) => Reading(
|
||||
dateTime: DateTime.parse(json['dateTime']),
|
||||
value: json['value'],
|
||||
);
|
||||
|
||||
Reading({required this.dateTime, required this.value});
|
||||
}
|
||||
36
lib/pages/flood_station_page.dart
Normal file
36
lib/pages/flood_station_page.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../model/flood_station.dart';
|
||||
import '../model/reading.dart';
|
||||
import '../services/api.dart';
|
||||
|
||||
class FloodStationPage extends StatelessWidget {
|
||||
const FloodStationPage({super.key, required FloodStation floodStation})
|
||||
: _floodStation = floodStation;
|
||||
final FloodStation _floodStation;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(_floodStation.label),
|
||||
),
|
||||
body: FutureBuilder<List<Reading>>(
|
||||
future: Api.fetchReadingsFromStation(_floodStation.id),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return ListView.builder(
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title:
|
||||
Text(snapshot.data!.elementAt(index).value.toString()),
|
||||
);
|
||||
},
|
||||
itemCount: snapshot.data!.length,
|
||||
);
|
||||
} else {
|
||||
return Placeholder();
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
|
||||
import '../model/flood_station.dart';
|
||||
import '../model/reading.dart';
|
||||
|
||||
class Api {
|
||||
static const String _rootUrl =
|
||||
@@ -18,4 +19,20 @@ class Api {
|
||||
}
|
||||
return stations;
|
||||
}
|
||||
|
||||
static Future<List<Reading>> fetchReadingsFromStation(
|
||||
String stationId) async {
|
||||
List<Reading> readings = [];
|
||||
final dateTime = DateTime.now().subtract(Duration(days: 1));
|
||||
final url =
|
||||
'$_rootUrl/id/stations/$stationId/readings?since=${dateTime.toIso8601String()}';
|
||||
final response = await http.get(Uri.parse(url));
|
||||
if (response.statusCode == 200) {
|
||||
final Map<String, dynamic> jsonStr = jsonDecode(response.body);
|
||||
for (final str in jsonStr['items']) {
|
||||
readings.add(Reading.fromMap(str));
|
||||
}
|
||||
}
|
||||
return readings;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user