friends_list.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'package:flutter/material.dart';
  2. import 'package:physigo/friends/services/challenge_location_service.dart';
  3. import 'package:physigo/friends/widgets/add_friend.dart';
  4. import '../models/friend.dart';
  5. import '../services/friends_service.dart';
  6. class FriendsList extends StatefulWidget {
  7. const FriendsList({Key? key}) : super(key: key);
  8. @override
  9. State<FriendsList> createState() => _FriendsListState();
  10. }
  11. class _FriendsListState extends State<FriendsList> {
  12. void _shareLocation(String friendId) async {
  13. try {
  14. await ChallengeLocationService.shareChallengeLocation(friendId);
  15. _showSnackBar("Challenge's location shared!", Colors.green);
  16. } catch (error) {
  17. _showSnackBar(error.toString(), Colors.red);
  18. }
  19. }
  20. void _showSnackBar(String message, Color color) {
  21. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  22. content: Text(message),
  23. backgroundColor: color,
  24. duration: const Duration(milliseconds: 1500),
  25. ));
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Column(
  30. mainAxisAlignment: MainAxisAlignment.center,
  31. children: [
  32. const AddFriend(),
  33. FutureBuilder<List<Friend>>(
  34. future: FriendsService.getFriends(),
  35. builder: (context, snapshot) {
  36. if (!snapshot.hasData) {
  37. return const CircularProgressIndicator();
  38. }
  39. final friends = snapshot.data!;
  40. return ListView.builder(
  41. shrinkWrap: true,
  42. itemBuilder: ((context, index) => _friendTile(friends[index])),
  43. itemCount: friends.length,
  44. );
  45. },
  46. ),
  47. ],
  48. );
  49. }
  50. Widget _friendTile(Friend friend) {
  51. return Card(
  52. child: ListTile(
  53. title: Text(
  54. "${friend.name} ${friend.surname}",
  55. textAlign: TextAlign.center,
  56. style: const TextStyle(fontSize: 18),
  57. ),
  58. subtitle: TextButton(
  59. child: const Text("Share challenge location"),
  60. onPressed: () => _shareLocation(friend.id),
  61. ),
  62. ),
  63. );
  64. }
  65. }