add toggle interested in list organizer and tag
This commit is contained in:
@@ -94,6 +94,30 @@ class _MyHomePageState extends State<ListItemOrganizers> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>> toggleInterested(String eventId) async {
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
var accessToken = prefs.getString("access_token") ?? "";
|
||||||
|
final url = Uri.parse("${globals.api}/events/${eventId}/interest");
|
||||||
|
if (accessToken.isNotEmpty) {
|
||||||
|
final response = await http.post(
|
||||||
|
url,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw (AppLocalizations.of(context)?.toogle_interest ??
|
||||||
|
"Error toogle interest: ${response.statusCode}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var event = json.decode(response.body);
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
return {"interested": false, "interested_count": 0};
|
||||||
|
}
|
||||||
|
|
||||||
// variable to call and store future list of posts
|
// variable to call and store future list of posts
|
||||||
|
|
||||||
// function to fetch data from api and return future list of posts
|
// function to fetch data from api and return future list of posts
|
||||||
@@ -190,10 +214,39 @@ class _MyHomePageState extends State<ListItemOrganizers> {
|
|||||||
|
|
||||||
final dateLongue =
|
final dateLongue =
|
||||||
DateFormat('EEEE d MMMM y', locale).format(startDate);
|
DateFormat('EEEE d MMMM y', locale).format(startDate);
|
||||||
|
final countInterestedString =
|
||||||
|
AppLocalizations.of(context)?.count_interested ??
|
||||||
|
"Interested people number";
|
||||||
|
final countInterested =
|
||||||
|
"${countInterestedString} : ${post.interestedCount}";
|
||||||
|
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text('${post.name!}'),
|
title: Text('${post.name!}'),
|
||||||
subtitle: Text('${post.place!}\n${dateLongue}'),
|
subtitle:
|
||||||
|
Text('${post.place!}\n${dateLongue}\n${countInterested}'),
|
||||||
|
trailing: IconButton(
|
||||||
|
onPressed: () async {
|
||||||
|
try {
|
||||||
|
final result = await toggleInterested(post.id!);
|
||||||
|
setState(() {
|
||||||
|
post.interested = result["interested"];
|
||||||
|
post.interestedCount = result["interested_count"];
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
AppLocalizations.of(context)?.error_update ??
|
||||||
|
"Error when updating")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
icon: Icon(
|
||||||
|
post.interested ?? false
|
||||||
|
? Icons.favorite
|
||||||
|
: Icons.favorite_border,
|
||||||
|
color:
|
||||||
|
post.interested ?? false ? Colors.red : Colors.grey)),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
|
@@ -121,6 +121,30 @@ class _MyHomePageState extends State<ListItemTags> {
|
|||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>> toggleInterested(String eventId) async {
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
var accessToken = prefs.getString("access_token") ?? "";
|
||||||
|
final url = Uri.parse("${globals.api}/events/${eventId}/interest");
|
||||||
|
if (accessToken.isNotEmpty) {
|
||||||
|
final response = await http.post(
|
||||||
|
url,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw (AppLocalizations.of(context)?.toogle_interest ??
|
||||||
|
"Error toogle interest: ${response.statusCode}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var event = json.decode(response.body);
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
return {"interested": false, "interested_count": 0};
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@@ -168,6 +192,7 @@ class _MyHomePageState extends State<ListItemTags> {
|
|||||||
Widget buildPosts(List<Events> posts) {
|
Widget buildPosts(List<Events> posts) {
|
||||||
// ListView Builder to show data in a list
|
// ListView Builder to show data in a list
|
||||||
String tag = AppLocalizations.of(context)?.item_tags ?? "Tags : ";
|
String tag = AppLocalizations.of(context)?.item_tags ?? "Tags : ";
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
// Here we take the value from the MyHomePage object that was created by
|
// Here we take the value from the MyHomePage object that was created by
|
||||||
@@ -191,9 +216,39 @@ class _MyHomePageState extends State<ListItemTags> {
|
|||||||
final dateLongue =
|
final dateLongue =
|
||||||
DateFormat('EEEE d MMMM y', locale).format(startDate);
|
DateFormat('EEEE d MMMM y', locale).format(startDate);
|
||||||
|
|
||||||
|
final countInterestedString =
|
||||||
|
AppLocalizations.of(context)?.count_interested ??
|
||||||
|
"Interested people number";
|
||||||
|
final countInterested =
|
||||||
|
"${countInterestedString} : ${post.interestedCount}";
|
||||||
|
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text('${post.name!}'),
|
title: Text('${post.name!}'),
|
||||||
subtitle: Text('${post.place!}\n${dateLongue}'),
|
subtitle:
|
||||||
|
Text('${post.place!}\n${dateLongue}\n${countInterested}'),
|
||||||
|
trailing: IconButton(
|
||||||
|
onPressed: () async {
|
||||||
|
try {
|
||||||
|
final result = await toggleInterested(post.id!);
|
||||||
|
setState(() {
|
||||||
|
post.interested = result["interested"];
|
||||||
|
post.interestedCount = result["interested_count"];
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
AppLocalizations.of(context)?.error_update ??
|
||||||
|
"Error when updating")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
icon: Icon(
|
||||||
|
post.interested ?? false
|
||||||
|
? Icons.favorite
|
||||||
|
: Icons.favorite_border,
|
||||||
|
color:
|
||||||
|
post.interested ?? false ? Colors.red : Colors.grey)),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
|
Reference in New Issue
Block a user