modify layout and add function to print error and text translate
This commit is contained in:
@@ -17,7 +17,7 @@ class WordsScreen extends StatefulWidget {
|
|||||||
class _WordsScreenState extends State<WordsScreen> {
|
class _WordsScreenState extends State<WordsScreen> {
|
||||||
Future<Word> fetchWord() async {
|
Future<Word> fetchWord() async {
|
||||||
final response = await http.get(
|
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) {
|
if (response.statusCode == 200) {
|
||||||
return Word.fromJson(jsonDecode(response.body));
|
return Word.fromJson(jsonDecode(response.body));
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
|
|||||||
|
|
||||||
import '../../model/word.dart';
|
import '../../model/word.dart';
|
||||||
import 'components/words_screen.dart';
|
import 'components/words_screen.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
class homepagestful extends StatefulWidget {
|
class homepagestful extends StatefulWidget {
|
||||||
const homepagestful({super.key});
|
const homepagestful({super.key});
|
||||||
@@ -12,8 +14,33 @@ class homepagestful extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _homepagestfulState extends State<homepagestful> {
|
class _homepagestfulState extends State<homepagestful> {
|
||||||
final TextEditingController _textHomepageContoller = TextEditingController();
|
late TextEditingController _textHomepageContoller = TextEditingController();
|
||||||
late List<WordsScreen> listaWidget;
|
late List<WordsScreen> listaWidget;
|
||||||
|
late String _request = ''; //stringa dove memorizzare la parola
|
||||||
|
|
||||||
|
//metodo per la richiesta
|
||||||
|
Future<void> 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<String, dynamic> 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
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
@@ -60,11 +87,14 @@ class _homepagestfulState extends State<homepagestful> {
|
|||||||
color: const Color(0x9121781C),
|
color: const Color(0x9121781C),
|
||||||
borderRadius: BorderRadius.circular(10.0),
|
borderRadius: BorderRadius.circular(10.0),
|
||||||
),
|
),
|
||||||
child: const Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
WordsScreen(),
|
Text(_request,
|
||||||
|
style: const TextStyle(fontSize: 30, color: Color(0xFFFFFFFF),fontWeight: FontWeight.bold)
|
||||||
|
),
|
||||||
|
|
||||||
//Align(
|
//Align(
|
||||||
// alignment: Alignment.center,
|
// alignment: Alignment.center,
|
||||||
// child: Text("ESEMPIO",
|
// child: Text("ESEMPIO",
|
||||||
@@ -74,36 +104,33 @@ class _homepagestfulState extends State<homepagestful> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
TextButton(
|
ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
showDialog(
|
if (_textHomepageContoller.text.isEmpty){
|
||||||
context: context,
|
showDialog(
|
||||||
builder: (context) {
|
context: context,
|
||||||
if (_textHomepageContoller.text.isEmpty){
|
builder: (context) {
|
||||||
return const AlertDialog(
|
return const AlertDialog(
|
||||||
|
content: Text('Inserire il vocabolo da tradurre',
|
||||||
content: Text('Inserisci la parola italiana da tradurre', style: TextStyle(color: Color(
|
style: TextStyle(color: Color(
|
||||||
0xFFFF0000), fontSize: 25, fontWeight: FontWeight.bold)),
|
0xFFFF0000),
|
||||||
);
|
fontSize: 25,
|
||||||
}else{
|
fontWeight: FontWeight.bold)),
|
||||||
//return AlertDialog(
|
);
|
||||||
// Retrieve the text the that user has entered by using the
|
},
|
||||||
// TextEditingController.
|
);
|
||||||
// content: Text(_textHomepageContoller.text),
|
} else {
|
||||||
//);
|
fetchWord();
|
||||||
|
}
|
||||||
return WordsScreen();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
child: Text('Traduci'),
|
child: Text('Traduci'),
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
backgroundColor: const Color(0xFFDF930B),
|
backgroundColor: const Color(0xFFDF930B),
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 20),
|
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 20),
|
||||||
textStyle: const TextStyle(
|
textStyle: const TextStyle(
|
||||||
fontSize: 15,
|
fontSize: 15,
|
||||||
fontWeight: FontWeight.bold)),
|
fontWeight: FontWeight.bold)),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user