Horizontal ListView inside SliverAppBar - firebase

I am trying to add a Horizontal ListView.builder Inside a SliverAppBar, but every list item appears on top of each other like a traditional list (VERTICLE), when try to change the Scroll direction the whole list disappear.
this is the Items inside SAB
body: SingleChildScrollView(
child: Column(
children: [
CATS(),
Center(
child: Text('خانه های کرایی جدید'),
SizedBox(height: 5),
LatestForRent(),//TO BE SCROLLED HORIZONTAL
SizedBox(height: 5),
Center(
child: Text('خانه های فروشی جدید'),
SizedBox(height: 5),
LatestForSale(), //TO BE SCROLLED HORIZONTAL
SizedBox(height: 5),
Center(
child: Text('خانه های گروی جدید'),
SizedBox(height: 5),
LatestGerawi(),//TO BE SCROLLED HORIZONTAL
this is the List
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Sell')
.orderBy('TS', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null)
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.red,
valueColor: new AlwaysStoppedAnimation<Color>(Colors.cyan[400]),
),
);
return ListView.builder(
// scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: 2,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
height: 200,
width: 300,

Give an height to your ListView.
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Sell')
.orderBy('TS', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null)
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.red,
valueColor: new AlwaysStoppedAnimation<Color>(Colors.cyan[400]),
),
);
return Container(
height: 200,
child:ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: 2,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
height: 200,
width: 300,
)));

Related

Pass post details to new page (Firebase in Flutter)

Multi level Flutter Page
I need to add the functionality of pressing a post card and it loading a more detailed page with that posts information. So far I have developed the listview displaying the post cards and have wrapped them with an InkWell that pushes to the detailed page.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Friends')),
body: FutureBuilder(
future: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
shrinkWrap: true,
physics: const ScrollPhysics(),
itemCount: (snapshot.data! as dynamic).docs.length,
itemBuilder: (context, index) {
DocumentSnapshot snap =
(snapshot.data! as dynamic).docs[index];
return Container(
decoration: BoxDecoration(
color: Colors.grey.shade300,
borderRadius:
BorderRadius.all(Radius.circular(20))),
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
child: Row(
children: [
InkWell(
child: Container(
padding: EdgeInsets.all(10),
width: 140,
height: 140,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
child: Material(
child: Ink.image(
image:
NetworkImage(snap['postUrl']),
fit: BoxFit.cover,
child: InkWell(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context,) =>
DetailedPage(uid: FirebaseAuth.instance.currentUser!.uid)),
),
)),
))),
),
),

flutter on click download image from firebase

Here, when I add a new picture, it appears here and it can be opened to a full screen, but when I click I want to download the picture that I long pressed on to the phone storage
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('gallery').snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData ? Center(
child: CircularProgressIndicator(),
) : Container(
child: GridView.builder(
itemCount: snapshot.data!.docs.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3
),
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.all(3),
child: FullScreenWidget(
backgroundColor: Colors.white,
child: FadeInImage.assetNetwork(
fit: BoxFit.contain,
placeholder: 'assets/loading.gif',
imageErrorBuilder:
(context, error, stackTrace) {
return Image.asset(
'assets/error.gif',
fit: BoxFit.fitWidth);
},
image: snapshot.data!.docs[index].get('url')),
),
);
}),
);
},
);

snapshot.data.docs.length not working on flutter

