| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							| 
									
										
										
										
											2020-04-24 22:41:52 +02:00
										 |  |  | import 'package:musicus_database/musicus_database.dart'; | 
					
						
							| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import '../backend.dart'; | 
					
						
							|  |  |  | import '../editors/instrument.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class InstrumentsSelector extends StatefulWidget { | 
					
						
							| 
									
										
										
										
											2020-03-31 15:49:15 +02:00
										 |  |  |   final bool multiple; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:19:56 +01:00
										 |  |  |   final List<Instrument> selection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   InstrumentsSelector({ | 
					
						
							| 
									
										
										
										
											2020-03-31 15:49:15 +02:00
										 |  |  |     this.multiple = false, | 
					
						
							| 
									
										
										
										
											2019-12-11 13:19:56 +01:00
										 |  |  |     this.selection, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  |   @override | 
					
						
							|  |  |  |   _InstrumentsSelectorState createState() => _InstrumentsSelectorState(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _InstrumentsSelectorState extends State<InstrumentsSelector> { | 
					
						
							|  |  |  |   Set<Instrument> selection = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:19:56 +01:00
										 |  |  |   @override | 
					
						
							|  |  |  |   void initState() { | 
					
						
							|  |  |  |     super.initState(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (widget.selection != null) { | 
					
						
							|  |  |  |       selection = widget.selection.toSet(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							|  |  |  |     final backend = Backend.of(context); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Scaffold( | 
					
						
							|  |  |  |       appBar: AppBar( | 
					
						
							| 
									
										
										
										
											2020-03-31 15:49:15 +02:00
										 |  |  |         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, | 
					
						
							| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  |       ), | 
					
						
							|  |  |  |       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]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-31 15:49:15 +02:00
										 |  |  |                 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), | 
					
						
							|  |  |  |                   ); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  |               }, | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           } 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, | 
					
						
							|  |  |  |               )); | 
					
						
							| 
									
										
										
										
											2019-12-11 13:19:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  |           if (instrument != null) { | 
					
						
							| 
									
										
										
										
											2020-03-31 15:49:15 +02:00
										 |  |  |             if (widget.multiple) { | 
					
						
							|  |  |  |               setState(() { | 
					
						
							|  |  |  |                 selection.add(instrument); | 
					
						
							|  |  |  |               }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |               Navigator.pop(context, instrument); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-12-11 12:44:30 +01:00
										 |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |