From bedd9c7d889ddf1d300bc7ff11afcd311bc6a725 Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 24 Jan 2025 17:58:36 +0100 Subject: [PATCH] simple view of town names --- lib/main.dart | 7 +++++-- lib/pages/landing_page.dart | 38 +++++++++++++++++++++++++++++++++++++ lib/services/api.dart | 3 ++- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 lib/pages/landing_page.dart diff --git a/lib/main.dart b/lib/main.dart index afda00c..1e3dd72 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; -import 'services/api.dart'; +import 'pages/landing_page.dart'; void main() { runApp(const MyApp()); @@ -11,13 +11,16 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - Api.fetchStations(); return MaterialApp( title: 'Floodwatch', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), + initialRoute: LandingPage.routeName, + routes: { + LandingPage.routeName: (context) => LandingPage(), + }, ); } } diff --git a/lib/pages/landing_page.dart b/lib/pages/landing_page.dart new file mode 100644 index 0000000..64cfd61 --- /dev/null +++ b/lib/pages/landing_page.dart @@ -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> 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>( + future: Api.fetchStations(), + builder: builder, + ), + ); + } +} diff --git a/lib/services/api.dart b/lib/services/api.dart index afb7a43..2d5776b 100644 --- a/lib/services/api.dart +++ b/lib/services/api.dart @@ -7,7 +7,7 @@ class Api { static const String _rootUrl = 'https://environment.data.gov.uk/flood-monitoring'; - static Future fetchStations() async { + static Future> fetchStations() async { List stations = []; final response = await http.get(Uri.parse('$_rootUrl/id/stations')); if (response.statusCode == 200) { @@ -16,5 +16,6 @@ class Api { stations.add(FloodStation.fromMap(str)); } } + return stations; } }