How to hide a single item from a stream builder - firebase

Hi I have a stream builder that lists containers with a button to count down and when the counter hits zero I wanted to hide that single container from the list I can’t figure out how to, if anyone does please help
bool visibility = true;
Widget _dataList() {
if (data != null) {
return StreamBuilder(
stream: data,
builder: (context, snapshot) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
padding: EdgeInsets.all(5.0),
itemBuilder: (context, i) {
return visibility == true
? new Container(
child: Padding(
padding: EdgeInsets.only(top: 5, right: 5, left: 5),
child: Material(
borderRadius: BorderRadius.circular(20),
elevation: 9,
color:
Theme.of(context).cardColor.withOpacity(.95),
shadowColor:
Theme.of(context).accentColor.withOpacity(.5),
child: Container(
padding: EdgeInsets.only(
bottom: 10, right: 10, left: 10, top: 15),
height: 210,
decoration: BoxDecoration(
color: Color(0xFF0B0F1B),
borderRadius: BorderRadius.circular(20),
border: Border.all(
width: 1,
color: Colors.grey.withOpacity(0.5))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
'Number Of Participants :-' +
snapshot.data.documents[i]
.data['numberOfParticipants'],
style: TextStyle(
color: Colors.white, fontSize: 15),
),
),
Padding(
padding: const EdgeInsets.only(top: 7.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 40,
),
Material(
borderRadius:
BorderRadius.circular(20),
elevation: 26,
color: Color(0xFF0B0F1B),
child: Container(
height: 30,
width: 120,
decoration: BoxDecoration(
color: Color(0xFF0B0F1B),
borderRadius:
BorderRadius.circular(20),
border: Border.all(
width: 1,
color: Colors.grey
.withOpacity(0.5))),
child: InkWell(
splashColor: Colors.redAccent,
borderRadius:
BorderRadius.circular(20),
child: Center(
child: Text(
'View',
style: TextStyle(
color: Color(0xFFC77AF3),
fontSize: 18),
)),
onTap: () {
int currentval = int.parse(
snapshot.data.documents[i]
.data[
'numberOfParticipants']);
setState(
() {
currentval--;
snapshot.data.documents[i]
.data[
'numberOfParticipants'] =
currentval.toString();
if (currentval == 0) {
visibility = false;
}
},
);
},
),
),
),
SizedBox(
width: 85,
),
Material(
borderRadius:
BorderRadius.circular(20),
elevation: 26,
color: Color(0xFF0B0F1B),
child: Container(
height: 30,
width: 120,
decoration: BoxDecoration(
color: Color(0xFF0B0F1B),
borderRadius:
BorderRadius.circular(20),
border: Border.all(
width: 1,
color: Colors.grey
.withOpacity(0.5))),
child: InkWell(
splashColor: Colors.greenAccent,
borderRadius:
BorderRadius.circular(20),
child: Center(
child: Text(
'Join',
style: TextStyle(
color: Colors.greenAccent,
fontSize: 18),
)),
onTap: () {
setState(
() {},
);
},
),
),
),
],
),
),
],
),
),
),
),
)
: new Container();
},
);
});
} else {
return Center(child: Text('Nothing yet'));
}
}
this is my code i tried to accomplish the visibility by an empty container because i don't want it to hold a place but the problem is it hides all other containers with it the whole list goes invisible

You can use Opacity with an opacity: of 0.0 to draw make an element hidden but still occupy space.
To make it not occupy space, replace it with an empty Container().
EDIT: To wrap it in an Opacity object, do the following:
new Opacity(opacity: 0.0, child: new Padding(
padding: const EdgeInsets.only(
left: 16.0,
),
child: new Icon(pencil, color: CupertinoColors.activeBlue),
))
Google Developers quick tutorial on Opacity: https://youtu.be/9hltevOHQBw
you can also try
Visibility(
visible: false,
child: child
)
);

Related

How to create horizontal ListView.builder

