aboutsummaryrefslogtreecommitdiff
path: root/lib/screens/new_chat_screen.dart
blob: 554b9a9bc13859af99ffee0b78d37c884136e75c (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'package:flutter/material.dart';
import 'package:linkchat/firebase/database.dart';
import 'package:linkchat/models/group.dart';
import 'package:linkchat/widgets/cached_avatar.dart';

import '../firebase/auth.dart';
import '../models/user.dart';

class NewChatScreen extends StatefulWidget {
  const NewChatScreen({super.key});

  @override
  State<NewChatScreen> createState() => _NewChatScreenState();
}

class _NewChatScreenState extends State<NewChatScreen> {
  final Auth _auth = Auth();
  final Database _db = Database();
  final TextEditingController _controller = TextEditingController();
  final ValueNotifier<List<FsUser>> _usersNotifier =
      ValueNotifier<List<FsUser>>([]);
  final ValueNotifier<List<FsUser>> _filteredUsersNotifier =
      ValueNotifier<List<FsUser>>([]);

  @override
  void initState() {
    super.initState();
    _db.getAllUsers().first.then((u) {
      _usersNotifier.value = u;
      _filteredUsersNotifier.value = u;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Nuevo chat'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              controller: _controller,
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Nombre del contacto',
              ),
              onChanged: (value) {
                if (value.isNotEmpty) {
                  _filteredUsersNotifier.value = _usersNotifier.value
                      .where((user) => user.displayName.contains(value))
                      .toList();
                } else {
                  _filteredUsersNotifier.value = _usersNotifier.value;
                }
              },
            ),
          ),
          Expanded(
            child: ValueListenableBuilder(
              valueListenable: _filteredUsersNotifier,
              builder: (context, value, child) => ListView.builder(
                itemCount: value.length,
                itemBuilder: (context, index) {
                  FsUser user = value[index];
                  return ListTile(
                    leading: CachedAvatar(user.photoUrl),
                    title: Text(user.displayName),
                    trailing: IconButton(
                      icon: const Icon(Icons.send),
                      onPressed: () {
                        _db
                            .saveGroup(Group(
                          createdBy: _auth.currentUser!.uid,
                          createdAt: DateTime.now(),
                          members: [
                            _auth.currentUser!.uid,
                            user.uid,
                          ],
                        ))
                            .whenComplete(() {
                          Navigator.of(context).pop();
                        });
                      },
                    ),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}