| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import 'package:flutter/material.dart';
- import 'package:physigo/friends/services/challenge_location_service.dart';
- import 'package:physigo/friends/widgets/add_friend.dart';
- import '../models/friend.dart';
- import '../services/friends_service.dart';
- class FriendsList extends StatefulWidget {
- const FriendsList({Key? key}) : super(key: key);
- @override
- State<FriendsList> createState() => _FriendsListState();
- }
- class _FriendsListState extends State<FriendsList> {
- void _shareLocation(String friendId) async {
- try {
- await ChallengeLocationService.shareChallengeLocation(friendId);
- _showSnackBar("Challenge's location shared!", Colors.green);
- } catch (error) {
- _showSnackBar(error.toString(), Colors.red);
- }
- }
- void _showSnackBar(String message, Color color) {
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- content: Text(message),
- backgroundColor: color,
- duration: const Duration(milliseconds: 1500),
- ));
- }
- @override
- Widget build(BuildContext context) {
- return Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const AddFriend(),
- FutureBuilder<List<Friend>>(
- future: FriendsService.getFriends(),
- builder: (context, snapshot) {
- if (!snapshot.hasData) {
- return const CircularProgressIndicator();
- }
- final friends = snapshot.data!;
- return ListView.builder(
- shrinkWrap: true,
- itemBuilder: ((context, index) => _friendTile(friends[index])),
- itemCount: friends.length,
- );
- },
- ),
- ],
- );
- }
- Widget _friendTile(Friend friend) {
- return Card(
- child: ListTile(
- title: Text(
- "${friend.name} ${friend.surname}",
- textAlign: TextAlign.center,
- style: const TextStyle(fontSize: 18),
- ),
- subtitle: TextButton(
- child: const Text("Share challenge location"),
- onPressed: () => _shareLocation(friend.id),
- ),
- ),
- );
- }
- }
|