refactored code to encapsulate date formatting

This commit is contained in:
2025-01-29 13:39:49 +01:00
parent 2655779fac
commit 7b7e3e9fe0
6 changed files with 85 additions and 43 deletions

View File

@@ -16,6 +16,8 @@ class FloodStationPage extends StatefulWidget {
}
class _FloodStationPageState extends State<FloodStationPage> {
bool _tableVisible = false;
@override
void deactivate() {
context.read<FloodStationProvider>().selectedStation = null;
@@ -29,6 +31,14 @@ class _FloodStationPageState extends State<FloodStationPage> {
return Scaffold(
appBar: AppBar(
title: Text(station?.label ?? ''),
actions: [
TextButton(
onPressed: () => setState(() {
_tableVisible = !_tableVisible;
}),
child: _tableVisible ? Text('Show Graph') : Text('Show Table'),
),
],
),
body: FutureBuilder<List<Reading>>(
future: Api.fetchReadingsFromStation(station?.id ?? ''),
@@ -36,6 +46,12 @@ class _FloodStationPageState extends State<FloodStationPage> {
if (snapshot.hasData) {
if (snapshot.data!.isEmpty) {
return Center(child: Text('No readings on record.'));
} else if (_tableVisible) {
return SingleChildScrollView(
child: Table(
children: _getTableChildren(snapshot.data!),
),
);
}
return Center(
child: Padding(
@@ -53,4 +69,17 @@ class _FloodStationPageState extends State<FloodStationPage> {
}),
);
}
List<TableRow> _getTableChildren(List<Reading> list) {
return list
.map<TableRow>(
(e) => TableRow(
children: [
Text(e.dateTime.toString()),
Text(e.value.toString()),
],
),
)
.toList();
}
}