fmassive/lib/set_list.dart

96 lines
2.8 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';
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;
late Stream<List<GymSet>> stream;
@override
initState() {
super.initState();
setStream();
}
void setStream() {
stream = (db.select(db.gymSets)
..where((gymSet) => gymSet.name.contains(widget.search))
..limit(10, offset: 0))
.watch();
}
@override
didUpdateWidget(covariant SetList oldWidget) {
super.didUpdateWidget(oldWidget);
setStream();
}
void toggleSearch() {
setState(() {
showSearch = !showSearch;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<List<GymSet>>(
stream: stream,
builder: (context, snapshot) {
final gymSets = snapshot.data;
if (gymSets == null)
return const Center(child: CircularProgressIndicator());
return ListView.builder(
itemCount: gymSets.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(gymSets[index].name),
subtitle: Text(
"${gymSets[index].reps} x ${gymSets[index].weight}kg"),
trailing: Text(DateFormat("yyyy-MM-dd")
.format(DateTime.parse(gymSets[index].created))),
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditGymSetPage(
gymSet: gymSets[index].toCompanion(false)),
),
);
});
},
);
}),
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()))),
),
);
},
child: const Icon(Icons.add)));
}
}