Pull down to refresh (yw lingy)

This commit is contained in:
2023-12-27 16:55:37 +02:00
parent c9f990e7fd
commit cecc3a93a8

View File

@@ -24,53 +24,64 @@ class _FetchAPIState extends State<FetchAPI> {
fetchedArrivals = fetchArrivals(); fetchedArrivals = fetchArrivals();
}); });
}, },
icon: const Icon(Icons.refresh_rounded)) icon: const Icon(Icons.refresh_sharp),
),
const SizedBox(width: 30,),
], ],
), ),
body: Column( body: RefreshIndicator(
mainAxisAlignment: MainAxisAlignment.start, onRefresh: _refreshData,
children: [ child: Column(
Expanded( mainAxisAlignment: MainAxisAlignment.start,
child: SizedBox( children: [
child: FutureBuilder<List<Arrival>>( Expanded(
future: fetchedArrivals, child: SizedBox(
builder: (context, snapshot) { child: FutureBuilder<List<Arrival>>(
if (snapshot.connectionState == ConnectionState.waiting) { future: fetchedArrivals,
return const CircularProgressIndicator(); builder: (context, snapshot) {
} else if (snapshot.hasError) { if (snapshot.connectionState == ConnectionState.waiting) {
return Text('Failed to fetch data.\nError: ${snapshot.error}'); return const CircularProgressIndicator();
} else { } else if (snapshot.hasError) {
return ListView.builder( return Text('Failed to fetch data.\nError: ${snapshot.error}');
itemCount: snapshot.data!.length, } else {
itemBuilder: (context, index) { return ListView.builder(
return ListTile( itemCount: snapshot.data!.length,
title: Text("Arrival Time: ${snapshot.data![index].timeOfDay}"), itemBuilder: (context, index) {
titleAlignment: ListTileTitleAlignment.center, return ListTile(
subtitle: Text("Week Day: ${weekNumToString(snapshot.data![index].weekDay)}"), title: Text("Arrival Time: ${snapshot.data![index].timeOfDay}"),
); titleAlignment: ListTileTitleAlignment.center,
}, subtitle: Text("Week Day: ${weekNumToString(snapshot.data![index].weekDay)}"),
); );
} },
}, );
}
},
),
), ),
), ),
), const SizedBox(height: 30,),
const SizedBox(height: 30,), InkWell(
InkWell( onTap: () {
onTap: () { Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false); },
}, child: Container(
child: Container( padding: const EdgeInsets.all(20.0),
padding: const EdgeInsets.all(20.0), child: const Text("Homescreen"),
child: const Text("Homescreen"), ),
), ),
), const SizedBox(height: 35,),
const SizedBox(height: 35,), ],
], ),
), ),
); );
} }
Future<void> _refreshData() async {
setState(() {
fetchedArrivals = fetchArrivals();
});
}
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@@ -97,4 +108,4 @@ String weekNumToString(int weekday) {
} }
throw "AAAAAAAAA"; throw "AAAAAAAAA";
} }