2021-10-22 23:08:09 +00:00
|
|
|
import 'package:diameter/components/detail.dart';
|
|
|
|
import 'package:diameter/components/dialogs.dart';
|
|
|
|
import 'package:diameter/config.dart';
|
|
|
|
import 'package:diameter/navigation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:diameter/components/forms.dart';
|
|
|
|
import 'package:diameter/models/accuracy.dart';
|
|
|
|
|
|
|
|
class AccuracyDetailScreen extends StatefulWidget {
|
|
|
|
static const String routeName = '/accuracy';
|
|
|
|
final Accuracy? accuracy;
|
|
|
|
|
|
|
|
const AccuracyDetailScreen({Key? key, this.accuracy}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_AccuracyDetailScreenState createState() => _AccuracyDetailScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AccuracyDetailScreenState extends State<AccuracyDetailScreen> {
|
|
|
|
final GlobalKey<FormState> _accuracyForm = GlobalKey<FormState>();
|
|
|
|
final _valueController = TextEditingController(text: '');
|
|
|
|
final _confidenceRatingController = TextEditingController(text: '');
|
|
|
|
final _notesController = TextEditingController(text: '');
|
|
|
|
bool _forCarbsRatio = false;
|
|
|
|
bool _forPortionSize = false;
|
|
|
|
|
2021-10-27 19:00:28 +00:00
|
|
|
bool _isSaving = false;
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
if (widget.accuracy != null) {
|
|
|
|
_valueController.text = widget.accuracy!.value;
|
|
|
|
_forCarbsRatio = widget.accuracy!.forCarbsRatio;
|
|
|
|
_forPortionSize = widget.accuracy!.forPortionSize;
|
|
|
|
_confidenceRatingController.text =
|
|
|
|
(widget.accuracy!.confidenceRating ?? '').toString();
|
|
|
|
_notesController.text = widget.accuracy!.notes ?? '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleSaveAction() async {
|
2021-10-27 19:00:28 +00:00
|
|
|
setState(() {
|
|
|
|
_isSaving = true;
|
|
|
|
});
|
2021-10-22 23:08:09 +00:00
|
|
|
if (_accuracyForm.currentState!.validate()) {
|
|
|
|
bool isNew = widget.accuracy == null;
|
|
|
|
isNew
|
|
|
|
? await Accuracy.save(
|
|
|
|
value: _valueController.text,
|
|
|
|
forCarbsRatio: _forCarbsRatio,
|
|
|
|
forPortionSize: _forPortionSize,
|
|
|
|
confidenceRating: int.tryParse(_confidenceRatingController.text),
|
|
|
|
notes: _notesController.text,
|
|
|
|
)
|
|
|
|
: await Accuracy.update(
|
|
|
|
widget.accuracy!.objectId!,
|
|
|
|
value: _valueController.text,
|
|
|
|
forCarbsRatio: _forCarbsRatio,
|
|
|
|
forPortionSize: _forPortionSize,
|
|
|
|
confidenceRating: int.tryParse(_confidenceRatingController.text),
|
|
|
|
notes: _notesController.text,
|
|
|
|
);
|
|
|
|
Navigator.pop(context, '${isNew ? 'New' : ''} Accuracy saved');
|
|
|
|
}
|
2021-10-27 19:00:28 +00:00
|
|
|
setState(() {
|
|
|
|
_isSaving = false;
|
|
|
|
});
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleCancelAction() {
|
|
|
|
bool isNew = widget.accuracy == null;
|
|
|
|
|
|
|
|
if (showConfirmationDialogOnCancel &&
|
|
|
|
(isNew &&
|
|
|
|
(_forCarbsRatio ||
|
|
|
|
_forPortionSize ||
|
|
|
|
_valueController.text != '' ||
|
|
|
|
int.tryParse(_confidenceRatingController.text) != null ||
|
|
|
|
_notesController.text != '')) ||
|
|
|
|
(!isNew &&
|
|
|
|
(_forCarbsRatio != widget.accuracy!.forCarbsRatio ||
|
|
|
|
_forPortionSize != widget.accuracy!.forPortionSize ||
|
|
|
|
widget.accuracy!.value != _valueController.text ||
|
|
|
|
int.tryParse(_confidenceRatingController.text) !=
|
|
|
|
widget.accuracy!.confidenceRating ||
|
|
|
|
(widget.accuracy!.notes ?? '') != _notesController.text))) {
|
|
|
|
Dialogs.showCancelConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
isNew: isNew,
|
|
|
|
onSave: handleSaveAction,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
bool isNew = widget.accuracy == null;
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(isNew ? 'New Accuracy' : widget.accuracy!.value),
|
|
|
|
),
|
|
|
|
drawer: const Navigation(currentLocation: AccuracyDetailScreen.routeName),
|
|
|
|
body: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: <Widget>[
|
|
|
|
StyledForm(
|
|
|
|
formState: _accuracyForm,
|
|
|
|
fields: [
|
|
|
|
TextFormField(
|
|
|
|
controller: _valueController,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Name',
|
|
|
|
),
|
|
|
|
validator: (value) {
|
|
|
|
if (value!.trim().isEmpty) {
|
|
|
|
return 'Empty name';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
StyledBooleanFormField(
|
|
|
|
value: _forCarbsRatio,
|
|
|
|
label: 'for carbs ratio',
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_forCarbsRatio = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
StyledBooleanFormField(
|
|
|
|
value: _forPortionSize,
|
|
|
|
label: 'for portion size',
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_forPortionSize = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: _confidenceRatingController,
|
|
|
|
keyboardType: TextInputType.number,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Confidence Rating',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: _notesController,
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Notes',
|
|
|
|
alignLabelWithHint: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
bottomNavigationBar: DetailBottomRow(
|
|
|
|
onCancel: handleCancelAction,
|
2021-10-27 19:00:28 +00:00
|
|
|
onSave: _isSaving ? null : handleSaveAction,
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|