import 'package:flutter/material.dart'; import 'package:fmassive/database.dart'; import 'package:fmassive/edit_set.dart'; import 'package:fmassive/main.dart'; import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; 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 { bool showSearch = false; final PagingController pagingController = PagingController(firstPageKey: 0); @override initState() { super.initState(); pagingController.addPageRequestListener((pageKey) { fetch(pageKey); }); } @override didUpdateWidget(covariant SetList oldWidget) { super.didUpdateWidget(oldWidget); pagingController.refresh(); } Future fetch(int pageKey) async { final gymSets = await (db.select(db.gymSets) ..where((gymSet) => gymSet.name.contains(widget.search)) ..orderBy([ (u) => OrderingTerm(expression: u.created, mode: OrderingMode.desc), ]) ..limit(10, offset: pageKey * 10)) .get(); final isLastPage = gymSets.length < 10; if (isLastPage) { pagingController.appendLastPage(gymSets); } else { final nextPageKey = pageKey + 1; pagingController.appendPage(gymSets, nextPageKey); } } void toggleSearch() { setState(() { showSearch = !showSearch; }); } @override Widget build(BuildContext context) { return Scaffold( body: PagedListView( pagingController: pagingController, builderDelegate: PagedChildBuilderDelegate( 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 { await Navigator.push( context, MaterialPageRoute( builder: (context) => EditGymSetPage( gymSet: gymSet.toCompanion(false), ), ), ); pagingController.refresh(); }, ), 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), ), ); } }