Hey there i want to make horizontal ListView.builder but it shows error that 'BoxConstraints forces and infinite width'. Actually i want to make a 'Buyer Request' page like Fiverr.
I achieved my goal using PageView.builder but when i use
if(snapshot.connectionState == ConnectionState.waiting)
return SpinKitDoubleBounce(color: kPrimaryColor);
it brings me back to 1st index whenever i swipe to next index.
So i want to use ListView.builder instead. Here is my code: (Hope someone will solve my problem)
if (snapshot.hasData)
return SizedBox(
child: ListView.builder(
// scrollDirection: Axis.horizontal,
itemCount: indexLength,
controller: PageController(viewportFraction: 1.0),
// onPageChanged: (int index) => setState(() => _index = index),
itemBuilder: (_, i) {
return SingleChildScrollView(
child: Card(
margin: EdgeInsets.all(10),
child: Wrap(
children: <Widget>[
ListTile(
leading: CircleAvatar(
backgroundColor: kPrimaryColor.withOpacity(0.8),
backgroundImage: AssetImage('assets/images/nullUser.png'),
child: snapshot.data[i]['PhotoURL'] != null
? ClipRRect(
borderRadius: BorderRadius.circular(50),
child:
Image.network(snapshot.data[i]['PhotoURL'],
width: 50,
height: 50,
fit: BoxFit.cover,),
)
: ClipRRect(
borderRadius: BorderRadius.circular(50),
child:
Image.asset('assets/images/nullUser.png',
width: 50,
height: 50,
fit: BoxFit.cover,),
)
),
title: Text(
snapshot.data[i]['Email'],
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
color: Colors.black.withOpacity(0.7),
),
),
subtitle: Text(
snapshot.data[i]['Time'],
style: TextStyle(
color: Colors.black.withOpacity(0.6)),
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
color: Colors.grey[200],
),
padding: EdgeInsets.all(10),
child: Text(
snapshot.data[i]['Description'],
style: TextStyle(
color: Colors.black.withOpacity(0.6)),
),
),
SizedBox(
height: 8,
),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(Icons.category_outlined),
title: Text(
'Category : ${snapshot.data[i]['Category']}',
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
),
),
SizedBox(height: 8),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(Icons.location_pin),
title: Text(
snapshot.data[i]['Location'],
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
),
),
SizedBox(height: 8),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(
Icons.attach_money,
color: kGreenColor,
),
title: Text(
'Budget : Rs.${snapshot.data[i]['Budget']}',
style: TextStyle(
fontSize: 14,
color: kGreenColor,
),
),
),
),
SizedBox(height: 8),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(Icons.timer),
title: Text(
'Duration : ${snapshot.data[i]['Duration']}',
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
),
),
SizedBox(
height: 35,
),
sendOfferButton(),
SizedBox(
height: 15,
),
Center(
child: Text(
"${i + 1}/$indexLength",
style: TextStyle(fontSize: 13),
),
),
],
),
),
],
),
),
);
},
),
);
If anyone want to see full file.
Check it Here
Use pageview.builder instead :
Container(
height: MediaQuery.of(context).size.height / 3,
width: MediaQuery.of(context).size.width,
child: PageView.builder(
pageSnapping: false,
physics: PageScrollPhysics(),
controller: _pageController,
scrollDirection: Axis.horizontal,
itemCount:
_articleController.articleListDat.length,
itemBuilder: (context, index) {
return Container();
}
And pagecontroller
PageController _pageController =
PageController(initialPage: 2, viewportFraction: 0.9);
First wrap your ListView.builder into LimitedBox / container then set height on it. Then add
scrollDirection: Axis.horizontal,
It's Done

how to connect space between Container with box shadow on flutter?

I had a problem with my project, i'm trying make container with box shadow on bottom. below that container i want make to new container but they are some separate space between container, i think its because box shadow. so the question is how to make they connect ith no space but the shadow still visible.
pic
here my code :
return Scaffold(
body: SafeArea(
child: Container(
child: ListView(
physics: BouncingScrollPhysics(),
children: <Widget>[
Container(
child: Padding(
padding: const EdgeInsets.all(0.0),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.zero,
topRight: Radius.zero,
),
child: Container(
height: 70.0,
margin: const EdgeInsets.only(
bottom: 6.0), //Same as `blurRadius` i guess
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(5.5),
bottomRight: Radius.circular(5.5),
topLeft: Radius.zero,
topRight: Radius.zero,
),
color: kDarkGreenColor,
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0.0, 1.0), //(x,y)
blurRadius: 6.0,
),
],
),
child: ListView.builder(
padding: EdgeInsets.only(left: 10, right: 5),
itemCount: planticos.length,
physics: BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.only(right: 15, bottom: 6),
height: 40,
width: 70,
decoration: BoxDecoration(
color: kMainColor.withOpacity(0),
image: DecorationImage(
image: AssetImage(planticos[index].image),
),
),
);
}),
),
),
),
),
Container(
margin: EdgeInsets.only(top: 0),
height: 200,
color: kMainColor,
),
],
),
),
),
);
}
use stack.
2nd Container will be visible over 1st Container.
for example...
Stack(
children: <Widget>[
Container(
height: 400,
decoration: BoxDecoration(
color: Colors.green,
),
),
Container(
height: 200,
decoration: BoxDecoration(
color: Colors.green,
boxShadow: [
BoxShadow(
color: Colors.red,
offset: Offset(0.0, 1.0), //(x,y)
blurRadius: 6.0,
),
],
),
),
],
),
You should use the stack widget, see this example:
Stack(
children:[
Container() //which you wanna be under the shadow.
Container() //which you wanna make it's shadow visible.
]
)
Tip: Use Positioned() under the stack to specify the position of containers.

