skantravels/Mobile/lib/touristguidebook.dart
2024-09-17 09:14:31 +02:00

186 lines
8.0 KiB
Dart

import 'dart:convert';
import 'package:dropdown_search/dropdown_search.dart';
import 'package:flutter/material.dart';
import 'package:mobile/base/sidemenu.dart';
import 'services/openaiservice.dart' as api;
class TouristGuideBookPage extends StatefulWidget {
const TouristGuideBookPage({super.key});
@override
State<TouristGuideBookPage> createState() => _TouristGuideBookPageState();
}
class _TouristGuideBookPageState extends State<TouristGuideBookPage> {
String? _selectedCountry;
bool _isLoading = false;
Map<String, dynamic> _touristbook = {};
final List<String> countries = [
"Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Argentina",
"Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil",
"Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia",
"Cameroon", "Canada", "Central African Republic", "Chad", "Chile", "China",
"Colombia", "Comoros", "Congo (Congo-Brazzaville)", "Congo (Democratic Republic)",
"Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark",
"Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Equatorial Guinea", "Eritrea", "Estonia", "Eswatini", "Ethiopia", "Fiji",
"Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana",
"Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
"Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran",
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan",
"Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon",
"Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
"Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
"Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco",
"Mongolia", "Montenegro", "Morocco", "Mozambique", "Myanmar", "Namibia",
"Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger",
"Nigeria", "North Korea", "North Macedonia", "Norway", "Oman", "Pakistan",
"Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Poland", "Portugal", "Qatar", "Romania", "Russia", "Rwanda", "Saint Kitts and Nevis",
"Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
"Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles",
"Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "South Korea", "South Sudan", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan",
"Tanzania", "Thailand", "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine",
"United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
"Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"
];
Future<void> getBook() async {
if (_selectedCountry != null) {
setState(() {
_isLoading = true;
});
try {
final response = await api.OpenAIService.getGuideBook(_selectedCountry!);
print(response);
final jsonData = jsonDecode(response);
setState(() {
_touristbook = jsonData[_selectedCountry!];
});
} catch (e) {
print('Error fetching guidebook: $e');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to load guidebook')),
);
} finally {
setState(() {
_isLoading = false;
});
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Choose a country')),
);
}
}
Widget _buildTouristBookSection(String title, String content) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
SizedBox(height: 5),
Text(content, style: TextStyle(fontSize: 16)),
],
),
);
}
@override
Widget build(BuildContext context) {
return SideMenu(
selectedIndex: 5,
body: Scaffold(
appBar: AppBar(
title: Text('Select Country'),
),
body: _isLoading
? Center(child: CircularProgressIndicator())
: SingleChildScrollView( // Added SingleChildScrollView here
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
DropdownSearch<String>(
items: countries,
popupProps: const PopupProps.menu(
showSearchBox: true,
searchFieldProps: TextFieldProps(
decoration: InputDecoration(
labelText: "Search a country",
border: OutlineInputBorder(),
),
),
),
dropdownDecoratorProps: const DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
labelText: "Select a country",
hintText: "Search a country",
border: OutlineInputBorder(),
),
),
onChanged: (String? country) {
setState(() {
_selectedCountry = country;
});
},
selectedItem: "Select a country you want a touristbook for",
),
SizedBox(height: 20),
ElevatedButton(
onPressed: getBook,
child: Text('Get Guidebook'),
),
if (_touristbook.isNotEmpty) ...[
_buildTouristBookSection(
_selectedCountry!,""
),
_buildTouristBookSection(
"Introduction - People",
_touristbook['introduction']?['people'] ?? 'No data',
),
_buildTouristBookSection(
"Introduction - Environment",
_touristbook['introduction']?['environment'] ?? 'No data',
),
_buildTouristBookSection(
"Famous Sites",
_touristbook['tourist_attractions']?['famous_sites']?.map((site) => site['name'] + ':\n' + site['description']).join('\n') ?? 'No data',
),
_buildTouristBookSection(
"Basic Language - Greetings",
_touristbook['basic_words']?.entries.map((e) => '${e.key}: ${e.value}').join('\n') ?? 'No data',
),
_buildTouristBookSection(
"Transportation Overview",
_touristbook['transportation']?['overview'] ?? 'No data',
),
_buildTouristBookSection(
"Public Transport - Buses",
_touristbook['transportation']?['public_transport']?['buses'] ?? 'No data',
),
_buildTouristBookSection(
"Public Transport - Trains",
_touristbook['transportation']?['public_transport']?['trains'] ?? 'No data',
),
_buildTouristBookSection(
"Public Transport - Taxis",
_touristbook['transportation']?['public_transport']?['taxis'] ?? 'No data',
),
],
],
),
),
),
);
}
}