musicus_mobile/common/lib/src/editors/instrument.dart

98 lines
2.5 KiB
Dart
Raw Normal View History

2019-12-03 12:47:23 +01:00
import 'package:flutter/material.dart';
2020-04-24 22:41:52 +02:00
import 'package:musicus_database/musicus_database.dart';
2019-12-03 12:47:23 +01:00
import '../backend.dart';
class InstrumentEditor extends StatefulWidget {
final Instrument instrument;
InstrumentEditor({
this.instrument,
});
@override
_InstrumentEditorState createState() => _InstrumentEditorState();
}
class _InstrumentEditorState extends State<InstrumentEditor> {
final nameController = TextEditingController();
2020-04-26 18:29:21 +02:00
bool uploading = false;
2019-12-03 12:47:23 +01:00
@override
void initState() {
super.initState();
if (widget.instrument != null) {
nameController.text = widget.instrument.name;
}
}
@override
Widget build(BuildContext context) {
final backend = MusicusBackend.of(context);
2019-12-03 12:47:23 +01:00
return Scaffold(
appBar: AppBar(
title: Text('Instrument/Role'),
2019-12-03 12:47:23 +01:00
actions: <Widget>[
2020-04-26 18:29:21 +02:00
uploading
? Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: SizedBox(
width: 24.0,
height: 24.0,
child: CircularProgressIndicator(
strokeWidth: 2.0,
),
),
),
)
: FlatButton(
child: Text('DONE'),
onPressed: () async {
setState(() {
uploading = true;
});
final instrument = Instrument(
id: widget.instrument?.id ?? generateId(),
name: nameController.text,
);
final success =
await backend.client.putInstrument(instrument);
setState(() {
uploading = false;
});
2019-12-03 12:47:23 +01:00
2020-04-26 18:29:21 +02:00
if (success) {
Navigator.pop(context, instrument);
} else {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Failed to upload'),
));
}
},
),
2019-12-03 12:47:23 +01:00
],
),
body: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: nameController,
decoration: InputDecoration(
labelText: 'Name',
),
),
),
],
),
);
}
}