Used Provider-Package to encapsulate state management
This commit is contained in:
@@ -1,22 +1,37 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../Widgets/reading_graph.dart';
|
||||
import '../model/flood_station.dart';
|
||||
import '../model/reading.dart';
|
||||
import '../services/api.dart';
|
||||
import '../services/flood_station_provider.dart';
|
||||
|
||||
class FloodStationPage extends StatefulWidget {
|
||||
const FloodStationPage({super.key});
|
||||
static const String routeName = '/station';
|
||||
|
||||
@override
|
||||
State<FloodStationPage> createState() => _FloodStationPageState();
|
||||
}
|
||||
|
||||
class _FloodStationPageState extends State<FloodStationPage> {
|
||||
@override
|
||||
void deactivate() {
|
||||
context.read<FloodStationProvider>().selectedStation = null;
|
||||
super.deactivate();
|
||||
}
|
||||
|
||||
class FloodStationPage extends StatelessWidget {
|
||||
const FloodStationPage({super.key, required FloodStation floodStation})
|
||||
: _floodStation = floodStation;
|
||||
final FloodStation _floodStation;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final FloodStation? station =
|
||||
context.read<FloodStationProvider>().selectedStation;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(_floodStation.label),
|
||||
title: Text(station?.label ?? ''),
|
||||
),
|
||||
body: FutureBuilder<List<Reading>>(
|
||||
future: Api.fetchReadingsFromStation(_floodStation.id),
|
||||
future: Api.fetchReadingsFromStation(station?.id ?? ''),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
if (snapshot.data!.isEmpty) {
|
||||
|
||||
Reference in New Issue
Block a user