diameter/lib/screens/meal/meal_list.dart

155 lines
5.0 KiB
Dart
Raw Normal View History

2021-10-22 23:08:09 +00:00
import 'package:diameter/components/dialogs.dart';
import 'package:diameter/models/meal.dart';
import 'package:diameter/models/settings.dart';
2021-10-22 23:08:09 +00:00
import 'package:diameter/navigation.dart';
import 'package:diameter/screens/meal/meal_detail.dart';
import 'package:flutter/material.dart';
class MealListScreen extends StatefulWidget {
static const String routeName = '/meals';
const MealListScreen({Key? key}) : super(key: key);
@override
_MealListScreenState createState() => _MealListScreenState();
}
class _MealListScreenState extends State<MealListScreen> {
List<Meal> _meals = [];
2021-10-22 23:08:09 +00:00
@override
void initState() {
super.initState();
reload();
}
void reload({String? message}) {
2021-10-22 23:08:09 +00:00
setState(() {
_meals = Meal.getAll();
2021-10-22 23:08:09 +00:00
});
setState(() {
if (message != null) {
var snackBar = SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(snackBar);
}
});
}
void onDelete(Meal meal) {
Meal.remove(meal.id);
reload(message: 'Meal deleted');
2021-10-22 23:08:09 +00:00
}
void handleDeleteAction(Meal meal) async {
if (Settings.get().showConfirmationDialogOnDelete) {
2021-10-22 23:08:09 +00:00
Dialogs.showConfirmationDialog(
context: context,
onConfirm: () => onDelete(meal),
message: 'Are you sure you want to delete this Meal?',
);
} else {
onDelete(meal);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Meals'), actions: <Widget>[
IconButton(onPressed: reload, icon: const Icon(Icons.refresh))
2021-10-22 23:08:09 +00:00
]),
drawer: const Navigation(currentLocation: MealListScreen.routeName),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: _meals.isNotEmpty ? ListView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: _meals.length,
itemBuilder: (context, index) {
final meal = _meals[index];
2021-12-06 22:09:40 +00:00
String portionType = meal.mealPortionType.hasValue ? ' per ${meal.mealPortionType.target!.value}' : '';
return ListTile(
2021-12-06 22:09:40 +00:00
isThreeLine: true,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
MealDetailScreen(id: meal.id),
),
).then((message) => reload(message: message));
},
title: Text(meal.value),
2021-12-06 22:09:40 +00:00
subtitle: Row(
children: [
Column(
children: [
Text(meal.mealSource.target?.value ?? ''),
Text(meal.notes ?? ''),
],
),
Expanded(
child: Column(
children: ((meal.carbsPerPortion ?? 0) > 0)
? [
Text(meal.carbsPerPortion!.toStringAsPrecision(3)),
Text(
'${Settings.nutritionMeasurementSuffix} carbs',
textScaleFactor: 0.75),
]
: [],
),
),
Expanded(
child: Column(
children: (meal.mealPortionType.hasValue)
? [
Text(meal.portionSize!.toStringAsPrecision(3)),
Text(
'${Settings.nutritionMeasurementSuffix}$portionType',
textScaleFactor: 0.75),
]
: [],
),
),
],
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () => handleDeleteAction(meal),
icon: const Icon(Icons.delete,
color: Colors.blue),
)
],
),
2021-10-22 23:08:09 +00:00
);
},
): const Center(
child: Text('You have not created any Meals yet!'),
2021-10-22 23:08:09 +00:00
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MealDetailScreen(),
),
).then((message) => reload(message: message));
2021-10-22 23:08:09 +00:00
},
child: const Icon(Icons.add),
),
);
}
}