2024-08-14 17:38:29 +00:00
|
|
|
import 'package:animations/animations.dart';
|
2024-08-14 17:26:42 +00:00
|
|
|
import 'package:dietary_guard/controllers/food_data.dart';
|
|
|
|
import 'package:dietary_guard/models/food_data.dart';
|
2024-08-14 17:38:29 +00:00
|
|
|
import 'package:dietary_guard/screens/food/details.dart';
|
2024-08-14 15:43:21 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-08-14 17:26:42 +00:00
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
2024-08-14 15:43:21 +00:00
|
|
|
|
2024-08-14 17:26:42 +00:00
|
|
|
class QueryScreen extends StatefulWidget {
|
2024-08-14 15:43:21 +00:00
|
|
|
const QueryScreen({super.key});
|
|
|
|
|
2024-08-14 17:26:42 +00:00
|
|
|
@override
|
|
|
|
State<QueryScreen> createState() => _QueryScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _QueryScreenState extends State<QueryScreen> {
|
|
|
|
bool _isLoading = false;
|
2024-08-15 13:20:07 +00:00
|
|
|
bool _hasApiKey = true;
|
2024-08-14 17:26:42 +00:00
|
|
|
|
2024-08-15 13:20:07 +00:00
|
|
|
int? _totalCount;
|
2024-08-14 17:26:42 +00:00
|
|
|
List<FoodData> _foodData = List.empty();
|
|
|
|
|
|
|
|
Future<void> _searchFood(String probe) async {
|
|
|
|
if (_isLoading) return;
|
|
|
|
|
|
|
|
setState(() => _isLoading = true);
|
|
|
|
|
|
|
|
final FoodDataController data = Get.find();
|
2024-08-15 14:53:49 +00:00
|
|
|
if (data.getApiKey() == null || data.getApiKey()!.isEmpty) {
|
2024-08-15 13:20:07 +00:00
|
|
|
setState(() => _hasApiKey = false);
|
|
|
|
return;
|
|
|
|
}
|
2024-08-14 17:26:42 +00:00
|
|
|
|
|
|
|
final result = await data.searchFood(probe);
|
|
|
|
|
|
|
|
setState(() {
|
2024-08-15 13:20:07 +00:00
|
|
|
_totalCount = result.totalHits;
|
2024-08-14 17:26:42 +00:00
|
|
|
_foodData = result.foods;
|
|
|
|
_isLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
final FoodDataController data = Get.find();
|
2024-08-15 14:53:49 +00:00
|
|
|
data.initialize(context).then((_) {
|
|
|
|
setState(() {
|
|
|
|
_hasApiKey = data.getApiKey() != null && data.getApiKey()!.isNotEmpty;
|
|
|
|
});
|
|
|
|
});
|
2024-08-14 17:26:42 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 15:43:21 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-08-15 03:53:01 +00:00
|
|
|
return Material(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: SafeArea(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2024-08-15 13:20:07 +00:00
|
|
|
if (!_hasApiKey)
|
|
|
|
Text('searchNoKeyHint'.tr).paddingSymmetric(vertical: 8)
|
|
|
|
else if (_totalCount != null)
|
|
|
|
Text('searchResultHint'
|
|
|
|
.trParams({'count': _totalCount.toString()}))
|
|
|
|
.paddingSymmetric(vertical: 8)
|
|
|
|
else
|
|
|
|
Text('searchHint'.tr).paddingSymmetric(vertical: 8),
|
2024-08-15 03:53:01 +00:00
|
|
|
SearchBar(
|
|
|
|
padding: const WidgetStatePropertyAll<EdgeInsets>(
|
|
|
|
EdgeInsets.symmetric(horizontal: 16.0),
|
|
|
|
),
|
|
|
|
onSubmitted: (value) {
|
|
|
|
_searchFood(value);
|
|
|
|
},
|
|
|
|
leading: const Icon(Icons.search),
|
|
|
|
).paddingSymmetric(horizontal: 24),
|
|
|
|
if (_isLoading)
|
|
|
|
const SizedBox(
|
|
|
|
width: 28,
|
|
|
|
height: 28,
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 3),
|
|
|
|
).paddingSymmetric(vertical: 24)
|
|
|
|
else
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: _foodData.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final item = _foodData[index];
|
|
|
|
return OpenContainer(
|
|
|
|
closedBuilder: (_, open) => ListTile(
|
|
|
|
contentPadding:
|
|
|
|
const EdgeInsets.symmetric(horizontal: 24),
|
|
|
|
title: Text(item.description),
|
|
|
|
subtitle: Text(
|
2024-08-15 12:10:07 +00:00
|
|
|
'${DateFormat("yyyy-MM-dd").format(item.mostRecentAcquisitionDate ?? item.publishedDate)} ${item.foodCategory ?? ''}',
|
2024-08-15 03:53:01 +00:00
|
|
|
),
|
|
|
|
onTap: () => open(),
|
2024-08-14 17:38:29 +00:00
|
|
|
),
|
2024-08-15 03:53:01 +00:00
|
|
|
openBuilder: (_, __) => FoodDetailsScreen(item: item),
|
|
|
|
openElevation: 0,
|
|
|
|
closedElevation: 0,
|
|
|
|
closedColor: Theme.of(context).colorScheme.surface,
|
|
|
|
openColor: Colors.transparent,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
).paddingOnly(top: 8),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-08-14 17:26:42 +00:00
|
|
|
),
|
|
|
|
);
|
2024-08-14 15:43:21 +00:00
|
|
|
}
|
|
|
|
}
|