I have created one application in which all the group names are fetched. Now, I want the functionality in which if I click on group name "Technical", I will be redirected to UI Page of Technical group. When I click on group name "Medical", I will be redirected to UI Page of Medical group and in similar manner. UI of all the pages are different. I have used firebase as backend. I am new to flutter and will be very much grateful to you if you can help me.
Source code of group_chat_screen.dart:
import 'package:chat_app/group_chats/create_group/add_members.dart';
import 'package:chat_app/group_chats/group_chat_room.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class GroupChatHomeScreen extends StatefulWidget {
const GroupChatHomeScreen({Key? key}) : super(key: key);
@override
_GroupChatHomeScreenState createState() => _GroupChatHomeScreenState();
}
class _GroupChatHomeScreenState extends State<GroupChatHomeScreen> {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
bool isLoading = true;
List groupList = [];
@override
void initState() {
super.initState();
getAvailableGroups();
}
void getAvailableGroups() async {
String uid = _auth.currentUser!.uid;
await _firestore
.collection('users')
.doc(uid)
.collection('groups')
.get()
.then((value) {
setState(() {
groupList = value.docs;
isLoading = false;
});
});
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: Text("Groups"),
),
body: isLoading
? Container(
height: size.height,
width: size.width,
alignment: Alignment.center,
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: groupList.length,
itemBuilder: (context, index) {
return ListTile(
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => GroupChatRoom(
groupName: groupList[index]['name'],
groupChatId: groupList[index]['id'],
),
),
),
leading: Icon(Icons.group),
title: Text(groupList[index]['name']),
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.create),
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => AddMembersInGroup(),
),
),
tooltip: "Create Group",
),
);
}
}
Source code of group_chat_room.dart:
import 'package:chat_app/group_chats/group_info.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class GroupChatRoom extends StatelessWidget {
final String groupChatId, groupName;
GroupChatRoom({required this.groupName, required this.groupChatId, Key? key})
: super(key: key);
final TextEditingController _message = TextEditingController();
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: Text(groupName),
actions: [
IconButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => GroupInfo(
groupName: groupName,
groupId: groupChatId,
),
),
),
icon: Icon(Icons.more_vert)),
],
),
);
}
}
CodePudding user response:
it' very easy just check your group name in onTap and according to your group name use if else condition and navigate to that screen:
ListView.builder(
itemCount: groupList.length,
itemBuilder: (context, index) {
return ListTile(
onTap: () {
if(groupList[index]['name']=="Technical"){
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => TechnicalChatRoom(
groupName: groupList[index]['name'],
groupChatId: groupList[index]['id'],
),
),
}
if(groupList[index]['name']=="Arts"){
MaterialPageRoute(
builder: (_) => ArtsChatRoom(
groupName: groupList[index]['name'],
groupChatId: groupList[index]['id'],
),
),
and so on
.......
...
}
),}
,
leading: Icon(Icons.group),
title: Text(groupList[index]['name']),
);
},
),