How to retrieve an array data from firebase using flutter

hi i have an firebase as a back end for my flutter app and i can read and write data fine but i can not retrieve an array of strings when i put it in a text widget and add the .toString() parameter it retrieves it but in unpresentable way i want to view it in a list view can anybody help.
this is my code
ListView.builder(
itemCount: snapshot.data.documents.length,
padding: EdgeInsets.all(5.0),
itemBuilder: (context, i) {
return new Container(
child: Padding(
padding: EdgeInsets.only(top: 5, right: 5, left: 5),
child: Material(
borderRadius: BorderRadius.circular(20),
elevation: 9,
color: Theme.of(context).cardColor.withOpacity(.95),
shadowColor:
Theme.of(context).accentColor.withOpacity(.5),
child: Container(
padding: EdgeInsets.only(
bottom: 10, right: 10, left: 10, top: 15),
height: 230,
decoration: BoxDecoration(
color: Color(0xFF0B0F1B),
borderRadius: BorderRadius.circular(20),
border: Border.all(
width: 1, color: Colors.grey.withOpacity(0.5))),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
'members :-' +
snapshot.data.documents[i].data['members']
.toString(),
style: TextStyle(
color: Colors.white, fontSize: 15),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
'amount :-' +
snapshot
.data.documents[i].data['amount'],
style: TextStyle(
color: Colors.white, fontSize: 15),
),
),)
If your snapshot is not a stream then await the request and store it in a local variable. Else, Make use of a stream builder widget

How to merge two collections in Firebase flutter application?

I have created a chat application using flutter and store data in Firebase .Firebase contains two collections .One collection contains user details and another one collection contains user chats with other user.I want to merge two collection details .How to get other user details stored in user collections?
I have attached with screenshots.
Firebase collection
Flutter code
I have the same thing implemented in my Application. What I did is I use the Streambuilder widget twice, one to pull the chats, each and every chat contains a sender_id, which I then use the Id in the second Stream builder to pull user info.
Check the code below.This is all chats page:
#override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('MyChatHeads')
.doc(_onlineUserId)
.collection('Heads')
.orderBy('head_time', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Scaffold(
body: Center(
child: SpinKitThreeBounce(
color: Colors.black54,
size: 20.0,
),
),
);
} else {
if (snapshot.data.documents.length == 0) {
return Scaffold(
body: placeHolder(),
);
placeHolder();
} else {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
CupertinoPageRoute(
builder: (_) => SearchUsersPage(
userId: _onlineUserId,
),
),
);
},
child: Icon(Icons.contacts_rounded),
foregroundColor: Colors.white,
backgroundColor: Color(0xff47c8b0),
),
body: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot myChatHeads = snapshot.data.documents[index];
return chatHeadItem(
index, myChatHeads, snapshot.data.documents.length);
},
),
);
}
}
},
);
}
The item in all chats page
Widget chatHeadItem(int index, DocumentSnapshot myChatHeads, int length) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Users')
.where('user_id', isEqualTo: myChatHeads['head_subject'])
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: SpinKitThreeBounce(
color: Colors.black54,
size: 20.0,
),
);
} else {
if (snapshot.data.documents.length == 0) {
return Container(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Row(
children: [
CircleAvatar(
radius: 30,
backgroundColor: Colors.green,
child: CircleAvatar(
radius: 28,
backgroundColor: Colors.white,
child: Image(
height: 56,
width: 56,
image: AssetImage('assets/images/holder.png'),
fit: BoxFit.cover,
),
),
),
SizedBox(
width: 10,
),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 10,
),
Text(
'User not found',
style: GoogleFonts.quicksand(
color: Colors.black87,
fontWeight: FontWeight.bold,
fontSize: 16.0,
letterSpacing: .5,
),
),
//setCompanyName(myInterviews),
SizedBox(
height: 4.0,
),
InkWell(
onTap: () {
Navigator.push(
context,
CupertinoPageRoute(
builder: (_) => ChatsEngagePage(
userId: _onlineUserId,
secondUserId: myChatHeads['head_subject'],
),
),
);
},
child: Text(
'${myChatHeads['head_last_message']}',
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.quicksand(
color: Colors.black87,
fontSize: 16.0,
letterSpacing: .5,
),
),
),
SizedBox(
height: 10,
),
],
),
),
],
),
),
);
} else {
DocumentSnapshot secondUserInfo = snapshot.data.documents[0];
return Container(
//color: Colors.green,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Column(
children: [
index == 0
? SizedBox(
height: 6,
)
: SizedBox(
height: 0,
),
InkWell(
onTap: () {
Navigator.push(
context,
CupertinoPageRoute(
builder: (_) => ChatsEngagePage(
userId: _onlineUserId,
secondUserId: myChatHeads['head_subject'],
userName: secondUserInfo['user_name'],
userImage: secondUserInfo['user_image'],
),
),
);
},
child: Row(
children: [
InkWell(
onTap: () {
Navigator.push(
context,
CupertinoPageRoute(
builder: (_) => PublicProfilePage(
userId: _onlineUserId,
secondUserId: secondUserInfo['user_id'],
),
),
);
},
child: CachedNetworkImage(
imageUrl: secondUserInfo['user_image'],
imageBuilder: (context, imageProvider) =>
CircleAvatar(
radius: 30,
backgroundColor: Colors.green,
child: CircleAvatar(
radius: 28,
backgroundColor: Colors.white,
backgroundImage: imageProvider,
),
),
placeholder: (context, url) => CircleAvatar(
radius: 30,
backgroundColor: Colors.green,
child: CircleAvatar(
radius: 28,
backgroundColor: Colors.white,
backgroundImage: AssetImage(
'assets/images/holder.png',
),
),
),
errorWidget: (context, url, error) =>
CircleAvatar(
radius: 30,
backgroundColor: Colors.green,
child: CircleAvatar(
radius: 28,
backgroundColor: Colors.white,
backgroundImage: AssetImage(
'assets/images/holder.png',
),
),
),
),
),
SizedBox(
width: 10,
),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 10,
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: Text(
'${secondUserInfo['user_name']}',
style: GoogleFonts.quicksand(
color: Colors.black87,
fontWeight: FontWeight.bold,
fontSize: 16.0,
letterSpacing: .5,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
Text(
timeAgoSinceDateEn(
DateTime.fromMillisecondsSinceEpoch(
myChatHeads['head_time'],
).toString(),
),
//postSnap['press_formatted_date'],
style: GoogleFonts.quicksand(
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.grey,
),
),
),
],
),
//setCompanyName(myInterviews),
SizedBox(
height: 4.0,
),
Text(
'${myChatHeads['head_last_message']}',
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.quicksand(
color: Colors.black87,
fontSize: 16.0,
letterSpacing: .5,
),
),
SizedBox(
height: 10,
),
],
),
),
],
),
),
index == length - 1
? Container()
: Divider(
//color: Colors.red,
),
index == length - 1
? SizedBox(
height: 4,
)
: SizedBox(
height: 0,
),
],
),
),
);
}
}
},
);
}
This is a single chat page:
body: Stack(
children: [
Container(
height: double.infinity,
width: double.infinity,
color: Colors.grey[100],
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Chats')
.doc(userId)
.collection(secondUserId)
.orderBy('message_time', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: SpinKitThreeBounce(
color: Colors.black54,
size: 20.0,
),
),
);
} else {
if (snapshot.data.documents.length == 0) {
return Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Container(
width: MediaQuery.of(context).size.width / 3,
child: Image(
image: AssetImage('assets/images/empty.png'),
width: double.infinity,
),
),
),
);
} else {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: ListView.builder(
shrinkWrap: true,
reverse: true,
// physics: NeverScrollableScrollPhysics(),
// primary: false,
padding: EdgeInsets.zero,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot myPresses =
snapshot.data.documents[index];
if (myPresses['message_owner'] == userId) {
return Padding(
padding: index == 0
? EdgeInsets.only(bottom: height + 26)
: EdgeInsets.only(bottom: 0),
child: Bubble(
margin: BubbleEdges.only(top: 10),
nip: BubbleNip.rightTop,
alignment: Alignment.topRight,
color: Colors.lightGreen[100],
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
myPresses['message_body'],
style: GoogleFonts.quicksand(
fontSize: 16,
color: Colors.black87,
),
),
Text(
timeAgoSinceDateEn(
DateTime.fromMillisecondsSinceEpoch(
myPresses['message_time'],
).toString(),
),
//postSnap['press_formatted_date'],
style: GoogleFonts.quicksand(
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.grey,
),
),
),
],
),
),
);
} else {
return Padding(
padding: index == 0
? EdgeInsets.only(bottom: height + 26)
: EdgeInsets.only(bottom: 0),
child: Bubble(
margin: BubbleEdges.only(top: 10),
alignment: Alignment.topLeft,
nip: BubbleNip.leftTop,
color: Color(0xffd4eaf5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
myPresses['message_body'],
style: GoogleFonts.quicksand(
fontSize: 16,
color: Colors.black87,
),
),
Text(
timeAgoSinceDateEn(
DateTime.fromMillisecondsSinceEpoch(
myPresses['message_time'],
).toString(),
),
//postSnap['press_formatted_date'],
style: GoogleFonts.quicksand(
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.grey,
),
),
),
],
),
),
);
}
},
),
);
}
}
},
),
),
Positioned(
bottom: 10.0,
left: 10.0,
right: 10.0,
child: MeasuredSize(
onChange: (Size size) {
setState(() {
print(size);
height = size.height;
});
},
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(0),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.4),
spreadRadius: 2,
blurRadius: 3,
offset: Offset(0, 2), // changes position of shadow
),
],
),
//height: 58,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0, vertical: 8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: TextFormField(
controller: textEditingController,
keyboardType: TextInputType.multiline,
textCapitalization: TextCapitalization.sentences,
maxLength: 800,
maxLines: null,
style: GoogleFonts.quicksand(
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.black54,
letterSpacing: .5,
),
),
decoration: InputDecoration(
labelText: 'Message',
contentPadding: const EdgeInsets.symmetric(
horizontal: 0.0, vertical: 0.0),
errorStyle: TextStyle(color: Colors.brown),
),
onChanged: (val) {
setState(() => _message = val);
},
validator: (val) =>
val.length < 1 ? ('Too short') : null,
),
),
SizedBox(
width: 16,
),
InkWell(
onTap: () {
_submitMessage();
},
child: Padding(
padding:
const EdgeInsets.only(right: 8.0, bottom: 20),
child: Icon(
Icons.send_rounded,
color: Colors.green,
),
),
),
],
),
),
),
),
),
],
),

