2023-04-15 17:03:30 +12:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:fmassive/database.dart';
|
|
|
|
import 'package:fmassive/edit_set.dart';
|
|
|
|
import 'package:fmassive/main.dart';
|
2023-11-10 18:50:56 +13:00
|
|
|
import 'package:fmassive/set_tile.dart';
|
2023-04-17 11:03:27 +12:00
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
2023-04-15 17:03:30 +12:00
|
|
|
import 'package:moor_flutter/moor_flutter.dart';
|
|
|
|
|
|
|
|
class SetList extends StatefulWidget {
|
|
|
|
final String search;
|
|
|
|
|
2023-11-10 17:09:20 +13:00
|
|
|
const SetList({super.key, required this.search});
|
2023-04-15 17:03:30 +12:00
|
|
|
|
|
|
|
@override
|
|
|
|
createState() => _SetList();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SetList extends State<SetList> {
|
|
|
|
bool showSearch = false;
|
2023-04-17 11:03:27 +12:00
|
|
|
final PagingController<int, GymSet> pagingController =
|
|
|
|
PagingController(firstPageKey: 0);
|
2023-04-15 17:03:30 +12:00
|
|
|
|
|
|
|
@override
|
|
|
|
initState() {
|
|
|
|
super.initState();
|
2023-04-17 11:03:27 +12:00
|
|
|
pagingController.addPageRequestListener((pageKey) {
|
|
|
|
fetch(pageKey);
|
|
|
|
});
|
2023-04-15 17:03:30 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
didUpdateWidget(covariant SetList oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
2023-04-17 11:03:27 +12:00
|
|
|
pagingController.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> fetch(int pageKey) async {
|
2023-04-17 11:50:31 +12:00
|
|
|
final gymSets = await (db.select(db.gymSets)
|
|
|
|
..where((gymSet) => gymSet.name.contains(widget.search))
|
2023-11-10 16:16:28 +13:00
|
|
|
..orderBy([
|
|
|
|
(u) => OrderingTerm(expression: u.created, mode: OrderingMode.desc),
|
|
|
|
])
|
2023-04-17 11:50:31 +12:00
|
|
|
..limit(10, offset: pageKey * 10))
|
|
|
|
.get();
|
2023-04-17 11:03:27 +12:00
|
|
|
|
2023-04-17 11:50:31 +12:00
|
|
|
final isLastPage = gymSets.length < 10;
|
2023-04-17 11:03:27 +12:00
|
|
|
|
2023-04-17 11:50:31 +12:00
|
|
|
if (isLastPage) {
|
|
|
|
pagingController.appendLastPage(gymSets);
|
|
|
|
} else {
|
|
|
|
final nextPageKey = pageKey + 1;
|
|
|
|
pagingController.appendPage(gymSets, nextPageKey);
|
2023-04-17 11:03:27 +12:00
|
|
|
}
|
2023-04-15 17:03:30 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
void toggleSearch() {
|
|
|
|
setState(() {
|
|
|
|
showSearch = !showSearch;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2023-04-17 11:03:27 +12:00
|
|
|
body: PagedListView<int, GymSet>(
|
|
|
|
pagingController: pagingController,
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<GymSet>(
|
2023-11-10 18:50:56 +13:00
|
|
|
itemBuilder: (context, gymSet, index) => SetTile(
|
|
|
|
pagingController: pagingController,
|
|
|
|
gymSet: gymSet,
|
2023-04-17 11:03:27 +12:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
onPressed: () async {
|
|
|
|
await Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
2023-11-29 16:33:01 +13:00
|
|
|
builder: (context) => const EditGymSetPage(
|
2023-04-17 11:03:27 +12:00
|
|
|
gymSet: GymSetsCompanion(
|
2023-11-29 16:33:01 +13:00
|
|
|
name: Value(''),
|
|
|
|
reps: Value(0),
|
|
|
|
weight: Value(0),
|
|
|
|
image: Value(''),
|
2023-04-17 11:03:27 +12:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
pagingController.refresh();
|
|
|
|
},
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
),
|
|
|
|
);
|
2023-04-15 17:03:30 +12:00
|
|
|
}
|
|
|
|
}
|