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();
}
}

View File

@@ -20,36 +20,22 @@ class MapPage extends StatefulWidget {
}
class _MapPageState extends State<MapPage> {
final _mapController = MapController();
final mapController = MapController();
late FloodStationProvider _floodStationProvider;
bool _loading = false;
@override
Widget build(BuildContext context) {
_floodStationProvider = context.watch<FloodStationProvider>();
if (_loading == true) {
return Center(
child: CircularProgressIndicator(),
);
} else if (_floodStationProvider.allStations.isEmpty) {
if (_floodStationProvider.allStations.isEmpty) {
return Center(
child: ElevatedButton(
onPressed: () {
setState(() {
_loading = true;
});
_floodStationProvider
.loadAllStations()
.whenComplete(() => setState(() {
_loading = false;
}));
},
onPressed: _floodStationProvider.loadAllStations,
child: Text('Load Map'),
),
);
}
return FlutterMap(
mapController: _mapController,
mapController: mapController,
options: MapOptions(
cameraConstraint: CameraConstraint.containCenter(
bounds: LatLngBounds.fromPoints(_floodStationProvider.allStations
@@ -70,14 +56,13 @@ class _MapPageState extends State<MapPage> {
padding: EdgeInsets.all(50),
maxZoom: 15,
markers: _stationsAsMarkers(_floodStationProvider.allStations),
builder: _clusterMarkerBuilder),
builder: _markerBuilder),
)
],
);
}
// builds the clustered marker
Widget _clusterMarkerBuilder(BuildContext context, List<Marker> markers) {
Widget _markerBuilder(BuildContext context, List<Marker> markers) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
@@ -86,7 +71,7 @@ class _MapPageState extends State<MapPage> {
child: Center(
child: Text(
markers.length.toString(),
style: TextStyle(color: Theme.of(context).colorScheme.onTertiary),
style: const TextStyle(color: Colors.white),
),
),
);
@@ -107,7 +92,7 @@ class _MapPageState extends State<MapPage> {
.toList();
}
void _markerTapped(FloodStation station) {
_markerTapped(FloodStation station) {
showDialog(
context: context,
builder: (context) => MapPopup(