fmassive/lib/set_list.dart

115 lines
3.3 KiB
Dart
Raw Normal View History

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-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:intl/intl.dart';
import 'package:moor_flutter/moor_flutter.dart';
class SetList extends StatefulWidget {
final String search;
const SetList({Key? key, required this.search}) : super(key: key);
@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 {
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),
])
..limit(10, offset: pageKey * 10))
.get();
2023-04-17 11:03:27 +12:00
final isLastPage = gymSets.length < 10;
2023-04-17 11:03:27 +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>(
itemBuilder: (context, gymSet, index) => ListTile(
title: Text(gymSet.name),
subtitle: Text("${gymSet.reps} x ${gymSet.weight}kg"),
trailing: Text(
DateFormat("yyyy-MM-dd").format(DateTime.parse(gymSet.created)),
),
onTap: () async {
2023-04-15 17:03:30 +12:00
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditGymSetPage(
2023-04-17 11:03:27 +12:00
gymSet: gymSet.toCompanion(false),
),
2023-04-15 17:03:30 +12:00
),
);
2023-04-17 11:03:27 +12:00
pagingController.refresh();
2023-04-15 17:03:30 +12:00
},
2023-04-17 11:03:27 +12:00
),
firstPageProgressIndicatorBuilder: (_) =>
const Center(child: CircularProgressIndicator()),
newPageProgressIndicatorBuilder: (_) =>
const Center(child: CircularProgressIndicator()),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditGymSetPage(
gymSet: GymSetsCompanion(
name: const Value(''),
reps: const Value(0),
weight: const Value(0),
image: const Value(''),
created: Value(DateTime.now().toString()),
),
),
),
);
pagingController.refresh();
},
child: const Icon(Icons.add),
),
);
2023-04-15 17:03:30 +12:00
}
}