I make some listview and it have likes and comments section. I want to show how many likes and comments that post had. The like section works by displaying the number of users who like it, i try to show it with snapshot.data.docs.length.toString() from firestore but when i try to show how many comments with same code as like section it not working and only get 0.
this is inside comments field
Padding(
padding: const EdgeInsets.only(top: 8),
child: Padding(
padding: const EdgeInsets.only(left: 15),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
Container(
width: 80.0,
child: Row(
mainAxisAlignment:
MainAxisAlignment.start,
children: [
GestureDetector(
onLongPress: () {
Provider.of<PostFunction>(context,
listen: false)
.showLikes(
context,
documentSnapshot
.data()['title']);
},
onTap: () {
print('Adding like...');
Provider.of<PostFunction>(context,
listen: false)
.addLike(
context,
documentSnapshot
.data()['title'],
Provider.of<AuthenticationService>(
context,
listen: false)
.getUserUid);
},
child: Icon(
FontAwesomeIcons.arrowAltCircleUp,
color: kGreyColor,
size: 18.0,
),
),
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('posts')
.doc(documentSnapshot
.data()['title'])
.collection('likes')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return Center(
child:
CircularProgressIndicator());
} else {
return Padding(
padding:
const EdgeInsets.only(
left: 8.0),
child: RichText(
text: TextSpan(
text: snapshot
.data.docs.length
.toString(),
style: GoogleFonts
.openSans(
color:
kGreyColor,
fontSize: 16.0),
children: <TextSpan>[
TextSpan(
text: ' votes',
style: GoogleFonts
.openSans(
color:
kGreyColor,
fontSize: 16.0,
)),
]),
),
);
}
})
],
),
),
SizedBox(width: 20),
Container(
width: 150.0,
child: Row(
mainAxisAlignment:
MainAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
Provider.of<PostFunction>(context,
listen: false)
.showComments(
context,
documentSnapshot,
documentSnapshot
.data()['title']);
},
child: Icon(
FontAwesomeIcons.solidComments,
color: kGreyColor,
size: 16.0,
),
),
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection(' posts')
.doc(documentSnapshot
.data()['title'])
.collection('comments')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return Center(
child:
CircularProgressIndicator());
} else {
return Padding(
padding:
const EdgeInsets.only(
left: 8.0),
child: Text(
snapshot.data.docs.length
.toString(),
style: GoogleFonts.openSans(
color: kGreyColor,
fontSize: 16.0),
),
);
}
}),
],
),
),
Spacer(),
Provider.of<AuthenticationService>(context,
listen: false)
.getUserUid ==
documentSnapshot.data()['useruid']
? IconButton(
icon: Icon(EvaIcons.moreVertical,
color: kGreyColor, size: 16),
onPressed: () {
Provider.of<PostFunction>(context,
listen: false)
.showPostOptions(
context,
documentSnapshot
.data()['title']);
})
: Container(width: 0.0, height: 0.0)
],
),
),
),
Replace
.collection(' posts')
with
.collection('posts')
in the “comments” section of the Streambuilder.
Your stream data is coming as empty as the database cannot find a collection with name (“ posts”).
So when you try to show how many comments the post had with the same code as the “likes” section it is not working and only getting 0 each time.
just declare the dataType of builder's arguments..i mean, builder:(BuildContext context,AsyncSnapshot snapShot).check the image
Could you please try this one?
StreamBuilder(
stream: firestoreDB,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
itemCount: snapshot.data!.size,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(snapshot.data!.docs[index]['name']),
subtitle: Text(snapshot.data!.docs[index]['description']),
),
);
},
);
},
),

How to get data from other collection in streambuilder

