Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:scoped_model/scoped_model.dart';
- import 'package:cached_network_image/cached_network_image.dart';
- import 'package:material_search/material_search.dart';
- import '.././custom_widget/profile_card_widget.dart';
- import 'package:firebase_storage/firebase_storage.dart';
- import 'package:cloud_firestore/cloud_firestore.dart';
- import 'dart:async';
- import '.././ui/chat_screen_2.dart';
- import '.././model/scoped_model.dart';
- class GroupsScreen extends StatefulWidget {
- @override
- _ChatScreenState createState() => _ChatScreenState();
- }
- class _ChatScreenState extends State<GroupsScreen>
- with AutomaticKeepAliveClientMixin<GroupsScreen> {
- @override
- bool get wantKeepAlive => true;
- final _scaffoldKey = new GlobalKey<ScaffoldState>();
- bool showBottomSheet = false;
- bool hasEmail = false;
- GlobalKey<FormState> _friendFormKey = GlobalKey();
- String searchFriend;
- bool emailExist = false;
- // Future<DocumentSnapshot> doesEmailExist() async {
- // var snapshot = await Firestore.instance
- // .collection('users')
- // .document('$searchFriend')
- // .get();
- // if (snapshot.exists) {
- // setState(() {
- // emailExist = true;
- // });
- // print("Email existes");
- // } else {
- // setState(() {
- // emailExist = false;
- // });
- // print("Email NOT existes");
- // }
- // return snapshot;
- // }
- void _persistentBottomSheet() {
- _scaffoldKey.currentState.showBottomSheet((context) {
- return new Container(
- color: Colors.white,
- height: 250.0,
- child: new Center(
- child: Container(
- padding: EdgeInsets.all(10.0),
- child: Column(
- children: <Widget>[
- TextField(
- decoration: InputDecoration(labelText: 'Add Friend'),
- ),
- Padding(padding: EdgeInsets.only(top: 5.0)),
- FlatButton(
- shape: OutlineInputBorder(
- borderRadius: BorderRadius.circular(20.0)),
- color: Colors.blue,
- onPressed: () {},
- child: Text(
- "Add",
- style: TextStyle(color: Colors.white),
- ),
- ),
- ],
- ))),
- );
- });
- }
- void _modalBottomSheet() {
- showModalBottomSheet(
- context: context,
- builder: (builder) {
- return new Container(
- color: Colors.yellowAccent,
- child: new Center(
- child: new Text("Hey guys !! it is a modal bottom sheet"),
- ),
- );
- });
- }
- @override
- Widget build(BuildContext context) {
- // super.build(context);
- return ScopedModelDescendant<MainModel>(
- builder: (BuildContext context, Widget child, MainModel model) {
- final double deviceWidth = MediaQuery
- .of(context)
- .size
- .width;
- final double deviceHeight = MediaQuery
- .of(context)
- .size
- .height;
- return DefaultTabController(
- length: 3,
- child: Scaffold(
- key: _scaffoldKey,
- // floatingActionButton: FloatingActionButton(
- // child: Icon(Icons.add),
- // onPressed: () {
- // setState(() {
- // showBottomSheet == true
- // ? showBottomSheet = false
- // : showBottomSheet = true;
- // });
- // if (showBottomSheet) {
- // _persistentBottomSheet();
- // } else {
- // Navigator.pop(context);
- // showBottomSheet = false;
- // }
- // }),
- appBar: AppBar(
- backgroundColor: Colors.amber,
- centerTitle: true,
- title: Text("Momo Chat"),
- actions: <Widget>[
- IconButton(
- icon: Icon(Icons.add),
- onPressed: () {
- _scaffoldKey.currentState.openEndDrawer();
- },
- ),
- ],
- bottom: TabBar(tabs: [
- Tab(icon: Icon(Icons.message)),
- Tab(icon: Icon(Icons.person)),
- Tab(icon: Icon(Icons.speaker_notes)),
- ]),
- ),
- drawer: Drawer(
- child: ListView(
- children: <Widget>[
- UserAccountsDrawerHeader(
- decoration: BoxDecoration(color: Colors.amber),
- accountName: Text(model.getUserModel.displayName),
- accountEmail: Text(model.getUserModel.email),
- currentAccountPicture: CircleAvatar(
- backgroundImage: CachedNetworkImageProvider(
- model.getUserModel.photoUrl),
- ),
- ),
- ListTile(
- trailing: Icon(Icons.person),
- title: Text("Account Info"),
- onTap: () => Navigator.pushNamed(context, '/accountInfo2'),
- ),
- ListTile(
- title: Text("Sign out"),
- trailing: Icon(Icons.exit_to_app),
- onTap: () {
- model.signOut().then((_) {
- Navigator.pushReplacementNamed(
- context, '/signInScreen');
- });
- },
- )
- ],
- ),
- ),
- // ******* SEND DRAWER *******
- endDrawer: Drawer(
- child: ListView(
- children: <Widget>[
- Container(
- width: 50.0,
- color: Colors.amber,
- padding: EdgeInsets.all(20.0),
- child: Center(
- child: Text(
- "Add Some Friends",
- style: TextStyle(
- fontWeight: FontWeight.bold,
- fontSize: 18.0,
- color: Colors.white),
- ),
- ),
- ),
- // ****** ADD FRIEND ACCOUNT INFO ******
- hasEmail
- ? model.getDoesEmailExistBool
- ? FutureBuilder(
- future: model.getDoesEmailExist(),
- builder: (context, snapshot) {
- // if(snapshot.connectionState == ConnectionState.waiting){
- // return Center(
- // child: Text("Loading..."),
- // );
- // }
- // return snapshot.hasData
- // ? ProfileCard(
- // name: snapshot.data['displayName']
- // .toString(),
- // email:
- // snapshot.data['email'].toString(),
- // bio: snapshot.data['bio'].toString(),
- // photoUrl: snapshot.data['photoUrl']
- // .toString())
- // : Center(
- // child: Container(
- // padding: EdgeInsets.all(20.0),
- // child: CircularProgressIndicator(),
- // ),
- // );
- switch (snapshot.connectionState) {
- case ConnectionState.waiting:
- return Center(
- child: Text("Loading..."),
- );
- case ConnectionState.done:
- return ProfileCard(
- name: snapshot.data['displayName']
- .toString(),
- email:
- snapshot.data['email'].toString(),
- bio: snapshot.data['bio'].toString(),
- photoUrl: snapshot.data['photoUrl']
- .toString());
- default:
- return Text("No User Found");
- }
- })
- : Center(
- child: Container(
- padding: EdgeInsets.all(20.0),
- child: Text("No Email exist"),
- ),
- )
- : Container(),
- hasEmail == false
- ? Container(
- padding: EdgeInsets.all(10.0),
- child: Form(
- key: _friendFormKey,
- child: TextFormField(
- onSaved: (value) {
- setState(() {
- searchFriend = value;
- });
- model.doesEmailExist(searchFriend);
- },
- decoration: InputDecoration(
- hintText: "enter email",
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(20.0),
- )),
- ),
- ),
- )
- : Container(),
- hasEmail
- ? Column(
- children: <Widget>[
- model.getDoesEmailExistBool
- ? Container(
- width: double.infinity,
- padding: EdgeInsets.all(10.0),
- child: FlatButton(
- shape: OutlineInputBorder(
- borderRadius:
- BorderRadius.circular(20.0)),
- color: Colors.blue,
- onPressed: () {
- model.addFriend();
- setState(() {
- hasEmail
- ? hasEmail = false
- : hasEmail = true;
- });
- },
- child: Text(
- 'Add!',
- style: TextStyle(
- color: Colors.white,
- ),
- ),
- ),
- )
- : Container(),
- Container(
- width: double.infinity,
- padding: EdgeInsets.all(10.0),
- child: FlatButton(
- shape: OutlineInputBorder(
- borderRadius: BorderRadius.circular(20.0)),
- color: Colors.blue,
- onPressed: () {
- setState(() {
- emailExist = false;
- hasEmail = false;
- });
- },
- child: Text(
- 'Search another',
- style: TextStyle(
- color: Colors.white,
- ),
- ),
- ),
- ),
- ],
- )
- : Container(
- padding: EdgeInsets.all(10.0),
- child: FlatButton(
- shape: OutlineInputBorder(
- borderRadius: BorderRadius.circular(20.0)),
- color: Colors.blue,
- onPressed: () {
- _friendFormKey.currentState.save();
- model.setDoesEmailExistFalse(false);
- setState(() {
- hasEmail ? hasEmail = false : hasEmail = true;
- });
- },
- child: Text(
- 'Search!',
- style: TextStyle(
- color: Colors.white,
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- body: TabBarView(children: [
- // ****** CHATS ********
- StreamBuilder<QuerySnapshot>(
- stream: Firestore.instance
- .collection('user friends list')
- .document(model.getUserModel.email)
- .collection('friends')
- .orderBy('email', descending: true)
- .limit(20)
- .snapshots(),
- builder: (BuildContext context,
- AsyncSnapshot<QuerySnapshot> snapshot) {
- if (!snapshot.hasData) return Container();
- return ListView.builder(
- itemCount: snapshot.data.documents.length,
- itemBuilder: (BuildContext context, int index) {
- String friendEmail = snapshot
- .data.documents[index].data['email']
- .toString();
- String groupChatId;
- readLocal() {
- if (model.getUserModel.email.hashCode <=
- friendEmail.hashCode) {
- groupChatId =
- '$friendEmail and ${model.getUserModel.email}';
- print(" GROUP CHAT ID $groupChatId");
- } else {
- groupChatId =
- '${model.getUserModel.email} and $friendEmail';
- print(" GROUP CHAT ID $groupChatId");
- }
- }
- readLocal();
- return GestureDetector(
- onTap: () {
- model.currentChat('$friendEmail');
- Navigator.pushNamed(context, '/chatScreen');
- },
- child: Container(
- color: Colors.transparent,
- width: double.infinity,
- child: Row(
- children: <Widget>[
- Container(
- padding: EdgeInsets.all(10.0),
- child: getFriendPhoto(friendEmail, deviceWidth),
- ),
- Flexible(
- child: Container(
- padding: EdgeInsets.all(10.0),
- child: Column(
- crossAxisAlignment:
- CrossAxisAlignment.start,
- children: <Widget>[
- Container(
- padding: EdgeInsets.only(
- top: 5.0, bottom: 2.0),
- child: getFriendDisplayName(
- friendEmail),
- ),
- Container(
- padding: EdgeInsets.only(bottom: 5.0),
- child: getLatestMessage(groupChatId),
- ),
- ],
- ),
- ),
- ),
- ],
- ),
- ),
- );
- },
- );
- },
- ),
- // ****** FRIEND LIST ******
- StreamBuilder<QuerySnapshot>(
- stream: Firestore.instance
- .collection('user friends list')
- .document(model.getUserModel.email)
- .collection('friends')
- .orderBy('email', descending: true)
- .limit(20)
- .snapshots(),
- builder: (BuildContext context,
- AsyncSnapshot<QuerySnapshot> snapshot) {
- if (!snapshot.hasData) return Container();
- return ListView.builder(
- itemCount: snapshot.data.documents.length,
- itemBuilder: (BuildContext context, int index) {
- String friendEmail = snapshot
- .data.documents[index].data['email']
- .toString();
- return StreamBuilder(
- stream: Firestore.instance
- .collection('users')
- .document(friendEmail)
- .snapshots(),
- builder: (context, snapshot) {
- switch (snapshot.connectionState) {
- case ConnectionState.none:
- return Text("none");
- case ConnectionState.waiting:
- return Container();
- default:
- if (snapshot.hasData) {
- return friendInfoListTile(
- snapshot, model, deviceHeight);
- } else {
- return Text("No data");
- }
- }
- },
- );
- },
- );
- },
- ),
- Center(
- child: Text("This Function Is Coming Soon!"),
- ),
- ]),
- ),
- );
- },
- );
- }
- Widget friendInfoListTile(snapshot, model, deviceHeight) {
- return GestureDetector(
- onTap: () {
- setState(() {
- model.currentChat('${snapshot.data['email']}');
- });
- Navigator.pushNamed(context, '/chatScreen');
- },
- child: Container(
- color: Colors.transparent,
- width: double.infinity,
- padding: EdgeInsets.symmetric(horizontal: 5.0),
- child: Row(
- children: <Widget>[
- Container(
- padding: EdgeInsets.all(5.0),
- child: CircleAvatar(
- radius: deviceHeight / 32,
- backgroundImage:
- CachedNetworkImageProvider('${snapshot.data['photoUrl']}'),
- ),
- ),
- Flexible(
- child: Container(
- padding: EdgeInsets.all(10.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Container(
- padding: EdgeInsets.only(top: 5.0, bottom: 2.0),
- child: Text(
- '${snapshot.data['displayName']}',
- style: TextStyle(
- // fontWeight: FontWeight.bold,
- fontSize: 17.0),
- overflow: TextOverflow.ellipsis,
- )),
- Container(
- padding: EdgeInsets.only(bottom: 5.0),
- child: Text('${snapshot.data['email']}'),
- ),
- ],
- ),
- ),
- )
- ],
- ),
- ),
- );
- }
- Widget getLatestMessage(groupChatId) {
- return StreamBuilder(
- stream: Firestore.instance
- .collection('messages')
- .document(groupChatId)
- .collection(groupChatId)
- .orderBy('sentOn', descending: true)
- .limit(1)
- .snapshots(),
- builder: (context, snapshot) {
- try {
- if (snapshot.data.documents[0] == null) {
- return Text("No Data");
- } else {
- if (snapshot.data.documents[0].data['type'] == '0') {
- String message = snapshot.data.documents[0].data['message']
- .replaceAll("\n", " ");
- return Text(
- message.toString(),
- overflow: TextOverflow.ellipsis,
- style: TextStyle(
- fontSize: 15.0
- ),
- );
- } else {
- return Text("Image");
- }
- }
- } catch (e) {
- if (snapshot.connectionState == ConnectionState.waiting) {
- return Text(" ");
- }
- return Text("No Message Yet!");
- }
- },
- );
- }
- Widget getFriendDisplayName(friendEmail) {
- return StreamBuilder(
- stream: Firestore.instance
- .collection('users')
- .document(friendEmail)
- .snapshots(),
- builder: (context, snapshot) {
- switch (snapshot.connectionState) {
- case ConnectionState.none:
- return Text("none");
- case ConnectionState.waiting:
- return Container();
- default:
- if (snapshot.hasData) {
- return Text("${snapshot.data['displayName']}", style: TextStyle(fontSize: 18.0),);
- } else {
- return Text("No data");
- }
- }
- },
- );
- }
- Widget getFriendPhoto(friendEmail, deviceWidth) {
- return StreamBuilder(
- stream: Firestore.instance
- .collection('users')
- .document(friendEmail)
- .snapshots(),
- builder: (context, snapshot) {
- switch (snapshot.connectionState) {
- case ConnectionState.none:
- return Text("none");
- case ConnectionState.waiting:
- return Container();
- default:
- if (snapshot.hasData) {
- return CircleAvatar(
- radius: deviceWidth / 15,
- backgroundImage:
- CachedNetworkImageProvider(snapshot.data['photoUrl']),
- );
- } else {
- return Text("No data");
- }
- }
- },
- );
- }
- Future<Map<String, String>> future(friendEmail) async {
- DocumentSnapshot snapshot = await Firestore.instance
- .collection('users')
- .document('$friendEmail')
- .get();
- String email = snapshot.data['email'];
- String displayName = snapshot.data['displayName'];
- String photoUrl = snapshot.data['photoUrl'];
- Map<String, String> friendData = {
- 'email': email,
- 'displayName': displayName,
- 'photoUrl': photoUrl,
- };
- return friendData;
- }
- Widget userListTileBasic(friendEmail) {
- return ListTile(
- title: Text('$friendEmail'),
- );
- }
- }
- class friendProfilePic extends StatefulWidget {
- String friendEmail;
- friendProfilePic(this.friendEmail);
- @override
- _friendProfilePicState createState() => _friendProfilePicState();
- }
- class _friendProfilePicState extends State<friendProfilePic> {
- @override
- Widget build(BuildContext context) {
- return StreamBuilder(
- stream: Firestore.instance
- .collection('users')
- .document(widget.friendEmail)
- .snapshots(),
- builder: (context, snapshot) {
- switch (snapshot.connectionState) {
- case ConnectionState.none:
- return Text("none");
- case ConnectionState.waiting:
- return Container();
- default:
- if (snapshot.hasData) {
- return CircleAvatar(
- backgroundImage:
- CachedNetworkImageProvider(snapshot.data['photoUrl']),
- );
- } else {
- return Text("No data");
- }
- }
- },
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement