From 0594b001992b8c45bbde827c9a0bc28e5897d65d Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Thu, 12 Dec 2019 14:57:51 +0100 Subject: [PATCH] Make work editor modular The work editor holds all data for a work and its parts. The widgets for editing work information are reusable now. --- lib/editors/work.dart | 149 +++++++++++++++++++++++++++--------------- 1 file changed, 95 insertions(+), 54 deletions(-) diff --git a/lib/editors/work.dart b/lib/editors/work.dart index fe09ebd..9bdbc7d 100644 --- a/lib/editors/work.dart +++ b/lib/editors/work.dart @@ -5,6 +5,84 @@ import '../database.dart'; import '../selectors/instruments.dart'; import '../selectors/person.dart'; +class WorkData { + String title = ''; + Person composer; + List instruments = []; + List parts = []; +} + +class WorkProperties extends StatelessWidget { + final TextEditingController titleController; + final Person composer; + final List instruments; + final void Function(Person) onComposerChanged; + final void Function(List) onInstrumentsChanged; + + WorkProperties({ + @required this.titleController, + @required this.composer, + @required this.instruments, + @required this.onComposerChanged, + @required this.onInstrumentsChanged, + }); + + @override + Widget build(BuildContext context) { + return Column( + children: [ + Padding( + padding: const EdgeInsets.all(16.0), + child: TextField( + controller: titleController, + decoration: InputDecoration( + labelText: 'Title', + ), + ), + ), + ListTile( + title: Text('Composer'), + subtitle: Text(composer != null + ? '${composer.firstName} ${composer.lastName}' + : 'Select composer'), + onTap: () async { + final Person person = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => PersonsSelector(), + fullscreenDialog: true, + )); + + if (person != null) { + onComposerChanged(person); + } + }, + ), + ListTile( + title: Text('Instruments'), + subtitle: Text(instruments.isNotEmpty + ? instruments.map((i) => i.name).join(', ') + : 'Select instruments'), + onTap: () async { + final List selection = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => InstrumentsSelector( + selection: instruments, + ), + fullscreenDialog: true, + )); + + if (selection != null) { + onInstrumentsChanged(selection); + } + }, + ), + ], + ); + } +} + class WorkEditor extends StatefulWidget { final Work work; @@ -20,8 +98,7 @@ class _WorkEditorState extends State { final titleController = TextEditingController(); Backend backend; - Person composer; - List instruments = []; + final WorkData data = WorkData(); @override void initState() { @@ -45,9 +122,9 @@ class _WorkEditorState extends State { await backend.db.personById(widget.work.composer).getSingle(); // We don't want to override a newly selected composer. - if (composer != null) { + if (data.composer != null) { setState(() { - composer = person; + data.composer = person; }); } }(); @@ -58,9 +135,9 @@ class _WorkEditorState extends State { await backend.db.instrumentsByWork(widget.work.id).get(); // We don't want to override already selected instruments. - if (instruments.isEmpty) { + if (data.instruments.isEmpty) { setState(() { - instruments = selection; + data.instruments = selection; }); } }(); @@ -81,55 +158,19 @@ class _WorkEditorState extends State { ), body: ListView( children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: TextField( - controller: titleController, - decoration: InputDecoration( - labelText: 'Title', - ), - ), - ), - ListTile( - title: Text('Composer'), - subtitle: Text(composer != null - ? '${composer.firstName} ${composer.lastName}' - : 'Select composer'), - onTap: () async { - final Person person = await Navigator.push( - context, - MaterialPageRoute( - builder: (context) => PersonsSelector(), - fullscreenDialog: true, - )); - - if (person != null) { - setState(() { - composer = person; - }); - } + WorkProperties( + titleController: titleController, + composer: data.composer, + instruments: data.instruments, + onComposerChanged: (composer) { + setState(() { + data.composer = composer; + }); }, - ), - ListTile( - title: Text('Instruments'), - subtitle: Text(instruments.isNotEmpty - ? instruments.map((i) => i.name).join(', ') - : 'Select instruments'), - onTap: () async { - final List selection = await Navigator.push( - context, - MaterialPageRoute( - builder: (context) => InstrumentsSelector( - selection: instruments, - ), - fullscreenDialog: true, - )); - - if (selection != null) { - setState(() { - instruments = selection; - }); - } + onInstrumentsChanged: (instruments) { + setState(() { + data.instruments = instruments; + }); }, ), ],