Install http in dependences!

import 'dart:convert';
import 'dart:ui';
import 'package:google_fonts/google_fonts.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  static const String _title = 'Flutter ChatGPT App';
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      theme: ThemeData(
        primarySwatch: Colors.orange,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _textFieldController = TextEditingController();
  String? _response;

  void translate(BuildContext context) {
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text("wait a moment")));
    _fetchResponse();
  }

  Future<void> _fetchResponse() async {
    final apiKey = "api key here";
    final headers = {
      "Authorization": 'Bearer $apiKey',
      "Content-Type": "application/json"
    };

    final body = jsonEncode({
      "model": "gpt-3.5-turbo",
      "messages": [
        {
          "role": "system",
          "content":
              "you are a translator app that translates english to italian. Translate the following sentence:"
        },
        {"role": "user", "content": _textFieldController.text}
      ]
    });

    final response = await http.post(
        Uri.parse("https://api.openai.com/v1/chat/completions"),
        headers: headers,
        body: body);

    if (response.statusCode == 200) {
      print("CIAOOO");
      //print(response.body);
      final data = json.decode(response.body);
      final message = data["choices"][0]["message"]["content"];

      setState(() {
        _response = message;
        print(_response);
      });
    } else {
      print("Failed to get response");
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
            title: Text("H-farm 23 ChatGPT"),
          ),
          body: Column(
            children: [
              // Note: Same code is applied for the TextFormField as well
              // Note: Same code is applied for the TextFormField as well
              Padding(
                padding: EdgeInsets.all(8),
                child: TextField(
                  controller: _textFieldController,
                  decoration: InputDecoration(
                    suffixIcon: Icon(Icons.search),
                    hintText: "Enter your text",
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                          width: 3, color: Colors.orangeAccent), //<-- SEE HERE
                      borderRadius: BorderRadius.circular(50.0),
                    ),
                  ),
                ),
              ),
              ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: SizedBox(
                  height: 40,
                  width: 340,
                  child: ElevatedButton.icon(
                      onPressed: () {
                        translate(context);
                      },
                      icon: Icon(Icons.translate),
                      label: Text(
                        "Translate",
                        style: TextStyle(fontSize: 18),
                      )),
                ),
              ),

              Container(
                padding: EdgeInsets.all(16),
                width: 300,
                margin: EdgeInsets.only(top: 16),
                height: MediaQuery.of(context).size.height / 1.5,
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.redAccent),
                    borderRadius: BorderRadius.circular(15)),
                child: _response != null
                    ? Text(_response!)
                    : Image.network(
                        "https://assets.stickpng.com/images/5900c83716ae4b3fc58f4826.png"),
              )
            ],
          )),
    );
  }
}