fooder-app/lib/widgets/meal.dart

149 lines
4.3 KiB
Dart
Raw Normal View History

2023-07-29 19:21:02 +02:00
import 'package:flutter/material.dart';
2023-07-30 15:31:36 +02:00
import 'package:fooder/models/meal.dart';
import 'package:fooder/widgets/entry.dart';
2024-03-29 16:47:25 +01:00
import 'package:fooder/widgets/macroEntry.dart';
2023-07-30 15:31:36 +02:00
import 'package:fooder/screens/edit_entry.dart';
2023-10-27 20:06:41 +02:00
import 'package:fooder/screens/meal.dart';
2023-07-30 15:31:36 +02:00
import 'package:fooder/client.dart';
2023-07-29 19:21:02 +02:00
import 'dart:core';
2024-03-29 16:47:25 +01:00
class MealHeader extends StatelessWidget {
final Meal meal;
2024-03-30 14:07:10 +01:00
const MealHeader({super.key, required this.meal});
2024-03-29 16:47:25 +01:00
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8),
child: Text(
meal.name,
style: Theme.of(context).textTheme.headlineSmall!.copyWith(
2024-03-30 14:07:10 +01:00
color: Theme.of(context).colorScheme.onPrimary,
fontWeight: FontWeight.bold,
),
2024-03-29 16:47:25 +01:00
),
),
],
);
}
}
2023-07-29 19:21:02 +02:00
class MealWidget extends StatelessWidget {
2024-03-30 14:07:10 +01:00
static const maxWidth = 920.0;
2024-03-29 16:47:25 +01:00
2023-07-29 19:21:02 +02:00
final Meal meal;
final ApiClient apiClient;
final Function() refreshParent;
2024-03-29 16:47:25 +01:00
final bool initiallyExpanded;
2023-07-29 19:21:02 +02:00
2024-03-30 14:07:10 +01:00
const MealWidget({
super.key,
required this.meal,
required this.apiClient,
required this.refreshParent,
required this.initiallyExpanded,
});
2024-03-29 16:47:25 +01:00
Future<void> _editMeal(context) async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MealScreen(
apiClient: apiClient,
meal: meal,
refresh: refreshParent,
),
),
).then((_) => refreshParent());
}
Future<void> _editEntry(context, entry) async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditEntryScreen(
apiClient: apiClient,
entry: entry,
),
),
).then((_) => refreshParent());
}
2023-07-29 19:21:02 +02:00
@override
Widget build(BuildContext context) {
2024-03-29 16:47:25 +01:00
var theme = Theme.of(context);
var colorScheme = theme.colorScheme;
2024-03-30 14:07:10 +01:00
var widthAvail = MediaQuery.of(context).size.width;
var width = widthAvail > maxWidth ? maxWidth : widthAvail;
2024-03-29 16:47:25 +01:00
return Center(
2023-07-29 19:21:02 +02:00
child: Padding(
2024-03-29 16:47:25 +01:00
padding: const EdgeInsets.all(8),
child: Card(
elevation: 4,
clipBehavior: Clip.antiAlias,
shadowColor: colorScheme.primary.withOpacity(1.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: SizedBox(
width: width,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
colorScheme.primary.withOpacity(0.6),
colorScheme.secondary.withOpacity(0.5),
],
),
2023-08-28 14:45:32 +02:00
),
2024-03-29 16:47:25 +01:00
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onLongPress: () => _editMeal(context),
child: ExpansionTile(
iconColor: colorScheme.onPrimary,
collapsedIconColor: colorScheme.onPrimary,
initiallyExpanded: initiallyExpanded,
title: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: <Widget>[
MealHeader(meal: meal),
2024-03-30 14:07:10 +01:00
const MacroHeaderWidget(
2024-03-29 16:47:25 +01:00
calories: true,
),
MacroEntryWidget(
protein: meal.protein,
carb: meal.carb,
fat: meal.fat,
calories: meal.calories,
),
],
2023-08-28 14:45:32 +02:00
),
2024-03-29 16:47:25 +01:00
),
children: <Widget>[
for (var (i, entry) in meal.entries.indexed)
ListTile(
title: EntryWidget(
entry: entry,
),
2024-03-30 14:07:10 +01:00
tileColor: i % 2 == 0
? colorScheme.secondary.withOpacity(0.1)
: Colors.transparent,
2024-03-29 16:47:25 +01:00
onTap: () => _editEntry(context, entry),
)
],
),
2023-08-28 14:45:32 +02:00
),
2024-03-29 16:47:25 +01:00
),
2023-08-28 14:45:32 +02:00
),
2023-07-29 19:21:02 +02:00
),
),
2024-03-29 16:47:25 +01:00
);
2023-07-29 19:21:02 +02:00
}
}