// ignore_for_file: library_private_types_in_public_api import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:cookbook/screens/recipe_search/recipe_screen.dart'; class SearchScreen extends StatefulWidget { const SearchScreen({super.key}); @override _SearchScreenState createState() => _SearchScreenState(); } class _SearchScreenState extends State { final TextEditingController _searchController = TextEditingController(); List recipes = []; Future searchRecipes(String query) async { const appId = '5810e402'; const appKey = 'ccb88260d1ea1ff705c195161054c830'; final apiUrl = 'https://api.edamam.com/search?q=$query&app_id=$appId&app_key=$appKey'; final response = await http.get(Uri.parse(apiUrl)); if (response.statusCode == 200) { final decodedResponse = jsonDecode(response.body); final hits = decodedResponse['hits']; setState(() { recipes = hits; }); } } @override void dispose() { _searchController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Search Recipes',style: TextStyle(color: Colors.white), ), iconTheme: const IconThemeData(color: Colors.white), ), body: Container( decoration: const BoxDecoration( image: DecorationImage( image: AssetImage('img/background.jpg'), fit: BoxFit.cover, ), ), child: Column( children: [ Padding( padding: const EdgeInsets.all(8.0), child: TextField( controller: _searchController, style: const TextStyle(color: Colors.white), decoration: const InputDecoration( labelText: 'Search for recipes', labelStyle: TextStyle( color: Colors.white ), border: OutlineInputBorder(), ), autofocus: true, ), ), ElevatedButton( onPressed: () { searchRecipes(_searchController.text); FocusScope.of(context).unfocus(); }, child: const Text('Search',style: TextStyle(color: Colors.white)), ), Expanded( child: GridView.builder( gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( maxCrossAxisExtent: 250, mainAxisSpacing: 10, crossAxisSpacing: 10, childAspectRatio: 0.8, ), itemCount: recipes.length, itemBuilder: (context, index) { final recipe = recipes[index]['recipe']; return GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => RecipeDetailsScreen(recipe: recipe), ), ); }, child: Container( decoration: BoxDecoration( color: Colors.orange, borderRadius: BorderRadius.circular(10), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 5, offset: const Offset(0, 3), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ ClipRRect( borderRadius: const BorderRadius.only( topLeft: Radius.circular(10), topRight: Radius.circular(10), ), child: Image.network( recipe['image'], height: 180, width: double.infinity, fit: BoxFit.cover, ), ), Padding( padding: const EdgeInsets.all(8.0), child: Text( recipe['label'], style: const TextStyle( fontWeight: FontWeight.bold, fontSize: 14, color: Colors.white ), maxLines: 2, overflow: TextOverflow.ellipsis, ), ), ], ), ), ); }, ), ), ], ), ), ); } }