aboutsummaryrefslogtreecommitdiff
path: root/lib/widgets/recent_chats.dart
blob: b83b5e54ecc6acea7ef11c9276b9dbfbf459e538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import 'package:flutter/material.dart';
import 'package:linkchat/firebase/database.dart';

import '../firebase/auth.dart';
import '../models/group.dart';
import '../widgets/chat_item.dart';

class RecentChats extends StatelessWidget {
  RecentChats({super.key});

  final Auth _auth = Auth();
  final Database _db = Database();

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 20),
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 25),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(35),
            topRight: Radius.circular(35),
          ),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              blurRadius: 10,
              spreadRadius: 2,
              offset: const Offset(0, 2),
            ),
          ]),
      child: StreamBuilder(
          stream: _db.getGroupsByUserID(_auth.currentUser!.uid),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                padding: const EdgeInsets.only(bottom: 100.0),
                itemCount: snapshot.data!.length,
                itemBuilder: (context, index) {
                  Group group = snapshot.data![index];
                  return FutureBuilder(
                      future: _db.getUserById(group.members.firstWhere(
                          (m) => m != _auth.currentUser!.uid,
                          orElse: () => group.createdBy)),
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          return ChatItem(
                            title: snapshot.data!.displayName,
                            subtitle: group.recentMessage?.messageText ?? "",
                            timestamp:
                                group.recentMessage?.sentAt ?? DateTime.now(),
                            avatarURL: snapshot.data!.photoUrl,
                            onTap: () {
                              Navigator.of(context).pushNamed(
                                '/chat',
                                arguments: [group, snapshot.data!],
                              );
                            },
                          );
                        } else if (snapshot.hasError) {
                          return ChatItem(
                            title: "Usuario desconocido",
                            subtitle: group.recentMessage?.messageText ?? "",
                            timestamp: DateTime.now(),
                            onTap: () {
                              Navigator.of(context).pushNamed(
                                '/chat',
                                arguments: [group, snapshot.data!],
                              );
                            },
                          );
                        }
                        return const Center(child: CircularProgressIndicator());
                      });
                },
              );
            } else if (snapshot.hasError) {
              print("Error: ${snapshot.error}");
              return const Center(child: Text('Hubo un error'));
            }
            return const Center(child: CircularProgressIndicator());
          }),
    );
  }
}