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) {
|
||||
|
||||
@@ -1,33 +1,39 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../Widgets/flood_station_list_view.dart';
|
||||
import '../model/flood_station.dart';
|
||||
import '../services/api.dart';
|
||||
import '../services/flood_station_provider.dart';
|
||||
import 'flood_station_page.dart';
|
||||
|
||||
class LandingPage extends StatelessWidget {
|
||||
class LandingPage extends StatefulWidget {
|
||||
const LandingPage({super.key});
|
||||
|
||||
static const routeName = '/';
|
||||
|
||||
Widget builder(
|
||||
BuildContext context, AsyncSnapshot<List<FloodStation>> snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return CircularProgressIndicator();
|
||||
} else if (snapshot.hasData) {
|
||||
return FloodStationListView(stations: snapshot.data!);
|
||||
} else if (snapshot.hasError) {
|
||||
return Text(snapshot.error.toString());
|
||||
} else {
|
||||
return Text('An unknown error occured.');
|
||||
}
|
||||
@override
|
||||
State<LandingPage> createState() => _LandingPageState();
|
||||
}
|
||||
|
||||
class _LandingPageState extends State<LandingPage> {
|
||||
late FloodStationProvider floodStationProvider;
|
||||
|
||||
@override
|
||||
initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance
|
||||
.addPostFrameCallback((_) => floodStationProvider.loadAllStations());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
floodStationProvider = context.watch<FloodStationProvider>();
|
||||
return Scaffold(
|
||||
body: FutureBuilder<List<FloodStation>>(
|
||||
future: Api.fetchStations(),
|
||||
builder: builder,
|
||||
body: FloodStationListView(
|
||||
stations: floodStationProvider.allStations,
|
||||
onItemTapped: (station) {
|
||||
floodStationProvider.selectedStation = station;
|
||||
Navigator.of(context).pushNamed(FloodStationPage.routeName);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user