aboutsummaryrefslogtreecommitdiff
path: root/lib/screens
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2023-05-22 00:09:43 -0600
committerIván Ávalos <avalos@disroot.org>2023-05-22 00:09:43 -0600
commit786267d0ebb337ad5b4f1e528fdd4c23731e0606 (patch)
tree6e696aeae7fe14244b555da9815ecd19d0b92b14 /lib/screens
parent41b1ed94d6964e3fcedf427bbd323861b03fd1de (diff)
downloadlinkchat-786267d0ebb337ad5b4f1e528fdd4c23731e0606.tar.gz
linkchat-786267d0ebb337ad5b4f1e528fdd4c23731e0606.tar.bz2
linkchat-786267d0ebb337ad5b4f1e528fdd4c23731e0606.zip
Se implementa funcionalidad básica de chat
Diffstat (limited to 'lib/screens')
-rw-r--r--lib/screens/chat_screen.dart97
-rw-r--r--lib/screens/dashboard_screen.dart6
-rw-r--r--lib/screens/new_chat_screen.dart99
3 files changed, 200 insertions, 2 deletions
diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart
new file mode 100644
index 0000000..d67e469
--- /dev/null
+++ b/lib/screens/chat_screen.dart
@@ -0,0 +1,97 @@
+import 'package:flutter/material.dart';
+import 'package:linkchat/firebase/auth.dart';
+import 'package:linkchat/firebase/database.dart';
+import 'package:linkchat/models/group.dart';
+
+import '../models/message.dart';
+import '../models/user.dart';
+import '../widgets/chat_bottom_sheet.dart';
+import '../widgets/chat_bubble.dart';
+
+class ChatScreen extends StatefulWidget {
+ const ChatScreen({super.key});
+
+ @override
+ State<ChatScreen> createState() => _ChatScreenState();
+}
+
+class _ChatScreenState extends State<ChatScreen> {
+ bool init = false;
+ Group? group;
+ FsUser? user;
+ final Auth _auth = Auth();
+ final Database _db = Database();
+
+ @override
+ Widget build(BuildContext context) {
+ List<dynamic> arguments =
+ ModalRoute.of(context)?.settings.arguments as List<dynamic>;
+ group = arguments[0] as Group;
+ user = arguments[1] as FsUser;
+
+ return Scaffold(
+ appBar: PreferredSize(
+ preferredSize: const Size.fromHeight(70.0),
+ child: Padding(
+ padding: const EdgeInsets.only(top: 5),
+ child: AppBar(
+ leadingWidth: 30,
+ title: Row(
+ children: [
+ ClipRRect(
+ borderRadius: BorderRadius.circular(30),
+ child: CircleAvatar(
+ backgroundImage: NetworkImage(user!.photoUrl),
+ )),
+ Padding(
+ padding: const EdgeInsets.only(left: 10),
+ child: Text(user!.displayName),
+ ),
+ ],
+ ),
+ actions: const [
+ Padding(
+ padding: EdgeInsets.only(right: 10),
+ child: Icon(
+ Icons.more_vert,
+ ),
+ )
+ ],
+ ),
+ ),
+ ),
+ body: StreamBuilder(
+ stream: _db.getMessagesByGroupId(group!.id!),
+ builder: (context, snapshot) {
+ if (snapshot.hasData) {
+ List<Message> msgs = snapshot.data!;
+ return ListView.builder(
+ itemCount: snapshot.data!.length,
+ itemBuilder: (context, index) => ChatBubble(
+ msgs[index].messageText,
+ alignment: msgs[index].sentBy == _auth.currentUser?.uid
+ ? ChatBubbleAlignment.end
+ : ChatBubbleAlignment.start,
+ ),
+ );
+ } else if (snapshot.hasError) {
+ print('Error: ${snapshot.error}');
+ return const Center(child: Text('Hubo un error'));
+ }
+ return const Center(child: CircularProgressIndicator());
+ },
+ ),
+ bottomSheet: ChatBottomSheet(
+ onPressed: (value) {
+ _db.saveMessage(
+ Message(
+ messageText: value,
+ sentBy: _auth.currentUser!.uid,
+ sentAt: DateTime.now(),
+ ),
+ group!.id!);
+ },
+ ),
+ );
+ }
+}
diff --git a/lib/screens/dashboard_screen.dart b/lib/screens/dashboard_screen.dart
index cb31a58..778c2bb 100644
--- a/lib/screens/dashboard_screen.dart
+++ b/lib/screens/dashboard_screen.dart
@@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
import '../firebase/auth.dart';
import '../providers/theme_provider.dart';
import '../settings/themes.dart';
+import '../widgets/recent_chats.dart';
class DashboardScreen extends StatefulWidget {
const DashboardScreen({super.key});
@@ -30,8 +31,9 @@ class _DashboardScreenState extends State<DashboardScreen> {
SliverAppBar.large(
title: const Text('Inicio'),
),
- const SliverFillRemaining(
- child: Placeholder(),
+ SliverFillRemaining(
+ hasScrollBody: true,
+ child: RecentChats(),
),
],
),
diff --git a/lib/screens/new_chat_screen.dart b/lib/screens/new_chat_screen.dart
new file mode 100644
index 0000000..7b7721f
--- /dev/null
+++ b/lib/screens/new_chat_screen.dart
@@ -0,0 +1,99 @@
+import 'package:flutter/material.dart';
+import 'package:linkchat/firebase/database.dart';
+import 'package:linkchat/models/group.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();
+ List<FsUser> users = [];
+ List<FsUser> filteredUsers = [];
+
+ @override
+ void initState() {
+ super.initState();
+ _db.getAllUsers().first.then((u) {
+ setState(() {
+ users = u;
+ filteredUsers = 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: [
+ TextField(
+ controller: _controller,
+ decoration: const InputDecoration(
+ border: OutlineInputBorder(),
+ labelText: 'Nombre del contacto',
+ ),
+ onChanged: (value) {
+ setState(() {
+ if (value.isNotEmpty) {
+ filteredUsers = users
+ .where((user) => user.displayName.contains(value))
+ .toList();
+ } else {
+ filteredUsers = users;
+ }
+ });
+ },
+ ),
+ ListView.builder(
+ shrinkWrap: true,
+ itemCount: filteredUsers.length,
+ itemBuilder: (context, index) {
+ FsUser user = filteredUsers[index];
+ return ListTile(
+ leading: CircleAvatar(
+ backgroundImage: NetworkImage(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();
+ });
+ },
+ ),
+ );
+ },
+ ),
+ ],
+ ),
+ );
+ }
+}