From b414655a5643c2710d213d97f96bf06aa54d3114 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 22 Mar 2020 15:58:39 +0100 Subject: [PATCH] Add basic work selector --- lib/selectors/work.dart | 71 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 lib/selectors/work.dart diff --git a/lib/selectors/work.dart b/lib/selectors/work.dart new file mode 100644 index 0000000..e42ebfc --- /dev/null +++ b/lib/selectors/work.dart @@ -0,0 +1,71 @@ +import 'package:flutter/material.dart'; + +import '../backend.dart'; +import '../database.dart'; +import '../editors/work.dart'; + +// TODO: Lazy load works and/or optimize queries. +class WorkSelector extends StatelessWidget { + @override + Widget build(BuildContext context) { + final backend = Backend.of(context); + + return Scaffold( + appBar: AppBar( + title: Text('Select work'), + ), + body: StreamBuilder>( + stream: backend.db.allPersons().watch(), + builder: (context, snapshot) { + if (snapshot.hasData) { + return ListView.builder( + itemCount: snapshot.data.length, + itemBuilder: (context, index) { + final person = snapshot.data[index]; + final title = Text('${person.lastName}, ${person.firstName}'); + return StreamBuilder>( + stream: backend.db.worksByComposer(person.id).watch(), + builder: (context, snapshot) { + if (snapshot.hasData && snapshot.data.isNotEmpty) { + return ExpansionTile( + title: title, + children: [ + for (final work in snapshot.data) + ListTile( + title: Text(work.title), + onTap: () => Navigator.pop(context, work), + ), + ], + ); + } else { + return ListTile( + title: title, + ); + } + }, + ); + }, + ); + } else { + return Container(); + } + }, + ), + floatingActionButton: FloatingActionButton( + child: const Icon(Icons.add), + onPressed: () async { + final Work work = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => WorkEditor(), + fullscreenDialog: true, + )); + + if (work != null) { + Navigator.pop(context, work); + } + }, + ), + ); + } +}