smilet/lib/Aktivitet/Participate.dart
2024-05-22 17:31:56 +02:00

229 lines
8.1 KiB
Dart

// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, file_names
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:smilet/LoginPage.dart';
import 'package:smilet/MainMenu/Meny.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:smilet/User.dart';
Future<List> _getActivities(String schoolCode) async {
var response = await http.get(Uri.parse(
'https://group-15-3.pvt.dsv.su.se/activity/available?schoolCode=${schoolCode}&username=${user.getUsername()}'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
debugPrint("YES");
return jsonDecode(response.body) as List<dynamic>;
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
debugPrint("NO");
debugPrint(response.statusCode.toString());
throw Exception();
}
}
Future<void> _updateActivity(int id, String username) async {
var response = await http.get(Uri.parse(
'https://group-15-3.pvt.dsv.su.se/activity/update?id=${id}&username=${username}'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
debugPrint("YES");
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
debugPrint("NO");
debugPrint(response.statusCode.toString());
debugPrint(username);
throw Exception();
}
}
class Participate extends StatefulWidget {
final User user;
const Participate({super.key, required this.user});
@override
_ParticipateState createState() => _ParticipateState();
}
class _ParticipateState extends State<Participate> {
@override
Widget build(BuildContext context) {
var futureBuilder = FutureBuilder(
future: _getActivities(user.getSchoolCode().toString()),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('connectionstate none');
case ConnectionState.waiting:
return _emptyActivities(context, snapshot);
default:
if (snapshot.hasError) {
return Text('snapshot error' + snapshot.error.toString());
} else if (snapshot.hasData) {
return _generateActivities(context, snapshot);
} else {
return Text('snapshot null');
}
}
},
);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Image.asset(
'assets/images/ml_logo.png',
height: 40,
),
),
body: Center(
child: Column(children: <Widget>[
SizedBox(height: 15),
Text(
'${schoolDetails["institution"].toString().toUpperCase()}s aktiviteter',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
)),
Expanded(child: futureBuilder),
])));
}
Widget _generateActivities(BuildContext context, AsyncSnapshot snapshot) {
List<dynamic> items = snapshot.data;
if (items.isEmpty) {
return Center(
child: Text("Inga kommande aktiviter!"),
);
} else {
return ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: items.length + 1,
itemBuilder: (BuildContext context, int index) {
if (index == 0) {
return Container();
} else {
Map map = items[index - 1];
int id = map["id"];
String title = map["title"].toString();
String place = map["place"].toString();
String time = map["time"].toString();
String date = map["date"].toString();
String creatorUser = map["creatorUser"].toString();
int amountRegistered = map["amountRegistered"];
return Container(
decoration: BoxDecoration(
color: Color.fromARGB(255, 59, 154, 233),
borderRadius: BorderRadius.all(Radius.circular(20))),
padding: const EdgeInsets.all(8),
height: 170,
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Text(
title,
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
Padding(
padding: const EdgeInsets.only(right: 20),
child: Text(
date,
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: Text(
place,
textAlign: TextAlign.left,
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
Padding(
padding: const EdgeInsets.only(right: 20),
child: Text(
time,
textAlign: TextAlign.right,
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: Text(
"Skapare: " + creatorUser,
textAlign: TextAlign.left,
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
Padding(
padding: const EdgeInsets.only(right: 20),
child: Text(
"Antal deltagare: " + amountRegistered.toString(),
textAlign: TextAlign.right,
style: TextStyle(fontSize: 15, color: Colors.white),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: ElevatedButton(
child: Text(
"Anmäl",
style: TextStyle(color: Colors.white),
),
style: TextButton.styleFrom(
backgroundColor: Color.fromARGB(255, 2, 205, 93),
),
onPressed: () async {
await _updateActivity(id, user.getUsername());
setState(() {});
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Meny(
user: user,
schoolDetails: schoolDetails,
)));
}),
),
],
)
],
),
);
}
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
);
}
}
Widget _emptyActivities(BuildContext context, AsyncSnapshot snapshot) {
return Center(child: CircularProgressIndicator());
}
}