From d9149d2ff25e6dfa9637e38dd0a656e4b03b75a5 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Wed, 11 Dec 2019 12:44:30 +0100 Subject: [PATCH] Add instruments selector This also adds a tile to the temporary home screen for debugging purposes. --- lib/screens/home.dart | 10 +++++ lib/selectors/instruments.dart | 77 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 lib/selectors/instruments.dart diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 9dc9c6e..b244bf2 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import '../backend.dart'; import '../selectors/person.dart'; +import '../selectors/instruments.dart'; class HomeScreen extends StatelessWidget { @override @@ -32,6 +33,15 @@ class HomeScreen extends StatelessWidget { fullscreenDialog: true, )), ), + ListTile( + title: Text('Select instrument'), + onTap: () => Navigator.push( + context, + MaterialPageRoute( + builder: (context) => InstrumentsSelector(), + fullscreenDialog: true, + )), + ), ], ), ); diff --git a/lib/selectors/instruments.dart b/lib/selectors/instruments.dart new file mode 100644 index 0000000..0d75a38 --- /dev/null +++ b/lib/selectors/instruments.dart @@ -0,0 +1,77 @@ +import 'package:flutter/material.dart'; + +import '../backend.dart'; +import '../database.dart'; +import '../editors/instrument.dart'; + +class InstrumentsSelector extends StatefulWidget { + @override + _InstrumentsSelectorState createState() => _InstrumentsSelectorState(); +} + +class _InstrumentsSelectorState extends State { + Set selection = {}; + + @override + Widget build(BuildContext context) { + final backend = Backend.of(context); + + return Scaffold( + appBar: AppBar( + title: Text('Select instruments'), + actions: [ + FlatButton( + child: Text('DONE'), + onPressed: () => Navigator.pop(context, selection.toList()), + ), + ], + ), + body: StreamBuilder( + stream: backend.db.allInstruments().watch(), + builder: (context, snapshot) { + if (snapshot.hasData) { + return ListView.builder( + itemCount: snapshot.data.length, + itemBuilder: (context, index) { + final instrument = snapshot.data[index]; + + return CheckboxListTile( + title: Text(instrument.name), + value: selection.contains(instrument), + onChanged: (selected) { + setState(() { + if (selected) { + selection.add(instrument); + } else { + selection.remove(instrument); + } + }); + }, + ); + }, + ); + } else { + return Container(); + } + }, + ), + floatingActionButton: FloatingActionButton( + child: const Icon(Icons.add), + onPressed: () async { + final Instrument instrument = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => InstrumentEditor(), + fullscreenDialog: true, + )); + + if (instrument != null) { + setState(() { + selection.add(instrument); + }); + } + }, + ), + ); + } +}