simple view of town names
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'services/api.dart';
|
import 'pages/landing_page.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@@ -11,13 +11,16 @@ class MyApp extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
Api.fetchStations();
|
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Floodwatch',
|
title: 'Floodwatch',
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
|
initialRoute: LandingPage.routeName,
|
||||||
|
routes: {
|
||||||
|
LandingPage.routeName: (context) => LandingPage(),
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
38
lib/pages/landing_page.dart
Normal file
38
lib/pages/landing_page.dart
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import '../model/flood_station.dart';
|
||||||
|
import '../services/api.dart';
|
||||||
|
|
||||||
|
class LandingPage extends StatelessWidget {
|
||||||
|
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 ListView.builder(
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
return ListTile(
|
||||||
|
title: Text(snapshot.data!.elementAt(index).town),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
itemCount: snapshot.data!.length,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Placeholder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
body: FutureBuilder<List<FloodStation>>(
|
||||||
|
future: Api.fetchStations(),
|
||||||
|
builder: builder,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ class Api {
|
|||||||
static const String _rootUrl =
|
static const String _rootUrl =
|
||||||
'https://environment.data.gov.uk/flood-monitoring';
|
'https://environment.data.gov.uk/flood-monitoring';
|
||||||
|
|
||||||
static Future<void> fetchStations() async {
|
static Future<List<FloodStation>> fetchStations() async {
|
||||||
List<FloodStation> stations = [];
|
List<FloodStation> stations = [];
|
||||||
final response = await http.get(Uri.parse('$_rootUrl/id/stations'));
|
final response = await http.get(Uri.parse('$_rootUrl/id/stations'));
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
@@ -16,5 +16,6 @@ class Api {
|
|||||||
stations.add(FloodStation.fromMap(str));
|
stations.add(FloodStation.fromMap(str));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return stations;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user