Files
oeffimaster/lib/main.dart
T
fbachus 1c0616fa86 feat(departures): make some adaptations to supp diff departure srces
with these adaptations to the api_handler and Departure.fromJson,
the fromJson-factory now works for responses from both /departureBoard
and /tripDetail

for /tripDetail a little json-copying is used, see
ApiHandler._expandDeparturesJson
2026-07-23 21:42:13 +02:00

409 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'api_handler.dart';
import 'transport_helper.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart'; //for testing on desktop
void main() async {
await dotenv.load();
const String dbName = "oeffimasterTransportDb";
databaseFactory = databaseFactoryFfi; // for debugging and desktop test
DbHelper.initDb(dbName);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Oeffimaster',
theme: ThemeData(
colorScheme: .fromSeed(seedColor: Colors.yellow),
),
home: const MyHomePage(title: 'Oeffimaster'), //this is what matters
// routes: {
// //'/': (context) => const MyHomePage(title: 'Oeffimaster'),
// '/stations': (context) => const ChooseStationPage(),
// },
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
//String API_KEY = ApiHandler.accessID;
String API_KEY = "no telly";
//Future<Map<String, dynamic>> ?id;
List<Station> 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<List<Station>> future_id) async {
stationList = await future_id;
id = stationList[0].id;
setState(() {});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
//backgroundColor: Theme.of(context).colorScheme.inversePrimary,
//backgroundColor: Colors.amber,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
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),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChooseStationPage()),
);
},
child: const Text('Start a route'),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
class ChooseStationPage extends StatefulWidget {
const ChooseStationPage({super.key});
@override
State<ChooseStationPage> createState() => _ChooseStationPageState();
}
class _ChooseStationPageState extends State<ChooseStationPage> {
Station? _selectedStation;
List<Station> stationList = [];
void findNearbyStations() async {
stationList = await ApiHandler.nearbyStations(
// TODO: replace with location
"52.55",
"13.55",
maxStations: 20,
);
}
/* TODO: function to get stations along a vehicles route past current station,
* and run the correct function on switching to the screen
*/
void findStationsByName(
String name, {
int? resultnum,
}) async {
stationList = await ApiHandler.findStations(name);
}
void showResult(Future<List<Station>> future_id) async {
stationList = await future_id;
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('RoutePicker')),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () {
showResult(ApiHandler.findStations('S Biesdorf'));
},
child: Text('Search for \'S Biesdorf\''),
),
Expanded(
child: SizedBox(
height: 200,
child: StationList(
stationList: stationList,
onTapped: _handleStationTapped,
),
),
),
],
),
),
);
}
void _handleStationTapped(Station station) {
// setState(() {
// _selectedStation = station;
// });
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChooseDeparturePage(startStation: station),
),
);
}
}
class StationList extends StatelessWidget {
final List<Station> stationList;
final ValueChanged<Station> onTapped;
StationList({
required this.stationList,
required this.onTapped,
});
@override
Widget build(BuildContext context) {
return ListView(
children: [
for (Station station in stationList)
ListTile(
title: Text(station.name),
onTap: () => onTapped(station),
),
],
);
}
}
class ChooseDeparturePage extends StatefulWidget {
final Station startStation;
const ChooseDeparturePage({super.key, required this.startStation});
@override
State<ChooseDeparturePage> createState() => _ChooseDeparturePageState();
}
class _ChooseDeparturePageState extends State<ChooseDeparturePage> {
List<Departure> departureList = [];
Departure? _selectedDeparture;
void loadDepartures(Station startStation) async {
departureList = await ApiHandler.departures(startStation.id);
setState(() {});
}
@override
initState() {
loadDepartures(widget.startStation);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('RoutePicker for ${widget.startStation.name}'),
),
body: Center(
child: Column(
children: [
Expanded(
child: SizedBox(
height: 400,
child: DepartureList(
departureList: departureList,
onTapped: _handleDepartureTapped,
),
),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChooseStationPage()),
);
},
child: const Text('Vehicle picked!'),
),
],
),
),
);
}
void _handleDepartureTapped(Departure departure) {
// setState(() {
// _selectedDeparture = departure;
// });
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChooseArrivalPage(departure: departure),
),
);
}
}
class DepartureList extends StatelessWidget {
final List<Departure> departureList;
final ValueChanged<Departure> onTapped;
DepartureList({
required this.departureList,
required this.onTapped,
});
@override
Widget build(BuildContext context) {
return ListView(
children: [
for (Departure departure in departureList)
ListTile(
title: Text(departure.vehicleName),
subtitle: Text(departure.departureTime.toString()),
onTap: () => onTapped(departure),
),
],
);
}
}
class ChooseArrivalPage extends StatefulWidget {
final Departure departure;
const ChooseArrivalPage({
super.key,
required this.departure,
});
@override
State<ChooseArrivalPage> createState() => _ChooseArrivalPageState();
}
class _ChooseArrivalPageState extends State<ChooseArrivalPage> {
List<Departure> departureList = [];
Departure? _selectedDeparture;
void loadArrivals(String ref) async {
departureList = await ApiHandler.tripDetail(ref);
setState(() {});
}
@override
initState() {
loadArrivals(widget.departure.ref);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Arrivals for ${widget.departure.vehicleName}'),
),
body: Center(
child: Column(
children: [
Expanded(
child: SizedBox(
height: 400,
child: ArrivalList(
departureList: departureList,
onTapped: _handleArrivalTapped,
),
),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChooseStationPage()),
);
},
child: const Text('Vehicle picked!'),
),
],
),
),
);
}
void _handleArrivalTapped(Departure departure) async {
// setState(() {
// _selectedDeparture = departure;
// });
Station startStation = await Station.dbGet(departure.stationId);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChooseDeparturePage(startStation: startStation),
),
);
}
}
class ArrivalList extends StatelessWidget {
final List<Departure> departureList;
final ValueChanged<Departure> onTapped;
const ArrivalList({
required this.departureList,
required this.onTapped,
});
@override
Widget build(BuildContext context) {
return ListView(
children: [
for (Departure departure in departureList)
ListTile(
title: Text(departure.stationName),
subtitle: Text(departure.arrivalTime.toString()),
onTap: () => onTapped(departure),
),
],
);
}
}