✨ Setup data
This commit is contained in:
		| @@ -1,10 +1,84 @@ | ||||
| import 'package:dietary_guard/controllers/food_data.dart'; | ||||
| import 'package:dietary_guard/models/food_data.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:get/get.dart'; | ||||
| import 'package:intl/intl.dart'; | ||||
|  | ||||
| class QueryScreen extends StatelessWidget { | ||||
| class QueryScreen extends StatefulWidget { | ||||
|   const QueryScreen({super.key}); | ||||
|  | ||||
|   @override | ||||
|   State<QueryScreen> createState() => _QueryScreenState(); | ||||
| } | ||||
|  | ||||
| class _QueryScreenState extends State<QueryScreen> { | ||||
|   bool _isLoading = false; | ||||
|  | ||||
|   int _totalCount = 0; | ||||
|   List<FoodData> _foodData = List.empty(); | ||||
|  | ||||
|   Future<void> _searchFood(String probe) async { | ||||
|     if (_isLoading) return; | ||||
|  | ||||
|     setState(() => _isLoading = true); | ||||
|  | ||||
|     final FoodDataController data = Get.find(); | ||||
|     if (data.getApiKey() == null) return; | ||||
|  | ||||
|     final result = await data.searchFood(probe); | ||||
|  | ||||
|     setState(() { | ||||
|       _totalCount = result.totalHits; | ||||
|       _foodData = result.foods; | ||||
|       _isLoading = false; | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   void initState() { | ||||
|     super.initState(); | ||||
|     final FoodDataController data = Get.find(); | ||||
|     data.initialize(context); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
|     return const SizedBox(); | ||||
|     return SafeArea( | ||||
|       child: Column( | ||||
|         children: [ | ||||
|           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: 20, | ||||
|               height: 20, | ||||
|               child: CircularProgressIndicator(strokeWidth: 3), | ||||
|             ).paddingSymmetric(vertical: 16) | ||||
|           else | ||||
|             Expanded( | ||||
|               child: ListView.builder( | ||||
|                 itemCount: _foodData.length, | ||||
|                 itemBuilder: (context, index) { | ||||
|                   final item = _foodData[index]; | ||||
|                   return ListTile( | ||||
|                     contentPadding: const EdgeInsets.symmetric(horizontal: 24), | ||||
|                     title: Text(item.description), | ||||
|                     subtitle: Text( | ||||
|                       DateFormat("yyyy-MM-dd").format(item.publishedDate), | ||||
|                     ), | ||||
|                   ); | ||||
|                 }, | ||||
|               ).paddingOnly(top: 8), | ||||
|             ), | ||||
|         ], | ||||
|       ), | ||||
|     ); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -1,10 +1,70 @@ | ||||
| import 'package:dietary_guard/controllers/food_data.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:get/get.dart'; | ||||
|  | ||||
| class SettingsScreen extends StatelessWidget { | ||||
| class SettingsScreen extends StatefulWidget { | ||||
|   const SettingsScreen({super.key}); | ||||
|  | ||||
|   @override | ||||
|   State<SettingsScreen> createState() => _SettingsScreenState(); | ||||
| } | ||||
|  | ||||
| class _SettingsScreenState extends State<SettingsScreen> { | ||||
|   final TextEditingController _fdcApiKeyController = TextEditingController(); | ||||
|  | ||||
|   Future<void> _applySettings() async { | ||||
|     final FoodDataController data = Get.find(); | ||||
|     await data.setApiKey(_fdcApiKeyController.text); | ||||
|  | ||||
|     ScaffoldMessenger.of(context).showSnackBar(SnackBar( | ||||
|       content: Text('settingsApplied'.tr), | ||||
|     )); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   void initState() { | ||||
|     final FoodDataController data = Get.find(); | ||||
|     _fdcApiKeyController.text = data.getApiKey() ?? ''; | ||||
|  | ||||
|     super.initState(); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   void dispose() { | ||||
|     _fdcApiKeyController.dispose(); | ||||
|     super.dispose(); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
|     return const Placeholder(); | ||||
|     return Scaffold( | ||||
|       appBar: AppBar( | ||||
|         title: Text('settings'.tr), | ||||
|       ), | ||||
|       body: ListView( | ||||
|         children: [ | ||||
|           const SizedBox(height: 8), | ||||
|           TextField( | ||||
|             controller: _fdcApiKeyController, | ||||
|             obscureText: true, | ||||
|             decoration: const InputDecoration( | ||||
|               border: OutlineInputBorder(), | ||||
|               label: Text("FDC API Key"), | ||||
|               isDense: true, | ||||
|             ), | ||||
|           ), | ||||
|           Row( | ||||
|             mainAxisAlignment: MainAxisAlignment.end, | ||||
|             children: [ | ||||
|               ElevatedButton.icon( | ||||
|                 icon: const Icon(Icons.save, size: 16), | ||||
|                 label: Text('apply'.tr), | ||||
|                 onPressed: () => _applySettings(), | ||||
|               ), | ||||
|             ], | ||||
|           ).paddingSymmetric(vertical: 12), | ||||
|         ], | ||||
|       ).paddingSymmetric(horizontal: 24), | ||||
|     ); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user