114 lines
3.6 KiB
Dart
114 lines
3.6 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/navigation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:diameter/models/meal_category.dart';
|
|
|
|
class MealCategoryDetailScreen extends StatefulWidget {
|
|
static const String routeName = '/meal-category';
|
|
final MealCategory? mealCategory;
|
|
|
|
const MealCategoryDetailScreen({Key? key, this.mealCategory})
|
|
: super(key: key);
|
|
|
|
@override
|
|
_MealCategoryDetailScreenState createState() =>
|
|
_MealCategoryDetailScreenState();
|
|
}
|
|
|
|
class _MealCategoryDetailScreenState extends State<MealCategoryDetailScreen> {
|
|
final GlobalKey<FormState> _mealCategoryForm = GlobalKey<FormState>();
|
|
final _valueController = TextEditingController(text: '');
|
|
final _notesController = TextEditingController(text: '');
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.mealCategory != null) {
|
|
_valueController.text = widget.mealCategory!.value;
|
|
_notesController.text = widget.mealCategory!.notes ?? '';
|
|
}
|
|
}
|
|
|
|
void handleSaveAction() async {
|
|
if (_mealCategoryForm.currentState!.validate()) {
|
|
bool isNew = widget.mealCategory == null;
|
|
isNew
|
|
? await MealCategory.save(
|
|
value: _valueController.text, notes: _notesController.text)
|
|
: await MealCategory.update(widget.mealCategory!.objectId!,
|
|
value: _valueController.text, notes: _notesController.text);
|
|
Navigator.pop(context, '${isNew ? 'New' : ''} Meal Category saved');
|
|
}
|
|
}
|
|
|
|
void handleCancelAction() {
|
|
bool isNew = widget.mealCategory == null;
|
|
|
|
if (showConfirmationDialogOnCancel &&
|
|
(isNew &&
|
|
(_valueController.text != '' || _notesController.text != '')) ||
|
|
(!isNew &&
|
|
(widget.mealCategory!.value != _valueController.text ||
|
|
(widget.mealCategory!.notes ?? '') != _notesController.text))) {
|
|
Dialogs.showCancelConfirmationDialog(
|
|
context: context,
|
|
isNew: isNew,
|
|
onSave: handleSaveAction,
|
|
);
|
|
} else {
|
|
Navigator.pop(context);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
bool isNew = widget.mealCategory == null;
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(isNew ? 'New Meal Category' : widget.mealCategory!.value),
|
|
),
|
|
drawer:
|
|
const Navigation(currentLocation: MealCategoryDetailScreen.routeName),
|
|
body: SingleChildScrollView(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
StyledForm(
|
|
formState: _mealCategoryForm,
|
|
fields: [
|
|
TextFormField(
|
|
controller: _valueController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Name',
|
|
),
|
|
validator: (value) {
|
|
if (value!.trim().isEmpty) {
|
|
return 'Empty name';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
TextFormField(
|
|
controller: _notesController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Notes',
|
|
alignLabelWithHint: true,
|
|
),
|
|
keyboardType: TextInputType.multiline,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
bottomNavigationBar: DetailBottomRow(
|
|
onCancel: handleCancelAction,
|
|
onSave: handleSaveAction,
|
|
),
|
|
);
|
|
}
|
|
}
|