fooder-app/lib/widgets/macro.dart

165 lines
3.4 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/material.dart';
2024-04-04 19:03:41 +02:00
import 'dart:core';
class MacroHeaderWidget extends StatelessWidget {
static const double padY = 4;
static const double padX = 8;
final bool? fiber;
final bool? calories;
final Alignment alignment;
const MacroHeaderWidget({
super.key,
this.fiber = false,
this.calories = false,
this.alignment = Alignment.centerLeft,
});
@override
Widget build(BuildContext context) {
var elements = <String>[
"C(g)",
"F(g)",
"P(g)",
];
if (fiber == true) {
elements.add(
"f(g)",
);
}
if (calories == true) {
elements.add(
"kcal",
);
}
var children = <Widget>[];
if (alignment == Alignment.centerRight) {
children.add(const Spacer());
}
for (var element in elements) {
children.add(
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 2,
),
child: SizedBox(
width: 55,
child: Text(
element,
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
),
textAlign: TextAlign.center,
),
),
),
);
}
if (alignment == Alignment.centerLeft) {
children.add(const Spacer());
}
return Padding(
padding: const EdgeInsets.symmetric(
vertical: padY,
horizontal: padX,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: children,
),
);
}
}
class MacroEntryWidget extends StatelessWidget {
static const double padY = 4;
static const double padX = 8;
final double protein;
final double carb;
final double fat;
2024-04-04 19:03:41 +02:00
final double? fiber;
final double? calories;
final Alignment alignment;
2024-04-04 19:03:41 +02:00
const MacroEntryWidget({
2024-03-30 14:07:10 +01:00
super.key,
required this.protein,
required this.carb,
required this.fat,
2024-04-04 19:03:41 +02:00
this.fiber,
this.calories,
this.alignment = Alignment.centerLeft,
2024-03-30 14:07:10 +01:00
});
@override
Widget build(BuildContext context) {
2024-04-04 19:03:41 +02:00
var elements = <String>[
(carb.toStringAsFixed(1)),
(fat.toStringAsFixed(1)),
(protein.toStringAsFixed(1)),
];
2023-07-30 20:44:50 +02:00
if (fiber != null) {
elements.add(
2024-04-04 19:03:41 +02:00
fiber!.toStringAsFixed(1),
2023-07-30 20:44:50 +02:00
);
}
if (calories != null) {
elements.add(
2024-04-04 19:03:41 +02:00
calories!.toStringAsFixed(0),
2023-07-30 20:44:50 +02:00
);
}
2024-04-04 19:03:41 +02:00
var children = <Widget>[];
if (alignment == Alignment.centerRight) {
children.add(const Spacer());
}
2024-04-04 19:03:41 +02:00
for (var element in elements) {
children.add(
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 2,
),
child: SizedBox(
width: 55,
child: Text(
element,
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
),
textAlign: TextAlign.center,
),
),
2023-07-30 20:44:50 +02:00
),
);
2023-07-30 14:40:45 +02:00
}
2024-04-04 19:03:41 +02:00
if (alignment == Alignment.centerLeft) {
children.add(const Spacer());
}
return Padding(
2024-04-04 19:03:41 +02:00
padding: const EdgeInsets.symmetric(
vertical: padY,
horizontal: padX,
),
child: Row(
2024-04-04 19:03:41 +02:00
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: children,
),
);
}
}