✨ Matching alert
This commit is contained in:
@ -1,29 +1,142 @@
|
||||
import 'package:dietary_guard/controllers/alert.dart';
|
||||
import 'package:dietary_guard/models/alert_configuration.dart';
|
||||
import 'package:dietary_guard/models/food_data.dart';
|
||||
import 'package:dietary_guard/widgets/alert_detect_result.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class FoodDetailsScreen extends StatelessWidget {
|
||||
class FoodDetailsScreen extends StatefulWidget {
|
||||
final FoodData item;
|
||||
|
||||
const FoodDetailsScreen({super.key, required this.item});
|
||||
|
||||
@override
|
||||
State<FoodDetailsScreen> createState() => _FoodDetailsScreenState();
|
||||
}
|
||||
|
||||
class _FoodDetailsScreenState extends State<FoodDetailsScreen> {
|
||||
final List<AlertDetectResult> _alertDetectResult = List.empty(growable: true);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_detectAlert();
|
||||
}
|
||||
|
||||
void _detectAlert() {
|
||||
final AlertController alert = Get.find();
|
||||
if (alert.configuration.isEmpty) return;
|
||||
|
||||
for (final item in alert.configuration) {
|
||||
for (final nutrient in widget.item.foodNutrients) {
|
||||
if (item.nutrientId != nutrient.nutrientId) continue;
|
||||
bool isOutOfRange = false;
|
||||
double? difference;
|
||||
bool isUndetected = false;
|
||||
if (nutrient.value != null) {
|
||||
final value = nutrient.value ?? 0;
|
||||
if (value > item.maxValue) {
|
||||
difference = value - item.maxValue;
|
||||
isOutOfRange = true;
|
||||
} else if (value < item.minValue) {
|
||||
difference = value - item.minValue;
|
||||
isOutOfRange = true;
|
||||
}
|
||||
} else {
|
||||
isUndetected = true;
|
||||
}
|
||||
|
||||
_alertDetectResult.add(AlertDetectResult(
|
||||
config: item,
|
||||
name: nutrient.nutrientName,
|
||||
unitName: unitNameValues.reverse[nutrient.unitName],
|
||||
current: nutrient.value,
|
||||
difference: difference,
|
||||
isOutOfRange: isOutOfRange,
|
||||
isUndetected: isUndetected,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildShowAlertResultButton() {
|
||||
return IconButton(
|
||||
icon: const Icon(Icons.assignment),
|
||||
onPressed: () {
|
||||
showModalBottomSheet(
|
||||
useRootNavigator: true,
|
||||
isScrollControlled: true,
|
||||
context: context,
|
||||
builder: (context) => AlertDetectResultDialog(
|
||||
result: _alertDetectResult,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(item.description),
|
||||
title: Text(widget.item.description),
|
||||
),
|
||||
body: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (_alertDetectResult.isEmpty)
|
||||
MaterialBanner(
|
||||
padding: const EdgeInsets.only(left: 24, right: 8),
|
||||
content: Text('alertEmpty'.tr),
|
||||
actions: const [SizedBox()],
|
||||
dividerColor: Colors.transparent,
|
||||
)
|
||||
else if (_alertDetectResult.any((x) => x.isOutOfRange))
|
||||
MaterialBanner(
|
||||
leading: const Icon(Icons.close),
|
||||
backgroundColor: Colors.red,
|
||||
padding: const EdgeInsets.only(left: 24, right: 8),
|
||||
content: Text('alertOutOfRange'.trParams({
|
||||
'count': _alertDetectResult
|
||||
.where((x) => x.isOutOfRange)
|
||||
.length
|
||||
.toString()
|
||||
})),
|
||||
actions: [_buildShowAlertResultButton()],
|
||||
dividerColor: Colors.transparent,
|
||||
)
|
||||
else if (_alertDetectResult.any((x) => x.isUndetected))
|
||||
MaterialBanner(
|
||||
leading: const Icon(Icons.question_mark),
|
||||
backgroundColor: Colors.grey,
|
||||
padding: const EdgeInsets.only(left: 24, right: 8),
|
||||
content: Text('alertUnclear'.trParams({
|
||||
'count': _alertDetectResult
|
||||
.where((x) => x.isUndetected)
|
||||
.length
|
||||
.toString()
|
||||
})),
|
||||
actions: [_buildShowAlertResultButton()],
|
||||
dividerColor: Colors.transparent,
|
||||
)
|
||||
else
|
||||
MaterialBanner(
|
||||
leading: const Icon(Icons.check),
|
||||
backgroundColor: Colors.green,
|
||||
padding: const EdgeInsets.only(left: 24, right: 8),
|
||||
content: Text('alertSafe'.tr),
|
||||
actions: [_buildShowAlertResultButton()],
|
||||
dividerColor: Colors.transparent,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text('nutrients'.tr).paddingOnly(left: 24, right: 24, bottom: 8),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: item.foodNutrients.length,
|
||||
itemCount: widget.item.foodNutrients.length,
|
||||
itemBuilder: (context, idx) {
|
||||
final entry = item.foodNutrients[idx];
|
||||
final entry = widget.item.foodNutrients[idx];
|
||||
final unitName = unitNameValues.reverse[entry.unitName];
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
@ -35,13 +148,15 @@ class FoodDetailsScreen extends StatelessWidget {
|
||||
Badge(label: Text('#${entry.nutrientId}'))
|
||||
],
|
||||
),
|
||||
subtitle: Text('${entry.nutrientNumber} ${unitName}'),
|
||||
subtitle: Text(
|
||||
'${entry.value?.toString() ?? '-'} $unitName',
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
)
|
||||
],
|
||||
).paddingSymmetric(vertical: 24),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user