Need to get images from Firebase by using Stream builder in Flutter

I am trying to get images from firebase by using StreamBuilder widget. Tried all what I know but no result. As classes connected with each other tight everything gets tangled. here images are retrieved from manually created list as indicated above but I want to replace them with images from firebase. Please review code and help
class MyApp extends StatefulWidget {
static const String id = 'Myapp_screen';
#override
_MyAppState createState() => new _MyAppState();
}
var cardAspectRatio = 12.0 / 16.0;
var widgetAspectRatio = cardAspectRatio * 1.2;
class _MyAppState extends State<MyApp> {
var currentPage = images.length - 1.0;
#override
Widget build(BuildContext context) {
PageController controller = PageController(initialPage: images.length - 1);
controller.addListener(() {
setState(() {
currentPage = controller.page;
});
});
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color(0xEFEFEF),
Color(0xFFFF),
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
tileMode: TileMode.clamp)),
child: Scaffold(
backgroundColor: Colors.transparent,
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 12.0, right: 12.0, top: 30.0, bottom: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(
CustomIcons.menu,
color: Colors.blue,
size: 30.0,
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.search,
color: Colors.blue,
size: 30.0,
),
onPressed: () {},
)
],
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("Trending",
style: TextStyle(
color: Colors.blue,
fontSize: 46.0,
fontFamily: "Calibre-Semibold",
letterSpacing: 1.0,
)),
IconButton(
icon: Icon(
CustomIcons.option,
size: 12.0,
color: Colors.white,
),
onPressed: () {},
)
],
),
),
Padding(
padding: const EdgeInsets.only(left: 20.0),
child: Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Color(0xFFff6e6e),
borderRadius: BorderRadius.circular(20.0),
),
child: Center(
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 22.0, vertical: 6.0),
child: Text("Animated",
style: TextStyle(color: Colors.white)),
),
),
),
SizedBox(
width: 15.0,
),
Text("25+ Stories",
style: TextStyle(color: Colors.blueAccent))
],
),
),
Stack(
children: <Widget>[
CardScrollWidget(currentPage),
Positioned.fill(
child: PageView.builder(
itemCount: images.length,
controller: controller,
reverse: true,
itemBuilder: (context, index) {
return Container();
},
),
)
],
),
],
),
),
bottomNavigationBar: NavigationBottomBar(),
),
),
);
}
}
And this is for card scroll
class CardScrollWidget extends StatelessWidget {
var currentPage;
var padding = 20.0;
var verticalInset = 20.0;
CardScrollWidget(this.currentPage);
#override
Widget build(BuildContext context) {
return new AspectRatio(
aspectRatio: widgetAspectRatio,
child: LayoutBuilder(builder: (context, contraints) {
var width = contraints.maxWidth;
var height = contraints.maxHeight;
var safeWidth = width - 2 * padding;
var safeHeight = height - 2 * padding;
var heightOfPrimaryCard = safeHeight;
var widthOfPrimaryCard = heightOfPrimaryCard * cardAspectRatio;
var primaryCardLeft = safeWidth - widthOfPrimaryCard;
var horizontalInset = primaryCardLeft / 2;
List<Widget> cardList = new List();
for (var i = 0; i < images.length; i++) {
var delta = i - currentPage;
bool isOnRight = delta > 0;
var start = padding +
max(
primaryCardLeft -
horizontalInset * -delta * (isOnRight ? 15 : 1),
0.0);
var cardItem = Positioned.directional(
top: padding + verticalInset * max(-delta, 0.0),
bottom: padding + verticalInset * max(-delta, 0.0),
start: start,
textDirection: TextDirection.rtl,
child: ClipRRect(
borderRadius: BorderRadius.circular(16.0),
child: Container(
decoration: BoxDecoration(color: Colors.white, boxShadow: [
BoxShadow(
color: Colors.black12,
offset: Offset(3.0, 6.0),
blurRadius: 10.0)
]),
child: AspectRatio(
aspectRatio: cardAspectRatio,
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Image.asset(images[i], fit: BoxFit.cover),
Align(
alignment: Alignment.bottomLeft,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(
horizontal: 16.0, vertical: 8.0),
child: Text(title[i],
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
fontFamily: "SF-Pro-Text-Regular")),
),
SizedBox(
height: 10.0,
),
Padding(
padding: const EdgeInsets.only(
left: 12.0, bottom: 12.0),
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 22.0, vertical: 6.0),
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(20.0)),
child: Text("Read Later",
style: TextStyle(color: Colors.white)),
),
)
],
),
)
],
),
),
),
),
);
cardList.add(cardItem);
}
return Stack(
children: cardList,
);
}),
);
}
}
Manually created list of images
List<String> images = [
"assets/image_04.jpg",
"assets/image_03.jpg",
"assets/image_02.jpg",
"assets/image_01.png",
];
Please add below StreamBuilder Widget code...<br>
StreamBuilder(
stream: Firestore.instance
.collection('details')
.document(documentId)
.collection(collectionId)
.orderBy('timestamp', descending: true)
.limit(20)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.green)));
} else {
listMessage = snapshot.data.documents;
return Padding(
padding: const EdgeInsets.only(
top: 5.0, right: 5.0, left: 5.0),
child: ListView.builder(
itemCount: snapshot.data.documents.length,
reverse: true,
itemBuilder: (context, index) =>
buildItem(index, snapshot.data.documents[index]),
),
);
}
},
));
then make one method that return widget which contains layout of your ListView items.
Widget buildItem(int index, DocumentSnapshot document) {
return Container(
child: CachedNetworkImage(
placeholder: (context, url) => Container(
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.black),
),
width: 200.0,
height: 200.0,
padding: EdgeInsets.all(70.0),
decoration: BoxDecoration(
color: Colors.lightGreen[200],
borderRadius: BorderRadius.all(
Radius.circular(8.0),
),
),
),
imageUrl: document['imgUrl'],
width: 200.0,
height: 200.0,
fit: BoxFit.cover,
),
}

Resources