I am facing a problem and I cannot solve it, I have a collection "Posts" that contain users' posts, each post contains a uid of the owner of the post, and I have another collection named "users", and each user has his name and photo, I want to get the user's name and image to display on his post.
Posts Collection
Users Collection
I have seen this article but I did not benefit perhaps because I am a beginner.
My Code:
StreamBuilder(
stream: Firestore.instance
.collection('Posts')
.orderBy('date', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
const Text('Loading...');
} else {
return ListView.builder(
physics:
NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot
.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot mypost =
snapshot
.data.documents[index];
return Stack(
children: <Widget>[
Container(
decoration:
BoxDecoration(
borderRadius:
BorderRadius
.circular(
15.0),
),
child: Padding(
padding:
EdgeInsets
.all(8.0),
child: Column(
children: <
Widget>[
InkWell(
child: Row(
children: <
Widget>[
Container(
width:
32.0,
height:
32.0,
decoration: BoxDecoration(shape: BoxShape.circle, image: DecorationImage(fit: BoxFit.fill, image: NetworkImage(
// user image url
'Owner of post img')))),
SizedBox(
width:
10.0,
),
Text(
'Owner of post Name',
style: TextStyle(
color: Colors.white,
fontSize: 12.0,
fontWeight: FontWeight.bold,
fontFamily: 'BalooDa2'),
),
],
),
),
Padding(
padding:
EdgeInsets.all(
5.0),
child: Text(
'${mypost['text']}',
style: TextStyle(
color: Colors
.white,
fontSize:
16.0,
fontFamily:
'Tajawal',
height:
1.3),
textAlign:
TextAlign
.justify,
textDirection:
TextDirection.rtl),
),
],
),
),
),
],
);
});
}
return Container(
height: 0.0,
width: 0.0,
);
},
),
You can user nested StreamBuilders to fetch user detail from the uid you are getting from posts collection.I have added a query which will match uid from posts data and look for the documents in users collection where uid is equal to uid from posts collection.
StreamBuilder(
stream: Firestore.instance
.collection('Posts')
.orderBy('date', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
const Text('Loading...');
} else {
return ListView.builder(
physics:
NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot
.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot mypost =
snapshot
.data.documents[index];
return Stack(
children: <Widget>[
Container(
decoration:
BoxDecoration(
borderRadius:
BorderRadius
.circular(
15.0),
),
child: Padding(
padding:
EdgeInsets
.all(8.0),
child: Column(
children: <
Widget>[
StreamBuilder(
stream: Firestore.instance
.collection('users')
.where('uid', isEqualTo: mypost['uid'])
.snapshots(),
builder: (context, snapshot){
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return SizedBox();
case ConnectionState.active:
default:
break;
}
if (snapshot.hasError) print(snapshot.error);
DocumentSnapshot user = snapshot.data.documents[0];
return InkWell(
child: Row(
children: <
Widget>[
Container(
width:
32.0,
height:
32.0,
decoration: BoxDecoration(shape: BoxShape.circle, image: DecorationImage(fit: BoxFit.fill, image: NetworkImage(
// user image url
'${user['uimg']}')))),
SizedBox(
width:
10.0,
),
Text(
'${user['name']}',
style: TextStyle(
color: Colors.white,
fontSize: 12.0,
fontWeight: FontWeight.bold,
fontFamily: 'BalooDa2'),
),
],
),
);
},
),
Padding(
padding:
EdgeInsets.all(
5.0),
child: Text(
'${mypost['text']}',
style: TextStyle(
color: Colors
.white,
fontSize:
16.0,
fontFamily:
'Tajawal',
height:
1.3),
textAlign:
TextAlign
.justify,
textDirection:
TextDirection.rtl),
),
],
),
),
),
],
);
});
}
return Container(
height: 0.0,
width: 0.0,
);
},
),

How to call another table attribute with uid? - firebase flutter

My problem similar to this How to compare UID and another child key in Firebase?
I'm trying to create a similar code from that thread but it didn't work
This is my firebase https://imgur.com/ZpU8ODO
StreamBuilder(
stream: FirebaseDatabase.instance
.reference()
.child("questions")
.onValue,
builder: (BuildContext context, AsyncSnapshot<Event> snapshot) {
if (snapshot.hasData) {
Map<dynamic, dynamic> map = snapshot.data.snapshot.value;
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: MediaQuery.of(context).size.width / (MediaQuery.of(context).size.height / 4),
mainAxisSpacing: 16.0,
),
itemCount: map.values.toList().length,
padding: EdgeInsets.only(left: 16, right: 16, top: 16),
itemBuilder: (BuildContext context, int index) {
return Container(
color: bgSecondaryColor,
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Text(
parse(parse(map.values.toList()[index]["question"]).body.text).documentElement.text,
),
),
Container(
child: Column(
children: <Widget>[
CircleAvatar(
backgroundImage: NetworkImage('https://mdn.mozillademos.org/files/7693/catfront.png'),
backgroundColor: Colors.lightGreen,
radius: 24.0,
),
Text(
//I want to call the "name" attribute in users here,
),
],
),
),
],
),
],
),
);
},
Sorry im a total newbie in both flutter and firebase. Kind regards from me. Thank you

Resources