Search hint

This commit is contained in:
LittleSheep 2024-08-15 21:20:07 +08:00
parent bd50859e73
commit 4569c33430
2 changed files with 18 additions and 1 deletions

View File

@ -15,7 +15,9 @@ class QueryScreen extends StatefulWidget {
class _QueryScreenState extends State<QueryScreen> {
bool _isLoading = false;
bool _hasApiKey = true;
int? _totalCount;
List<FoodData> _foodData = List.empty();
Future<void> _searchFood(String probe) async {
@ -24,11 +26,15 @@ class _QueryScreenState extends State<QueryScreen> {
setState(() => _isLoading = true);
final FoodDataController data = Get.find();
if (data.getApiKey() == null) return;
if (data.getApiKey() == null) {
setState(() => _hasApiKey = false);
return;
}
final result = await data.searchFood(probe);
setState(() {
_totalCount = result.totalHits;
_foodData = result.foods;
_isLoading = false;
});
@ -48,6 +54,14 @@ class _QueryScreenState extends State<QueryScreen> {
child: SafeArea(
child: Column(
children: [
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),
SearchBar(
padding: const WidgetStatePropertyAll<EdgeInsets>(
EdgeInsets.symmetric(horizontal: 16.0),

View File

@ -39,4 +39,7 @@ const i18nSimplifiedChinese = {
'fdcApiCredit':
'DietaryGuard 的食品数据来源于 U.S. Department of Agriculture, Agricultural Research Service, Beltsville Human Nutrition Research Center. FoodData Central. 在此感谢他们慷慨贡献的食品数据并发布在公有领域。',
'loading': '加载中',
'searchHint': '在下方键入关键词来搜索',
'searchNoKeyHint': '无 API 令牌,请先在「设置」>「数据源」设置 API 密钥',
'searchResultHint': '共命中 @count 条记录(截取前 100 条)',
};