From 3738a17145b9277b2cb2768d421170ede21b9db4 Mon Sep 17 00:00:00 2001 From: fbachus Date: Thu, 30 Jul 2026 19:06:12 +0200 Subject: [PATCH] feat(ui): show journeys on homescreen and add route to journeyDetails also clean up clutter from homescreen --- lib/main.dart | 166 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 135 insertions(+), 31 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index c8eef6d..6b88ef3 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -24,7 +24,8 @@ class MyApp extends StatelessWidget { theme: ThemeData( colorScheme: .fromSeed(seedColor: Colors.yellow), ), - home: const MyHomePage(title: 'Oeffimaster'), //this is what matters + // home: const MyHomePage(title: 'Oeffimaster'), //this is what matters + home: const JourneyOverviewPage(), // routes: { // //'/': (context) => const MyHomePage(title: 'Oeffimaster'), // '/stations': (context) => const ChooseStationPage(), @@ -49,17 +50,6 @@ class _MyHomePageState extends State { List stationList = []; String id = ''; - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - // TODO :find a better way to show the list void showResult(Future> futureId) async { stationList = await futureId; @@ -87,19 +77,19 @@ class _MyHomePageState extends State { child: Column( mainAxisAlignment: .center, children: [ - const Text('You have pushed the button this many times:'), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - Text('API key from ENV is $API_KEY'), - MaterialButton( - onPressed: () { - showResult(ApiHandler.findStations('S Biesdorf')); - }, - child: Text('Search for \'S Biesdorf\''), - ), - Text(id), + // const Text('You have pushed the button this many times:'), + // Text( + // '$_counter', + // style: Theme.of(context).textTheme.headlineMedium, + // ), + // Text('API key from ENV is $API_KEY'), + // MaterialButton( + // onPressed: () { + // showResult(ApiHandler.findStations('S Biesdorf')); + // }, + // child: Text('Search for \'S Biesdorf\''), + // ), + // Text(id), ElevatedButton( onPressed: () { Navigator.push( @@ -113,8 +103,13 @@ class _MyHomePageState extends State { ), ), floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', + onPressed: () { + Navigator.push( + context, + MaterialPageRoute(builder: (context) => ChooseStationPage()), + ); + }, + tooltip: 'Start a route', child: const Icon(Icons.add), ), ); @@ -514,17 +509,120 @@ class _JourneyOverviewPageState extends State { List allJourneys = []; @override - initState() async { + initState() { super.initState(); + } + + // void retry() async { + // // retry with setstate + // await Future.delayed(Duration(milliseconds: 1300)); + // setState(() {}); + // } + + Future> loadJourneys() async { // TODO: implement batch query to get a list (all) of journeys - allJourneys = await Journey.dbGetAll(); + List journeys; + if (allJourneys.isEmpty) { + await Future.delayed(Duration(milliseconds: 500)); + journeys = await Journey.dbGetAll(); + allJourneys = journeys; + } else { + journeys = allJourneys; + } + return journeys; } @override Widget build(BuildContext context) { return Scaffold( body: Center( - child: JourneyList(journeys: allJourneys), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + FutureBuilder( + future: loadJourneys(), + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return Column( + mainAxisAlignment: MainAxisAlignment.center, + spacing: 16, + children: [ + const CircularProgressIndicator(), + const Text('Loading Journeys...'), + ], + ); + } + if (snapshot.hasError) { + return Column( + children: [ + Text('couldn\'t load Journeys'), + // ElevatedButton( + // onPressed: () { + // Future.delayed(Duration(milliseconds: 300)); + // setState(() {}); + // }, + // child: const Text('retry'), + // ), + ], + ); + } + if (snapshot.hasData == false) { + return Column( + children: [ + Text('No journeys found'), + // ElevatedButton( + // onPressed: () { + // Future.delayed(Duration(milliseconds: 300)); + // setState(() {}); + // }, + // child: const Text('retry'), + // ), + ], + ); + } + return Expanded( + child: SizedBox( + height: 1, + child: JourneyList( + journeys: snapshot.data!, + onTapped: _handleJourneyTapped, + ), + ), + ); + }, + ), + ElevatedButton( + onPressed: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => ChooseStationPage(), + ), + ); + }, + child: const Text('Start a route'), + ), + ], + ), + ), + floatingActionButton: FloatingActionButton( + onPressed: () { + Navigator.push( + context, + MaterialPageRoute(builder: (context) => ChooseStationPage()), + ); + }, + tooltip: 'Start a route', + child: const Icon(Icons.add), + ), + ); + } + + void _handleJourneyTapped(Journey journey) async { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => JourneyDetailPage(singleJourney: journey), ), ); } @@ -532,9 +630,12 @@ class _JourneyOverviewPageState extends State { class JourneyList extends StatelessWidget { final List journeys; + final Function onTapped; + const JourneyList({ super.key, required this.journeys, + required this.onTapped, }); @override @@ -542,7 +643,10 @@ class JourneyList extends StatelessWidget { return ListView( children: [ for (Journey jour in journeys) - ListTile(title: Text('${jour.startStation} to ${jour.endStation}')), + ListTile( + title: Text('${jour.startStation} to ${jour.endStation}'), + onTap: () => onTapped(jour), + ), ], ); }