mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47:25 +01:00
The role and instrument tables have been merged into one (the instrument table). There are not that many roles that aren't instruments and it is much simpler to mange this way. The role editor and role selector have been removed and the instrument related UI parts have been modified accordingly.
108 lines
3 KiB
Dart
108 lines
3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../backend.dart';
|
|
import '../database.dart';
|
|
import '../editors/instrument.dart';
|
|
|
|
class InstrumentsSelector extends StatefulWidget {
|
|
final bool multiple;
|
|
final List<Instrument> selection;
|
|
|
|
InstrumentsSelector({
|
|
this.multiple = false,
|
|
this.selection,
|
|
});
|
|
|
|
@override
|
|
_InstrumentsSelectorState createState() => _InstrumentsSelectorState();
|
|
}
|
|
|
|
class _InstrumentsSelectorState extends State<InstrumentsSelector> {
|
|
Set<Instrument> selection = {};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
if (widget.selection != null) {
|
|
selection = widget.selection.toSet();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final backend = Backend.of(context);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.multiple ? 'Select instruments/roles' : 'Select instrument/role'),
|
|
actions: widget.multiple
|
|
? <Widget>[
|
|
FlatButton(
|
|
child: Text('DONE'),
|
|
onPressed: () => Navigator.pop(context, selection.toList()),
|
|
),
|
|
]
|
|
: null,
|
|
),
|
|
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];
|
|
|
|
if (widget.multiple) {
|
|
return CheckboxListTile(
|
|
title: Text(instrument.name),
|
|
value: selection.contains(instrument),
|
|
checkColor: Colors.black,
|
|
onChanged: (selected) {
|
|
setState(() {
|
|
if (selected) {
|
|
selection.add(instrument);
|
|
} else {
|
|
selection.remove(instrument);
|
|
}
|
|
});
|
|
},
|
|
);
|
|
} else {
|
|
return ListTile(
|
|
title: Text(instrument.name),
|
|
onTap: () => Navigator.pop(context, 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) {
|
|
if (widget.multiple) {
|
|
setState(() {
|
|
selection.add(instrument);
|
|
});
|
|
} else {
|
|
Navigator.pop(context, instrument);
|
|
}
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|