2024-09-29 11:02:27 +02:00

252 lines
8.2 KiB
Dart

// ignore_for_file: unnecessary_brace_in_string_interps
import 'dart:io';
import 'dart:convert';
import 'package:covas_mobile/classes/alert.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
import '../variable/globals.dart' as globals;
import '../classes/events.dart';
import 'ListItemMenu.dart';
void main() {
initializeDateFormatting("fr_FR", null).then((_) => (const MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const ItemMenu(title: 'Flutter Demo Home Page'),
);
}
}
class ItemMenu extends StatefulWidget {
const ItemMenu({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<ItemMenu> createState() => _ItemMenuState();
}
class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
String listUser = "";
String eventName = "";
String eventStartDate = "";
String eventDescription = "";
String organizers = "";
String place = "";
String imgUrl = "";
Events? events;
@override
void initState() {
super.initState();
_getEventInfos();
}
Future<void> _getEventInfos() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
String formerName = "";
String formerDate = "";
String formerOrga = "";
String formerMap = "";
String formerImage = "";
String formerDesc = "";
if (accessToken.isNotEmpty) {
var urlGet = Uri.parse("${globals.api}/events/${widget.title}");
var responseGet = await http.get(urlGet,
headers: {HttpHeaders.cookieHeader: 'access_token=${accessToken}'});
stderr.writeln('Response Get status: ${responseGet.statusCode}');
if (responseGet.statusCode == 200) {
stderr.writeln('Username : ${responseGet.body}');
var events = jsonDecode(utf8.decode(responseGet.bodyBytes));
formerName = events["name"];
formerMap =
"${events["place"]} - ${events["zip_code"]} ${events["city"]} - ${events["country"]}";
formerDesc = events["description"];
final startDate = DateTime.parse(events["start_date"]);
final date = DateFormat.yMd().format(startDate);
final time = DateFormat.Hm().format(startDate);
final endDate = DateTime.parse(events["end_date"]);
final dateE = DateFormat.yMd().format(endDate);
final timeE = DateFormat.Hm().format(endDate);
if (events["imgUrl"] != null) {
formerImage = events["imgUrl"];
}
formerDate = "${date} ${time} à ${dateE} ${timeE}";
if (events["organizers"].length > 1) {
formerOrga = "${events['organizers'][0]}";
for (var i = 1; i < events["organizers"].length; i++) {
formerOrga = "${formerOrga}, ${events['organizers'][i]}";
}
} else {
formerOrga = "${events['organizers'][0]}";
}
} else {
var text = "";
switch (responseGet.statusCode) {
case 400:
{
text = "Requête mal construite";
}
break;
case 406:
{
text = "Mot de passe incorrect";
}
break;
case 404:
{
text = "Utilisateur inconnu";
}
break;
case 403:
{
text = "Vous n'avez pas l'autorisation de faire cette action";
}
break;
case 410:
{
text = "Token invalide";
}
break;
case 500:
{
text = "Probleme interne du serveur";
}
break;
default:
{
text = "Probleme d'authentification inconnu";
}
break;
}
showErrorDialog(context, text);
}
} else {
showErrorDialog(context, "Cache invalide");
}
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
eventName = formerName;
eventStartDate = formerDate;
organizers = formerOrga;
place = formerMap;
imgUrl = formerImage;
eventDescription = formerDesc;
});
}
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text("${eventName}"),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (_) => ListItemMenu()));
},
)),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 60.0),
child: Center(
child: Container(height: 250, child: Image.network(imgUrl)),
),
),
Row(
children: [
Icon(Icons.event),
Text("Date : ${eventStartDate}",
style: TextStyle(fontSize: 15.0))
],
),
Row(children: [
Icon(Icons.explore),
Flexible(
child: Text("Carte : ${place}",
style: TextStyle(fontSize: 15.0),
maxLines: 3,
overflow: TextOverflow.ellipsis))
]),
Row(children: [
Icon(Icons.group),
Text("Organisateurs : ${organizers}",
style: TextStyle(fontSize: 15.0))
]),
Row(children: [
Icon(Icons.description),
Flexible(
child: Text("Description : ${eventDescription}",
style: TextStyle(fontSize: 15.0),
maxLines: 3,
overflow: TextOverflow.ellipsis))
])
],
)));
}
}