2021-10-22 23:08:09 +00:00
|
|
|
import 'package:diameter/components/dialogs.dart';
|
|
|
|
import 'package:diameter/config.dart';
|
|
|
|
import 'package:diameter/models/log_entry.dart';
|
|
|
|
import 'package:diameter/models/log_meal.dart';
|
|
|
|
import 'package:diameter/screens/log/log_meal_detail.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:diameter/components/progress_indicator.dart';
|
|
|
|
|
|
|
|
class LogMealListScreen extends StatefulWidget {
|
|
|
|
final LogEntry? logEntry;
|
|
|
|
|
|
|
|
const LogMealListScreen({Key? key, this.logEntry}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LogMealListScreenState createState() => _LogMealListScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LogMealListScreenState extends State<LogMealListScreen> {
|
|
|
|
void refresh({String? message}) {
|
|
|
|
if (widget.logEntry != null) {
|
|
|
|
setState(() {
|
|
|
|
widget.logEntry!.meals = LogMeal.fetchAllForLogEntry(widget.logEntry!);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
if (message != null) {
|
|
|
|
var snackBar = SnackBar(
|
|
|
|
content: Text(message),
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
);
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
..removeCurrentSnackBar()
|
|
|
|
..showSnackBar(snackBar);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleEditAction(LogMeal meal) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => LogMealDetailScreen(
|
|
|
|
logEntry: widget.logEntry!,
|
|
|
|
logMeal: meal,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).then((message) => refresh(message: message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDelete(LogMeal meal) {
|
|
|
|
meal.delete().then((_) => refresh(message: 'Meal deleted'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDeleteAction(LogMeal meal) async {
|
|
|
|
if (showConfirmationDialogOnDelete) {
|
|
|
|
Dialogs.showConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
onConfirm: () => onDelete(meal),
|
|
|
|
message: 'Are you sure you want to delete this Meal?',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
onDelete(meal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SingleChildScrollView(
|
|
|
|
padding: const EdgeInsets.only(top: 10.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
FutureBuilder<List<LogMeal>>(
|
|
|
|
future: widget.logEntry!.meals,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return ViewWithProgressIndicator(
|
|
|
|
snapshot: snapshot,
|
|
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
|
|
? const Padding(
|
|
|
|
padding: EdgeInsets.all(10.0),
|
|
|
|
child: Text('No Meals for this Log Entry'),
|
|
|
|
)
|
2021-10-27 19:00:28 +00:00
|
|
|
: ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
itemCount: snapshot.data != null ? snapshot.data!.length : 0,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final meal = snapshot.data![index];
|
|
|
|
return ListTile(
|
|
|
|
onTap: () => handleEditAction(meal),
|
|
|
|
title: Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(meal.value)),
|
|
|
|
Expanded(
|
|
|
|
child: Text(meal.carbsPerPortion != null ? '${meal.carbsPerPortion} g carbs' : '')),
|
|
|
|
Expanded(child: Text(meal.bolus != null ? '${meal.bolus} U' : ''))
|
|
|
|
],
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
2021-10-27 19:00:28 +00:00
|
|
|
trailing: IconButton(
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.delete,
|
|
|
|
color: Colors.blue,
|
|
|
|
),
|
|
|
|
onPressed: () => handleDeleteAction(meal),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2021-10-22 23:08:09 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|