✨ Search hint
This commit is contained in:
		| @@ -15,7 +15,9 @@ class QueryScreen extends StatefulWidget { | |||||||
|  |  | ||||||
| class _QueryScreenState extends State<QueryScreen> { | class _QueryScreenState extends State<QueryScreen> { | ||||||
|   bool _isLoading = false; |   bool _isLoading = false; | ||||||
|  |   bool _hasApiKey = true; | ||||||
|  |  | ||||||
|  |   int? _totalCount; | ||||||
|   List<FoodData> _foodData = List.empty(); |   List<FoodData> _foodData = List.empty(); | ||||||
|  |  | ||||||
|   Future<void> _searchFood(String probe) async { |   Future<void> _searchFood(String probe) async { | ||||||
| @@ -24,11 +26,15 @@ class _QueryScreenState extends State<QueryScreen> { | |||||||
|     setState(() => _isLoading = true); |     setState(() => _isLoading = true); | ||||||
|  |  | ||||||
|     final FoodDataController data = Get.find(); |     final FoodDataController data = Get.find(); | ||||||
|     if (data.getApiKey() == null) return; |     if (data.getApiKey() == null) { | ||||||
|  |       setState(() => _hasApiKey = false); | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     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; | ||||||
|     }); |     }); | ||||||
| @@ -48,6 +54,14 @@ class _QueryScreenState extends State<QueryScreen> { | |||||||
|       child: SafeArea( |       child: SafeArea( | ||||||
|         child: Column( |         child: Column( | ||||||
|           children: [ |           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( |             SearchBar( | ||||||
|               padding: const WidgetStatePropertyAll<EdgeInsets>( |               padding: const WidgetStatePropertyAll<EdgeInsets>( | ||||||
|                 EdgeInsets.symmetric(horizontal: 16.0), |                 EdgeInsets.symmetric(horizontal: 16.0), | ||||||
|   | |||||||
| @@ -39,4 +39,7 @@ const i18nSimplifiedChinese = { | |||||||
|   'fdcApiCredit': |   'fdcApiCredit': | ||||||
|       'DietaryGuard 的食品数据来源于 U.S. Department of Agriculture, Agricultural Research Service, Beltsville Human Nutrition Research Center. FoodData Central. 在此感谢他们慷慨贡献的食品数据并发布在公有领域。', |       'DietaryGuard 的食品数据来源于 U.S. Department of Agriculture, Agricultural Research Service, Beltsville Human Nutrition Research Center. FoodData Central. 在此感谢他们慷慨贡献的食品数据并发布在公有领域。', | ||||||
|   'loading': '加载中', |   'loading': '加载中', | ||||||
|  |   'searchHint': '在下方键入关键词来搜索', | ||||||
|  |   'searchNoKeyHint': '无 API 令牌,请先在「设置」>「数据源」设置 API 密钥', | ||||||
|  |   'searchResultHint': '共命中 @count 条记录(截取前 100 条)', | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user