211 lines
8.0 KiB
Dart
211 lines
8.0 KiB
Dart
|
import 'package:diameter/components/detail.dart';
|
||
|
import 'package:diameter/components/dialogs.dart';
|
||
|
import 'package:diameter/components/forms.dart';
|
||
|
import 'package:diameter/config.dart';
|
||
|
import 'package:diameter/models/accuracy.dart';
|
||
|
import 'package:diameter/models/meal_category.dart';
|
||
|
import 'package:diameter/models/meal_portion_type.dart';
|
||
|
import 'package:diameter/models/meal_source.dart';
|
||
|
import 'package:diameter/navigation.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
class MealSourceDetailScreen extends StatefulWidget {
|
||
|
static const String routeName = '/meal-source';
|
||
|
|
||
|
final MealSource? mealSource;
|
||
|
|
||
|
const MealSourceDetailScreen({Key? key, this.mealSource}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_MealSourceDetailScreenState createState() => _MealSourceDetailScreenState();
|
||
|
}
|
||
|
|
||
|
class _MealSourceDetailScreenState extends State<MealSourceDetailScreen> {
|
||
|
late Future<List<Accuracy>> _portionSizeAccuracies;
|
||
|
late Future<List<Accuracy>> _carbsRatioAccuracies;
|
||
|
late Future<List<MealCategory>> _mealCategories;
|
||
|
late Future<List<MealPortionType>> _mealPortionTypes;
|
||
|
|
||
|
final GlobalKey<FormState> _mealSourceForm = GlobalKey<FormState>();
|
||
|
final _valueController = TextEditingController(text: '');
|
||
|
final _notesController = TextEditingController(text: '');
|
||
|
String? _defaultCarbsRatioAccuracy;
|
||
|
String? _defaultPortionSizeAccuracy;
|
||
|
String? _defaultMealCategory;
|
||
|
String? _defaultMealPortionType;
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
|
||
|
if (widget.mealSource != null) {
|
||
|
_valueController.text = widget.mealSource!.value;
|
||
|
_notesController.text = widget.mealSource!.notes ?? '';
|
||
|
|
||
|
_defaultCarbsRatioAccuracy = widget.mealSource!.defaultCarbsRatioAccuracy;
|
||
|
_defaultPortionSizeAccuracy =
|
||
|
widget.mealSource!.defaultPortionSizeAccuracy;
|
||
|
_defaultMealCategory = widget.mealSource!.defaultMealCategory;
|
||
|
_defaultMealPortionType = widget.mealSource!.defaultMealPortionType;
|
||
|
}
|
||
|
|
||
|
_portionSizeAccuracies = Accuracy.fetchAllForPortionSize();
|
||
|
_carbsRatioAccuracies = Accuracy.fetchAllForCarbsRatio();
|
||
|
_mealCategories = MealCategory.fetchAll();
|
||
|
_mealPortionTypes = MealPortionType.fetchAll();
|
||
|
}
|
||
|
|
||
|
void handleSaveAction() async {
|
||
|
bool isNew = widget.mealSource == null;
|
||
|
if (_mealSourceForm.currentState!.validate()) {
|
||
|
isNew
|
||
|
? await MealSource.save(
|
||
|
value: _valueController.text,
|
||
|
defaultCarbsRatioAccuracy: _defaultCarbsRatioAccuracy,
|
||
|
defaultPortionSizeAccuracy: _defaultPortionSizeAccuracy,
|
||
|
defaultMealCategory: _defaultMealCategory,
|
||
|
defaultMealPortionType: _defaultMealPortionType,
|
||
|
notes: _notesController.text,
|
||
|
)
|
||
|
: await MealSource.update(
|
||
|
widget.mealSource!.objectId!,
|
||
|
value: _valueController.text,
|
||
|
defaultCarbsRatioAccuracy: _defaultCarbsRatioAccuracy,
|
||
|
defaultPortionSizeAccuracy: _defaultPortionSizeAccuracy,
|
||
|
defaultMealCategory: _defaultMealCategory,
|
||
|
defaultMealPortionType: _defaultMealPortionType,
|
||
|
notes: _notesController.text,
|
||
|
);
|
||
|
Navigator.pop(context, '${isNew ? 'New' : ''} Meal Source saved');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void handleCancelAction() {
|
||
|
bool isNew = widget.mealSource == null;
|
||
|
if (showConfirmationDialogOnCancel &&
|
||
|
((isNew &&
|
||
|
(_valueController.text != '' ||
|
||
|
_defaultCarbsRatioAccuracy != null ||
|
||
|
_defaultPortionSizeAccuracy != null ||
|
||
|
_defaultMealCategory != null ||
|
||
|
_defaultMealPortionType != null ||
|
||
|
_notesController.text != '')) ||
|
||
|
(!isNew &&
|
||
|
(_valueController.text != widget.mealSource!.value ||
|
||
|
_defaultCarbsRatioAccuracy !=
|
||
|
widget.mealSource!.defaultCarbsRatioAccuracy ||
|
||
|
_defaultPortionSizeAccuracy !=
|
||
|
widget.mealSource!.defaultPortionSizeAccuracy ||
|
||
|
_defaultMealCategory !=
|
||
|
widget.mealSource!.defaultMealCategory ||
|
||
|
_defaultMealPortionType !=
|
||
|
widget.mealSource!.defaultMealPortionType ||
|
||
|
_notesController.text !=
|
||
|
(widget.mealSource!.notes ?? ''))))) {
|
||
|
Dialogs.showCancelConfirmationDialog(
|
||
|
context: context,
|
||
|
isNew: isNew,
|
||
|
onSave: handleSaveAction,
|
||
|
);
|
||
|
} else {
|
||
|
Navigator.pop(context);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
bool isNew = widget.mealSource == null;
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: Text(isNew ? 'New Meal Source' : widget.mealSource!.value),
|
||
|
),
|
||
|
drawer:
|
||
|
const Navigation(currentLocation: MealSourceDetailScreen.routeName),
|
||
|
body: SingleChildScrollView(
|
||
|
child: Column(
|
||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
|
children: <Widget>[
|
||
|
StyledForm(
|
||
|
formState: _mealSourceForm,
|
||
|
fields: [
|
||
|
TextFormField(
|
||
|
controller: _valueController,
|
||
|
decoration: const InputDecoration(
|
||
|
labelText: 'Name',
|
||
|
),
|
||
|
validator: (value) {
|
||
|
if (value!.trim().isEmpty) {
|
||
|
return 'Empty name';
|
||
|
}
|
||
|
return null;
|
||
|
},
|
||
|
),
|
||
|
StyledFutureDropdownButton<Accuracy>(
|
||
|
selectedItem: _defaultCarbsRatioAccuracy,
|
||
|
label: 'Default Carbs Ratio Accuracy',
|
||
|
items: _carbsRatioAccuracies,
|
||
|
getItemValue: (item) => item.objectId,
|
||
|
renderItem: (item) => Text(item.value),
|
||
|
onChanged: (value) {
|
||
|
setState(() {
|
||
|
_defaultCarbsRatioAccuracy = value;
|
||
|
});
|
||
|
},
|
||
|
),
|
||
|
StyledFutureDropdownButton<Accuracy>(
|
||
|
selectedItem: _defaultPortionSizeAccuracy,
|
||
|
label: 'Default Portion Size Accuracy',
|
||
|
items: _portionSizeAccuracies,
|
||
|
getItemValue: (item) => item.objectId,
|
||
|
renderItem: (item) => Text(item.value),
|
||
|
onChanged: (value) {
|
||
|
setState(() {
|
||
|
_defaultPortionSizeAccuracy = value;
|
||
|
});
|
||
|
},
|
||
|
),
|
||
|
StyledFutureDropdownButton<MealCategory>(
|
||
|
selectedItem: _defaultMealCategory,
|
||
|
label: 'Default Meal Category',
|
||
|
items: _mealCategories,
|
||
|
getItemValue: (item) => item.objectId,
|
||
|
renderItem: (item) => Text(item.value),
|
||
|
onChanged: (value) {
|
||
|
setState(() {
|
||
|
_defaultMealCategory = value;
|
||
|
});
|
||
|
},
|
||
|
),
|
||
|
StyledFutureDropdownButton<MealPortionType>(
|
||
|
selectedItem: _defaultMealPortionType,
|
||
|
label: 'Default Meal Portion Type',
|
||
|
items: _mealPortionTypes,
|
||
|
getItemValue: (item) => item.objectId,
|
||
|
renderItem: (item) => Text(item.value),
|
||
|
onChanged: (value) {
|
||
|
setState(() {
|
||
|
_defaultMealPortionType = value;
|
||
|
});
|
||
|
},
|
||
|
),
|
||
|
TextFormField(
|
||
|
controller: _notesController,
|
||
|
decoration: const InputDecoration(
|
||
|
labelText: 'Notes',
|
||
|
alignLabelWithHint: true,
|
||
|
),
|
||
|
keyboardType: TextInputType.multiline,
|
||
|
)
|
||
|
],
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
bottomNavigationBar: DetailBottomRow(
|
||
|
onCancel: handleCancelAction,
|
||
|
onSave: handleSaveAction,
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|