refactored code to encapsulate date formatting
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user