Compare commits
2 Commits
07f8112c7a
...
09e904f52f
Author | SHA1 | Date | |
---|---|---|---|
09e904f52f | |||
22f3027d6f |
38
lib/controllers/alert.dart
Normal file
38
lib/controllers/alert.dart
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:dietary_guard/models/alert_configuration.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
class AlertController extends GetxController {
|
||||||
|
List<AlertConfiguration> configuration = List.empty();
|
||||||
|
|
||||||
|
late final SharedPreferences _prefs;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onInit() async {
|
||||||
|
_prefs = await SharedPreferences.getInstance();
|
||||||
|
loadAlertConfiguration();
|
||||||
|
super.onInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadAlertConfiguration() {
|
||||||
|
final raw = _prefs.getString("alert_configuration");
|
||||||
|
if (raw == null) return;
|
||||||
|
configuration = List<AlertConfiguration>.from(jsonDecode(raw).map(
|
||||||
|
(x) => AlertConfiguration.fromJson(x),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> setAlertConfiguration(List<AlertConfiguration> value) async {
|
||||||
|
await _prefs.setString(
|
||||||
|
"alert_configuration",
|
||||||
|
jsonEncode(value.map((x) => x.toJson()).toList()),
|
||||||
|
);
|
||||||
|
loadAlertConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearAlertConfiguration() {
|
||||||
|
_prefs.remove("alert_configuration");
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
import 'package:dietary_guard/controllers/alert.dart';
|
||||||
import 'package:dietary_guard/controllers/food_data.dart';
|
import 'package:dietary_guard/controllers/food_data.dart';
|
||||||
import 'package:dietary_guard/screens/query.dart';
|
import 'package:dietary_guard/screens/query.dart';
|
||||||
import 'package:dietary_guard/screens/settings.dart';
|
import 'package:dietary_guard/screens/settings.dart';
|
||||||
@ -63,5 +64,6 @@ class MyApp extends StatelessWidget {
|
|||||||
|
|
||||||
void _initializeProviders(BuildContext context) async {
|
void _initializeProviders(BuildContext context) async {
|
||||||
Get.put(FoodDataController());
|
Get.put(FoodDataController());
|
||||||
|
Get.put(AlertController());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
44
lib/models/alert_configuration.dart
Normal file
44
lib/models/alert_configuration.dart
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
class AlertConfiguration {
|
||||||
|
int nutrientId;
|
||||||
|
double maxValue;
|
||||||
|
double minValue;
|
||||||
|
|
||||||
|
AlertConfiguration({
|
||||||
|
required this.nutrientId,
|
||||||
|
required this.maxValue,
|
||||||
|
required this.minValue,
|
||||||
|
});
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'nutrient_id': nutrientId,
|
||||||
|
'min_value': minValue,
|
||||||
|
'max_value': maxValue,
|
||||||
|
};
|
||||||
|
|
||||||
|
factory AlertConfiguration.fromJson(Map<String, dynamic> json) =>
|
||||||
|
AlertConfiguration(
|
||||||
|
nutrientId: json['nutrient_id'],
|
||||||
|
minValue: json['min_value'],
|
||||||
|
maxValue: json['max_value'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class AlertDetectResult {
|
||||||
|
AlertConfiguration config;
|
||||||
|
String name;
|
||||||
|
String? unitName;
|
||||||
|
double? current;
|
||||||
|
double? difference;
|
||||||
|
bool isOutOfRange;
|
||||||
|
bool isUndetected;
|
||||||
|
|
||||||
|
AlertDetectResult({
|
||||||
|
required this.config,
|
||||||
|
required this.name,
|
||||||
|
required this.unitName,
|
||||||
|
required this.current,
|
||||||
|
required this.difference,
|
||||||
|
required this.isOutOfRange,
|
||||||
|
required this.isUndetected,
|
||||||
|
});
|
||||||
|
}
|
@ -98,7 +98,7 @@ class Nutrients {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class FoodSearchCriteria {
|
class FoodSearchCriteria {
|
||||||
List<Type> dataType;
|
List<Type?> dataType;
|
||||||
String query;
|
String query;
|
||||||
String generalSearchInput;
|
String generalSearchInput;
|
||||||
int pageNumber;
|
int pageNumber;
|
||||||
@ -107,7 +107,7 @@ class FoodSearchCriteria {
|
|||||||
int numberOfResultsPerPage;
|
int numberOfResultsPerPage;
|
||||||
int pageSize;
|
int pageSize;
|
||||||
bool requireAllWords;
|
bool requireAllWords;
|
||||||
List<Type> foodTypes;
|
List<Type?> foodTypes;
|
||||||
|
|
||||||
FoodSearchCriteria({
|
FoodSearchCriteria({
|
||||||
required this.dataType,
|
required this.dataType,
|
||||||
@ -124,8 +124,8 @@ class FoodSearchCriteria {
|
|||||||
|
|
||||||
factory FoodSearchCriteria.fromJson(Map<String, dynamic> json) =>
|
factory FoodSearchCriteria.fromJson(Map<String, dynamic> json) =>
|
||||||
FoodSearchCriteria(
|
FoodSearchCriteria(
|
||||||
dataType:
|
dataType: List<Type>.from(
|
||||||
List<Type>.from(json["dataType"].map((x) => typeValues.map[x]!)),
|
json["dataType"]?.map((x) => typeValues.map[x]) ?? List.empty()),
|
||||||
query: json["query"],
|
query: json["query"],
|
||||||
generalSearchInput: json["generalSearchInput"],
|
generalSearchInput: json["generalSearchInput"],
|
||||||
pageNumber: json["pageNumber"],
|
pageNumber: json["pageNumber"],
|
||||||
@ -134,8 +134,8 @@ class FoodSearchCriteria {
|
|||||||
numberOfResultsPerPage: json["numberOfResultsPerPage"],
|
numberOfResultsPerPage: json["numberOfResultsPerPage"],
|
||||||
pageSize: json["pageSize"],
|
pageSize: json["pageSize"],
|
||||||
requireAllWords: json["requireAllWords"],
|
requireAllWords: json["requireAllWords"],
|
||||||
foodTypes:
|
foodTypes: List<Type>.from(
|
||||||
List<Type>.from(json["foodTypes"].map((x) => typeValues.map[x]!)),
|
json["foodTypes"]?.map((x) => typeValues.map[x]) ?? List.empty()),
|
||||||
);
|
);
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {
|
Map<String, dynamic> toJson() => {
|
||||||
@ -162,10 +162,10 @@ final typeValues =
|
|||||||
class FoodData {
|
class FoodData {
|
||||||
int fdcId;
|
int fdcId;
|
||||||
String description;
|
String description;
|
||||||
String commonNames;
|
String? commonNames;
|
||||||
String additionalDescriptions;
|
String? additionalDescriptions;
|
||||||
Type dataType;
|
Type? dataType;
|
||||||
int ndbNumber;
|
int? ndbNumber;
|
||||||
DateTime publishedDate;
|
DateTime publishedDate;
|
||||||
FoodCategory? foodCategory;
|
FoodCategory? foodCategory;
|
||||||
DateTime? mostRecentAcquisitionDate;
|
DateTime? mostRecentAcquisitionDate;
|
||||||
@ -205,7 +205,7 @@ class FoodData {
|
|||||||
description: json["description"],
|
description: json["description"],
|
||||||
commonNames: json["commonNames"],
|
commonNames: json["commonNames"],
|
||||||
additionalDescriptions: json["additionalDescriptions"],
|
additionalDescriptions: json["additionalDescriptions"],
|
||||||
dataType: typeValues.map[json["dataType"]]!,
|
dataType: typeValues.map[json["dataType"]],
|
||||||
ndbNumber: json["ndbNumber"],
|
ndbNumber: json["ndbNumber"],
|
||||||
publishedDate: DateTime.parse(json["publishedDate"]),
|
publishedDate: DateTime.parse(json["publishedDate"]),
|
||||||
foodCategory: foodCategoryValues.map[json["foodCategory"]],
|
foodCategory: foodCategoryValues.map[json["foodCategory"]],
|
||||||
@ -264,7 +264,7 @@ class FoodNutrient {
|
|||||||
int nutrientId;
|
int nutrientId;
|
||||||
String nutrientName;
|
String nutrientName;
|
||||||
String nutrientNumber;
|
String nutrientNumber;
|
||||||
UnitName unitName;
|
UnitName? unitName;
|
||||||
DerivationCode? derivationCode;
|
DerivationCode? derivationCode;
|
||||||
String? derivationDescription;
|
String? derivationDescription;
|
||||||
int? derivationId;
|
int? derivationId;
|
||||||
@ -305,15 +305,15 @@ class FoodNutrient {
|
|||||||
nutrientId: json["nutrientId"],
|
nutrientId: json["nutrientId"],
|
||||||
nutrientName: json["nutrientName"],
|
nutrientName: json["nutrientName"],
|
||||||
nutrientNumber: json["nutrientNumber"],
|
nutrientNumber: json["nutrientNumber"],
|
||||||
unitName: unitNameValues.map[json["unitName"]]!,
|
unitName: unitNameValues.map[json["unitName"]],
|
||||||
derivationCode: derivationCodeValues.map[json["derivationCode"]]!,
|
derivationCode: derivationCodeValues.map[json["derivationCode"]],
|
||||||
derivationDescription: json["derivationDescription"],
|
derivationDescription: json["derivationDescription"],
|
||||||
derivationId: json["derivationId"],
|
derivationId: json["derivationId"],
|
||||||
value: json["value"]?.toDouble(),
|
value: json["value"]?.toDouble(),
|
||||||
foodNutrientSourceId: json["foodNutrientSourceId"],
|
foodNutrientSourceId: json["foodNutrientSourceId"],
|
||||||
foodNutrientSourceCode: json["foodNutrientSourceCode"],
|
foodNutrientSourceCode: json["foodNutrientSourceCode"],
|
||||||
foodNutrientSourceDescription: foodNutrientSourceDescriptionValues
|
foodNutrientSourceDescription: foodNutrientSourceDescriptionValues
|
||||||
.map[json["foodNutrientSourceDescription"]]!,
|
.map[json["foodNutrientSourceDescription"]],
|
||||||
rank: json["rank"],
|
rank: json["rank"],
|
||||||
indentLevel: json["indentLevel"],
|
indentLevel: json["indentLevel"],
|
||||||
foodNutrientId: json["foodNutrientId"],
|
foodNutrientId: json["foodNutrientId"],
|
||||||
|
@ -1,27 +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/models/food_data.dart';
|
||||||
|
import 'package:dietary_guard/widgets/alert_detect_result.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
class FoodDetailsScreen extends StatelessWidget {
|
class FoodDetailsScreen extends StatefulWidget {
|
||||||
final FoodData item;
|
final FoodData item;
|
||||||
|
|
||||||
const FoodDetailsScreen({super.key, required this.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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Material(
|
||||||
|
color: Theme.of(context).colorScheme.surface,
|
||||||
|
child: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(item.description),
|
title: Text(widget.item.description),
|
||||||
),
|
),
|
||||||
body: Column(
|
body: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
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),
|
Text('nutrients'.tr).paddingOnly(left: 24, right: 24, bottom: 8),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
itemCount: item.foodNutrients.length,
|
itemCount: widget.item.foodNutrients.length,
|
||||||
itemBuilder: (context, idx) {
|
itemBuilder: (context, idx) {
|
||||||
final entry = item.foodNutrients[idx];
|
final entry = widget.item.foodNutrients[idx];
|
||||||
final unitName = unitNameValues.reverse[entry.unitName];
|
final unitName = unitNameValues.reverse[entry.unitName];
|
||||||
return ListTile(
|
return ListTile(
|
||||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||||
@ -33,13 +148,16 @@ class FoodDetailsScreen extends StatelessWidget {
|
|||||||
Badge(label: Text('#${entry.nutrientId}'))
|
Badge(label: Text('#${entry.nutrientId}'))
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
subtitle: Text('${entry.nutrientNumber} ${unitName}'),
|
subtitle: Text(
|
||||||
|
'${entry.value?.toString() ?? '-'} $unitName',
|
||||||
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
).paddingSymmetric(vertical: 24),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ class QueryScreen extends StatefulWidget {
|
|||||||
class _QueryScreenState extends State<QueryScreen> {
|
class _QueryScreenState extends State<QueryScreen> {
|
||||||
bool _isLoading = false;
|
bool _isLoading = false;
|
||||||
|
|
||||||
int _totalCount = 0;
|
|
||||||
List<FoodData> _foodData = List.empty();
|
List<FoodData> _foodData = List.empty();
|
||||||
|
|
||||||
Future<void> _searchFood(String probe) async {
|
Future<void> _searchFood(String probe) async {
|
||||||
@ -30,7 +29,6 @@ class _QueryScreenState extends State<QueryScreen> {
|
|||||||
final result = await data.searchFood(probe);
|
final result = await data.searchFood(probe);
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_totalCount = result.totalHits;
|
|
||||||
_foodData = result.foods;
|
_foodData = result.foods;
|
||||||
_isLoading = false;
|
_isLoading = false;
|
||||||
});
|
});
|
||||||
@ -45,7 +43,9 @@ class _QueryScreenState extends State<QueryScreen> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return SafeArea(
|
return Material(
|
||||||
|
color: Theme.of(context).colorScheme.surface,
|
||||||
|
child: SafeArea(
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
SearchBar(
|
SearchBar(
|
||||||
@ -75,14 +75,14 @@ class _QueryScreenState extends State<QueryScreen> {
|
|||||||
const EdgeInsets.symmetric(horizontal: 24),
|
const EdgeInsets.symmetric(horizontal: 24),
|
||||||
title: Text(item.description),
|
title: Text(item.description),
|
||||||
subtitle: Text(
|
subtitle: Text(
|
||||||
DateFormat("yyyy-MM-dd").format(item.publishedDate),
|
'${DateFormat("yyyy-MM-dd").format(item.mostRecentAcquisitionDate ?? item.publishedDate)} ${foodCategoryValues.reverse[item.foodCategory] ?? ''}',
|
||||||
),
|
),
|
||||||
onTap: () => open(),
|
onTap: () => open(),
|
||||||
),
|
),
|
||||||
openBuilder: (_, __) => FoodDetailsScreen(item: item),
|
openBuilder: (_, __) => FoodDetailsScreen(item: item),
|
||||||
openElevation: 0,
|
openElevation: 0,
|
||||||
closedElevation: 0,
|
closedElevation: 0,
|
||||||
closedColor: Colors.transparent,
|
closedColor: Theme.of(context).colorScheme.surface,
|
||||||
openColor: Colors.transparent,
|
openColor: Colors.transparent,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@ -90,6 +90,7 @@ class _QueryScreenState extends State<QueryScreen> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
import 'package:dietary_guard/controllers/alert.dart';
|
||||||
import 'package:dietary_guard/controllers/food_data.dart';
|
import 'package:dietary_guard/controllers/food_data.dart';
|
||||||
|
import 'package:dietary_guard/models/alert_configuration.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
@ -12,10 +14,25 @@ class SettingsScreen extends StatefulWidget {
|
|||||||
class _SettingsScreenState extends State<SettingsScreen> {
|
class _SettingsScreenState extends State<SettingsScreen> {
|
||||||
final TextEditingController _fdcApiKeyController = TextEditingController();
|
final TextEditingController _fdcApiKeyController = TextEditingController();
|
||||||
|
|
||||||
|
List<AlertConfiguration> _currentAlerts = List.empty(growable: true);
|
||||||
|
|
||||||
|
void _addAlert() {
|
||||||
|
setState(() {
|
||||||
|
_currentAlerts.add(AlertConfiguration(
|
||||||
|
nutrientId: 0,
|
||||||
|
maxValue: 0,
|
||||||
|
minValue: 0,
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _applySettings() async {
|
Future<void> _applySettings() async {
|
||||||
final FoodDataController data = Get.find();
|
final FoodDataController data = Get.find();
|
||||||
await data.setApiKey(_fdcApiKeyController.text);
|
await data.setApiKey(_fdcApiKeyController.text);
|
||||||
|
|
||||||
|
final AlertController alert = Get.find();
|
||||||
|
await alert.setAlertConfiguration(_currentAlerts);
|
||||||
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
content: Text('settingsApplied'.tr),
|
content: Text('settingsApplied'.tr),
|
||||||
));
|
));
|
||||||
@ -26,6 +43,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
final FoodDataController data = Get.find();
|
final FoodDataController data = Get.find();
|
||||||
_fdcApiKeyController.text = data.getApiKey() ?? '';
|
_fdcApiKeyController.text = data.getApiKey() ?? '';
|
||||||
|
|
||||||
|
final AlertController alert = Get.find();
|
||||||
|
_currentAlerts = List.from(alert.configuration, growable: true);
|
||||||
|
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,36 +55,116 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _buildSectionHeader(String title) {
|
||||||
|
return Container(
|
||||||
|
color: Theme.of(context).colorScheme.secondaryContainer,
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 8),
|
||||||
|
child: Text(
|
||||||
|
title,
|
||||||
|
style: Theme.of(context).textTheme.bodyLarge,
|
||||||
|
),
|
||||||
|
).marginOnly(bottom: 16);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Material(
|
||||||
|
color: Theme.of(context).colorScheme.surface,
|
||||||
|
child: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text('settings'.tr),
|
title: Text('settings'.tr),
|
||||||
),
|
),
|
||||||
body: ListView(
|
body: ListView(
|
||||||
children: [
|
children: [
|
||||||
const SizedBox(height: 8),
|
_buildSectionHeader('settingsAlertSection'.tr),
|
||||||
|
Column(
|
||||||
|
children: [
|
||||||
|
...(_currentAlerts.map((x) => Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: TextFormField(
|
||||||
|
initialValue: x.nutrientId.toString(),
|
||||||
|
decoration: InputDecoration(
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
label: Text("alertNutrientId".tr),
|
||||||
|
isDense: true,
|
||||||
|
),
|
||||||
|
onTapOutside: (_) =>
|
||||||
|
FocusManager.instance.primaryFocus?.unfocus(),
|
||||||
|
onChanged: (value) {
|
||||||
|
x.nutrientId = int.tryParse(value) ?? 0;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 6),
|
||||||
|
Expanded(
|
||||||
|
child: TextFormField(
|
||||||
|
initialValue: x.maxValue.toString(),
|
||||||
|
decoration: InputDecoration(
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
label: Text("alertMaxValue".tr),
|
||||||
|
isDense: true,
|
||||||
|
),
|
||||||
|
onTapOutside: (_) =>
|
||||||
|
FocusManager.instance.primaryFocus?.unfocus(),
|
||||||
|
onChanged: (value) {
|
||||||
|
x.maxValue = double.tryParse(value) ?? 0;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 6),
|
||||||
|
Expanded(
|
||||||
|
child: TextFormField(
|
||||||
|
initialValue: x.minValue.toString(),
|
||||||
|
decoration: InputDecoration(
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
label: Text("alertMinValue".tr),
|
||||||
|
isDense: true,
|
||||||
|
),
|
||||||
|
onTapOutside: (_) =>
|
||||||
|
FocusManager.instance.primaryFocus?.unfocus(),
|
||||||
|
onChanged: (value) {
|
||||||
|
x.minValue = double.tryParse(value) ?? 0;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
))),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
TextButton.icon(
|
||||||
|
label: Text('newAlert'.tr),
|
||||||
|
onPressed: () => _addAlert(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
).paddingSymmetric(vertical: 12, horizontal: 24),
|
||||||
|
],
|
||||||
|
).paddingSymmetric(horizontal: 24),
|
||||||
|
_buildSectionHeader('settingsDataSection'.tr),
|
||||||
TextField(
|
TextField(
|
||||||
controller: _fdcApiKeyController,
|
controller: _fdcApiKeyController,
|
||||||
obscureText: true,
|
obscureText: true,
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
border: OutlineInputBorder(),
|
border: UnderlineInputBorder(),
|
||||||
label: Text("FDC API Key"),
|
label: Text("FDC API Key"),
|
||||||
isDense: true,
|
isDense: false,
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
onTapOutside: (_) =>
|
||||||
|
FocusManager.instance.primaryFocus?.unfocus(),
|
||||||
|
).paddingSymmetric(horizontal: 24),
|
||||||
Row(
|
Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
ElevatedButton.icon(
|
TextButton.icon(
|
||||||
icon: const Icon(Icons.save, size: 16),
|
|
||||||
label: Text('apply'.tr),
|
label: Text('apply'.tr),
|
||||||
onPressed: () => _applySettings(),
|
onPressed: () => _applySettings(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
).paddingSymmetric(vertical: 12),
|
).paddingSymmetric(vertical: 12, horizontal: 24),
|
||||||
],
|
],
|
||||||
).paddingSymmetric(horizontal: 24),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,14 @@ const i18nEnglish = {
|
|||||||
'settings': 'Settings',
|
'settings': 'Settings',
|
||||||
'preparingData': 'Preparing data...',
|
'preparingData': 'Preparing data...',
|
||||||
'settingsApplied': 'Settings Applied',
|
'settingsApplied': 'Settings Applied',
|
||||||
|
'settingsDataSection': 'Data Source',
|
||||||
|
'settingsAlertSection': 'Alert',
|
||||||
|
'newAlert': 'New Alert',
|
||||||
'apply': 'Apply',
|
'apply': 'Apply',
|
||||||
'searchHistoryNotIncluded': 'Search History not Included, yet',
|
'searchHistoryNotIncluded': 'Search History not Included, yet',
|
||||||
'nutrients': 'Nutrients',
|
'nutrients': 'Nutrients',
|
||||||
|
'alertNutrientId': 'Nutrient ID',
|
||||||
|
'alertMaxValue': 'Max',
|
||||||
|
'alertMinValue': 'Min',
|
||||||
|
'alerts': 'Alerts',
|
||||||
};
|
};
|
||||||
|
@ -3,7 +3,19 @@ const i18nSimplifiedChinese = {
|
|||||||
'settings': '设置',
|
'settings': '设置',
|
||||||
'preparingData': '准备数据中…',
|
'preparingData': '准备数据中…',
|
||||||
'settingsApplied': '设置已应用',
|
'settingsApplied': '设置已应用',
|
||||||
|
'settingsDataSection': '数据源',
|
||||||
|
'settingsAlertSection': '危险告警',
|
||||||
|
'newAlert': '新告警',
|
||||||
'apply': '应用',
|
'apply': '应用',
|
||||||
'searchHistoryNotIncluded': '搜索记录还没实现',
|
'searchHistoryNotIncluded': '搜索记录还没实现',
|
||||||
'nutrients': '营养物质',
|
'nutrients': '营养物质',
|
||||||
|
'alertNutrientId': '营养物质编号',
|
||||||
|
'alertMaxValue': '告警上限',
|
||||||
|
'alertMinValue': '告警下限',
|
||||||
|
'alerts': '告警',
|
||||||
|
'alertOutOfRange': '有 @count 项告警规则触发,点击右侧按钮了解详情',
|
||||||
|
'alertUnclear': '有 @count 项告警规则并无数据支持,点击右侧按钮了解详情',
|
||||||
|
'alertEmpty': '无告警规则配置,前往设置添加规则',
|
||||||
|
'alertSafe': '无告警规则触发,可安心食用',
|
||||||
|
'alertDetectResult': '告警匹配详情',
|
||||||
};
|
};
|
||||||
|
79
lib/widgets/alert_detect_result.dart
Normal file
79
lib/widgets/alert_detect_result.dart
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
import 'package:dietary_guard/models/alert_configuration.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
|
class AlertDetectResultDialog extends StatelessWidget {
|
||||||
|
final List<AlertDetectResult> result;
|
||||||
|
|
||||||
|
const AlertDetectResultDialog({super.key, required this.result});
|
||||||
|
|
||||||
|
Color _getColor(AlertDetectResult result) {
|
||||||
|
if (result.isOutOfRange) {
|
||||||
|
return Colors.red;
|
||||||
|
} else if (result.isUndetected) {
|
||||||
|
return Colors.grey;
|
||||||
|
} else {
|
||||||
|
return Colors.green;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IconData _getIcon(AlertDetectResult result) {
|
||||||
|
if (result.isOutOfRange) {
|
||||||
|
return Icons.close;
|
||||||
|
} else if (result.isUndetected) {
|
||||||
|
return Icons.question_mark;
|
||||||
|
} else {
|
||||||
|
return Icons.check;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return SizedBox(
|
||||||
|
height: MediaQuery.of(context).size.height * 0.65,
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
padding: const EdgeInsets.only(
|
||||||
|
left: 24,
|
||||||
|
right: 24,
|
||||||
|
top: 24,
|
||||||
|
bottom: 8,
|
||||||
|
),
|
||||||
|
child: Text(
|
||||||
|
'alertDetectResult'.tr,
|
||||||
|
style: Theme.of(context).textTheme.titleLarge,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: ListView.builder(
|
||||||
|
itemCount: result.length,
|
||||||
|
itemBuilder: (context, idx) {
|
||||||
|
final item = result[idx];
|
||||||
|
return ListTile(
|
||||||
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||||
|
leading: CircleAvatar(
|
||||||
|
backgroundColor: _getColor(item),
|
||||||
|
child: Icon(_getIcon(item), color: Colors.white),
|
||||||
|
),
|
||||||
|
title: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Text(item.name),
|
||||||
|
const SizedBox(width: 6),
|
||||||
|
Badge(label: Text('#${item.config.nutrientId}'))
|
||||||
|
],
|
||||||
|
),
|
||||||
|
subtitle: Text(
|
||||||
|
'${item.current} ${(item.difference ?? 0) > 0 ? '↑' : '↓'}${item.difference?.abs()} ${item.unitName}',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user