From 5b6ee75ca88e95ba30e7e11502437f172dea7e53 Mon Sep 17 00:00:00 2001 From: paoloGuagnano Date: Mon, 29 Apr 2024 17:06:47 +0200 Subject: [PATCH] modify layout and add function to print error and text translate --- .../screens/home/components/words_screen.dart | 2 +- .../screens/home/home_sreen_statefull.dart | 85 ++++++++++++------- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/motula_translate_app/lib/screens/home/components/words_screen.dart b/motula_translate_app/lib/screens/home/components/words_screen.dart index 825da8a..9613d6b 100644 --- a/motula_translate_app/lib/screens/home/components/words_screen.dart +++ b/motula_translate_app/lib/screens/home/components/words_screen.dart @@ -17,7 +17,7 @@ class WordsScreen extends StatefulWidget { class _WordsScreenState extends State { Future fetchWord() async { final response = await http.get( - Uri.parse("https://9152-37-101-56-133.ngrok-free.app/api/words/tasto")); + Uri.parse("https://d06f-151-45-52-98.ngrok-free.app/api/words/tasto")); if (response.statusCode == 200) { return Word.fromJson(jsonDecode(response.body)); diff --git a/motula_translate_app/lib/screens/home/home_sreen_statefull.dart b/motula_translate_app/lib/screens/home/home_sreen_statefull.dart index 061a663..3f4bdfc 100644 --- a/motula_translate_app/lib/screens/home/home_sreen_statefull.dart +++ b/motula_translate_app/lib/screens/home/home_sreen_statefull.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; import '../../model/word.dart'; import 'components/words_screen.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; class homepagestful extends StatefulWidget { const homepagestful({super.key}); @@ -12,8 +14,33 @@ class homepagestful extends StatefulWidget { } class _homepagestfulState extends State { - final TextEditingController _textHomepageContoller = TextEditingController(); + late TextEditingController _textHomepageContoller = TextEditingController(); late List listaWidget; + late String _request = ''; //stringa dove memorizzare la parola + + //metodo per la richiesta + Future fetchWord() async { + String text_controller = _textHomepageContoller.text; + final response = await http.get( + Uri.parse("https://315a-151-45-52-98.ngrok-free.app/api/words/$text_controller")); + + if (response.statusCode == 200) { + final Map data = json.decode(response.body); + if (data['status'] == true) { + final request = data['translation']; + setState(() { + _request = request; //aggiorna lo stato della stringa + }); + } else { + final request = data['message']; + setState(() { + _request = request; //aggiorna lo stato della stringa con messsaggio not found + }); + } + } else { + throw Exception('Failed to fetch Word'); + } + } @override void dispose() { @@ -60,11 +87,14 @@ class _homepagestfulState extends State { color: const Color(0x9121781C), borderRadius: BorderRadius.circular(10.0), ), - child: const Column( + child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ - WordsScreen(), + Text(_request, + style: const TextStyle(fontSize: 30, color: Color(0xFFFFFFFF),fontWeight: FontWeight.bold) + ), + //Align( // alignment: Alignment.center, // child: Text("ESEMPIO", @@ -74,36 +104,33 @@ class _homepagestfulState extends State { ), ), const SizedBox(height: 20), - TextButton( + ElevatedButton( onPressed: () { - showDialog( - context: context, - builder: (context) { - if (_textHomepageContoller.text.isEmpty){ - return const AlertDialog( - - content: Text('Inserisci la parola italiana da tradurre', style: TextStyle(color: Color( - 0xFFFF0000), fontSize: 25, fontWeight: FontWeight.bold)), - ); - }else{ - //return AlertDialog( - // Retrieve the text the that user has entered by using the - // TextEditingController. - // content: Text(_textHomepageContoller.text), - //); - - return WordsScreen(); - } - }, - ); + if (_textHomepageContoller.text.isEmpty){ + showDialog( + context: context, + builder: (context) { + return const AlertDialog( + content: Text('Inserire il vocabolo da tradurre', + style: TextStyle(color: Color( + 0xFFFF0000), + fontSize: 25, + fontWeight: FontWeight.bold)), + ); + }, + ); + } else { + fetchWord(); + } }, + child: Text('Traduci'), style: ElevatedButton.styleFrom( - backgroundColor: const Color(0xFFDF930B), - padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 20), - textStyle: const TextStyle( - fontSize: 15, - fontWeight: FontWeight.bold)), + backgroundColor: const Color(0xFFDF930B), + padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 20), + textStyle: const TextStyle( + fontSize: 15, + fontWeight: FontWeight.bold)), ), ], ),