import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shopindia/views/search_results_page.dart';
import 'package:shopindia/views/widgets/bottom_navigation_bar.dart';

class SearchPage extends StatelessWidget {
  SearchPage({super.key});

  final TextEditingController searchController = TextEditingController();

  void performSearch(String query) {
    // Implement your search functionality here
    Get.to(SearchResultsPage(query: query,));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Search'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Get.back(),
        ),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: TextField(
            controller: searchController,
            onSubmitted: performSearch,
            decoration: InputDecoration(
              hintText: 'Enthaa veenden para...', // Placeholder text
              prefixIcon: const Icon(Icons.search),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8.0),
              ),
            ),
            textInputAction: TextInputAction.search,
          ),
        ),
      ),
      bottomNavigationBar: bottomNavigationBar, // State-managed navigation bar
    );
  }
}



MY SERVER HAS THE CAPACITY TO DO REVERSE IMAGE SEARCH. upload image to localhost:8000/upload_image/ using post. I want to implement the capability to upload image and display serach results using SearchResultsPage so update both files accordingly

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/search_controller.dart';
import 'widgets/product_card.dart';
import 'widgets/bottom_navigation_bar.dart'; 


class SearchResultsPage extends StatelessWidget {
  final SearchPageController searchPageController = Get.put(SearchPageController());
  final String query;
  SearchResultsPage({super.key, required this.query}) {
    // Set the search parameters in the controller
    searchPageController.setSearchParameters(50, query); // Adjust number of results as needed
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(query),
        ),
        body: Obx(() {
          if (searchPageController.isLoading.value) {
            return const Center(child: CircularProgressIndicator());
          } else if (searchPageController.errorMessage.isNotEmpty) {
            return Center(child: Text(searchPageController.errorMessage.value));
          } else {
            return GridView.builder(
              padding: const EdgeInsets.all(8.0),
              itemCount: searchPageController.products.length,
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2, // One column, change to 2 for two columns
                crossAxisSpacing: 8.0,
                mainAxisSpacing: 8.0,
                childAspectRatio: 0.75, // Adjust as needed
              ),
              itemBuilder: (context, index) {
                final product = searchPageController.products[index];
                return ProductCard(product: product);
              },
            );
          }
        }),
        bottomNavigationBar: bottomNavigationBar); // Using the bottomNavigationBar with state management
  }
}

import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import '../models/product.dart';

class SearchPageController extends GetxController {
  var products = <Product>[].obs;
  var isLoading = true.obs;
  var errorMessage = ''.obs;

  // Define the variables to store the search parameters
  var numResults = 50; // Default number of results
  var query = ''; // Default query string

  @override
  void onInit() {
    super.onInit();
    fetchProducts();
  }

  // Optionally, create a method to update the search parameters
  void setSearchParameters(int results, String searchQuery) {
    numResults = results;
    query = searchQuery;
    fetchProducts();
  }

  Future<void> fetchProducts() async {
    try {
      isLoading(true);
      errorMessage('');

      // Make sure to use the defined variables
      final response = await http.get(Uri.parse(
          'http://localhost:8000/search?num=$numResults&query=$query'));

      if (response.statusCode == 200) {
        products.value = Product.listFromJson(response.body);
      } else {
        errorMessage('Failed to load products: ${response.statusCode}');
      }
    } catch (e) {
      errorMessage('An error occurred: $e');
    } finally {
      isLoading(false);
    }
  }
}