import 'package:flutter/material.dart'; import 'package:fmassive/database.dart'; import 'package:fmassive/edit_plan.dart'; import 'package:fmassive/main.dart'; import 'package:moor/moor.dart'; class PlansPage extends StatelessWidget { const PlansPage({super.key, required this.search}); final String search; @override Widget build(BuildContext context) { return Scaffold( body: Center( child: _PlansPage(search: search), ), ); } } class _PlansPage extends StatefulWidget { const _PlansPage({super.key, required this.search}); final String search; @override createState() => _PlansPageState(); } class _PlansPageState extends State<_PlansPage> { bool showSearch = false; late Stream> stream; @override initState() { super.initState(); setStream(); } void setStream() { stream = (db.select(db.plans) ..where((gymSet) => gymSet.days.contains(widget.search)) ..limit(10, offset: 0)) .watch(); } @override didUpdateWidget(covariant _PlansPage oldWidget) { super.didUpdateWidget(oldWidget); setStream(); } void toggleSearch() { setState(() { showSearch = !showSearch; }); } @override Widget build(BuildContext context) { return Scaffold( body: StreamBuilder>( stream: stream, builder: (context, snapshot) { final plans = snapshot.data; if (plans == null) return const Center(child: CircularProgressIndicator()); return ListView.builder( itemCount: plans.length, itemBuilder: (context, index) { return ListTile( title: Text(plans[index].days.replaceAll(',', ', ')), subtitle: Text(plans[index].workouts.replaceAll(',', ', ')), onTap: () async { await Navigator.push( context, MaterialPageRoute( builder: (context) => EditPlanPage( plan: plans[index].toCompanion(false)), ), ); }); }, ); }), floatingActionButton: FloatingActionButton( onPressed: () async { await Navigator.push( context, MaterialPageRoute( builder: (context) => const EditPlanPage( plan: PlansCompanion(days: Value(''), workouts: Value(''))), ), ); }, child: const Icon(Icons.add))); } }