Flutter sort data Firestore with Streambuilder - flutter

My goal: When the user presses the "List" button inside "_mainListItem" I want the listview to get sorted by orderBy. Aswell as updated on screen
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class mainlist extends StatefulWidget {
#override
_mainlistpage createState() => _mainlistpage();
}
class _mainlistpage extends State<mainlist> {
Widget homePage() {
return StreamBuilder(
stream: Firestore.instance.collection("Test").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Text("Loading");
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_mainListItem(context, snapshot.data.documents[index]));
},
);
}
Widget _mainListItem(BuildContext context, DocumentSnapshot document) {
return Card(
color: Colors.white,
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => profile(context, document)));
},
child: Container(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.black12))),
child: Row(
children: [
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(
children: <Widget>[
Stack(
alignment: Alignment.topRight,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 5),
child: ClipRRect(
borderRadius:
BorderRadius.circular(0.0),
child: FittedBox(
child: Image.asset(
"assets/Profile Picture.png",
fit: BoxFit.fill,
)),
),
),
Padding(
padding: const EdgeInsets.only(
top: 7, right: 4),
child: Text(
'Test',
style: TextStyle(fontSize: 12),
),
),
]),
Row()
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
document['name'],
),
// Text("2km"),
],
),
],
),
],
),
),
],
),
),
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 5, bottom: 5),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10, right: 7),
child: Container(
child: Material(
borderRadius: BorderRadius.circular(5),
shadowColor: Colors.black,
elevation: 1,
child: SizedBox(
height: 28,
width: 68,
child: IconButton(
padding: EdgeInsets.only(bottom: 10),
**icon: Icon(Icons.list),
disabledColor: Colors.blue,
iconSize: 25,**
)),
),
),
),
],
),
)
],
)
],
),
),
),
);
}
#override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
backgroundColor: Colors.grey,
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
color: Colors.red,
),
title: Text("Test"),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.menu),
iconSize: 30,
color: Colors.white,
)
],
),
body: homePage(),
);
}
}
I have tried
- adding the streambuilder function into the ontapped: on the List button
- have read and watched every video there is and still can't find the solution
note: the app looks weird because I deleted unnecessary information

You can sort the list items before the snapshot method like:
.orderBy('sortField', descending: true).snapshot()
I hope this works for you.

Try mapping the values to a List<CustomObject> and using the list of objects in your list view.

i suggest you use state to determine the field how your list will be sorted by.
this is what i'd do to achieve this (continuing from the same code):
...
class _mainlistpage extends State<mainlist> {
String _orderBy = 'defaultSort'; //? HERE YOU PUT WHAT YOUR SORTING FIELD NAME IS
bool _isDescending = true; //? THIS IS WHAT WILL SET THE ORDER SORTING
Widget homePage() {
return StreamBuilder(
stream: Firestore.instance
.collection("Test")
.orderBy(_orderBy, descending: _isDescending) //? PUT THE ORDERBY QUERY HERE
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Text("Loading");
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_mainListItem(context, snapshot.data.documents[index]));
},
);
}
...
somewhere in the class, put the button or dropdown and use setState(...) to set the
states of the new variables.
NOTE: you might have to create 'indexes' in firestore. you will get errors when a new
index is required.

Related

List view childs get out of container

I am trying to make a list view only occupy part of the screen, but it keeps growing till the end not respecting the contianer constraints. I tried to use a sizedbox too but it didn' work. List tiles outside the container are shown without any widget inside, but the background is shown anyways
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: pedidos,
builder: (context, AsyncSnapshot<List<Pedido>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: MediaQuery.of(context).size.height * 0.6,
child: ListView.builder(
itemCount: snapshot.data!.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Hero(
tag:
"pedidos_card${snapshot.data![index].idPedido}",
child: ListTile(
tileColor: Colors.white,
leading: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle),
child: Center(
child: Text(
style: Theme.of(context)
.textTheme
.headlineSmall,
"${snapshot.data![index].idPedido}"),
),
),
title: Text(
'Pedido: ${snapshot.data![index].idPedido}'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 10),
Text(
'Estado: ${snapshot.data![index].estadoPedido.last.tipoEstadoPedido.name}'),
SizedBox(height: 10),
Text(
"Cliente: ${snapshot.data![index].cliente.nombre}")
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
trailing: Checkbox(
value: pedidosSeleccion
.contains(snapshot.data![index]),
onChanged: (bool? value) {
// value = checkboxList[index];
// setState(() {});
},
),
onTap: () {
bool isSelected = pedidosSeleccion
.contains(snapshot.data![index]);
if (isSelected) {
pedidosSeleccion
.remove(snapshot.data![index]);
} else {
pedidosSeleccion.add(snapshot.data![index]);
}
setState(() {});
},
),
));
}),
),
ElevatedButton(
onPressed: () {}, child: Text('Ver ultima milla')),
],
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
});
}
}
example
you can use Expanded instead of Sizedbox
eg:-
Column(
children:[
Expanded(flex:9,
child: ListView(
padding: const EdgeInsets.only(top: 10.0),
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
),
),
Expanded(flex:1,
child:
ElevatedButton(
// fill in required params
),
)
])

The getter 'length' was called on null in flutter

I have added three streamBuilder on my homepage and all the StreamBuilders fetch different data from cloud firestore but i am getting 'The getter 'length' was called on null in flutter' . I have tried to look for a solution on the internet but all in vain. Am getting this error:
════════ Exception caught by widgets library ═══════════════════════════════════
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was
StreamBuilder<List<Order>>
lib/…/home/home.dart:52
Here is my home.dart:
import 'package:flutter/material.dart';
import 'package:merza/models/orders.dart';
import 'package:merza/models/user.dart';
import 'package:merza/screens/home/items.dart';
import 'package:merza/screens/home/orders/delivered_order.dart';
import 'package:merza/screens/home/orders/order_status.dart';
import 'package:merza/screens/user/add_order.dart';
import 'package:merza/services/auth.dart';
import 'package:merza/services/databse.dart';
import 'package:merza/shared/grid.dart';
import 'package:intl/intl.dart';
import 'package:merza/shared/menus.dart';
import 'package:merza/shared/splash_screen.dart';
import 'package:provider/provider.dart';
import 'package:merza/screens/home/orders/orders_list.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'orders/orders_tile.dart';
class Home extends StatelessWidget {
//Auth
AuthService _auth = AuthService();
//Database
Database _db = Database();
#override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
if(user.uid == null){
Navigator.pushNamedAndRemoveUntil(context, "/home", (r) => false);
}else{
return StreamBuilder<UserData>(
stream: Database(uid: user.uid).userData,
builder: (context, snapshot) {
if (snapshot.hasData) {
UserData userData = snapshot.data;
//Streambuilder for delivered orders
return StreamBuilder<List<Order>>(
stream: Database(receiver_nrc: userData.nrc, paramOne: 'payment_status', paramValue: 'unpaid').dataFetch,
builder: (context, snapshot2) {
String count_unpaid = snapshot2.data.length.toString();
if (snapshot2.hasData) {
return StreamBuilder <List<Order>>(
stream: Database(receiver_nrc: userData.nrc, paramOne: 'order_status', paramValue: 'delivered').dataFetch,
builder: (context, snapshot3) {
String count_awaiting = snapshot3.data.length.toString();
if (snapshot3.hasData) {
return StreamProvider<List<Order>>.value(
value: Database(receiver_nrc: userData.nrc).orders,
child: Scaffold(
appBar: LoggedBar(title: 'Merza',),
body: SingleChildScrollView(
child: Column(
children: [
Container(), // Required some widget in between to float AppBar
Container( // To take AppBar Size only
child: AppBar(
elevation: 0.0,
backgroundColor: Colors.white,
leading: IconButton(icon: Icon(Icons.home), color: Colors.green[900], onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => Home()));
},),
primary: false,
title: TextField(
decoration: InputDecoration(
hintText: "Search",
border: InputBorder.none,
hintStyle: TextStyle(color: Colors.green[900]))),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search, color: Colors.green[900]), onPressed: () {},),
],
),
),
Container(
color: Colors.green,
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 20.0),
child: Text('A TRADING ZONE THAT PROMOTES SAFETY TRADING AND SECURE DIGITAL TRANSACTIONS', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14, color: Colors.white)),
), Container(
margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: 130,
child: ListTile(
title: Image.network('https://madvertadvertising.com/media/merza/images/seller.jpeg'),
onTap: (){
// Navigator.push(context, MaterialPageRoute(builder: (context) => ItemsPage()));
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(width: 8),
],
),
],
),
),
),
Expanded(
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: 130,
child: ListTile(
title: Image.network('https://madvertadvertising.com/media/merza/images/delivery.jpeg'),
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => AwaitingOrdersList(nrc: userData.nrc, paramValueOne: 'order_status', paramValueTwo: 'delivered', barTitle: 'Delivered', isStaff: userData.is_staff.toString(), allowStaffPrivaledges: 'no',)));
},
),
),
const SizedBox(width: 8),
],
),
),
),
],
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: 130,
child: ListTile(
title: Image.network('https://madvertadvertising.com/media/merza/images/items.jpeg'),
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => ItemsPage()));
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(width: 8),
],
),
],
),
),
),
Expanded(
child: Container(
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: 130,
child: ListTile(
title: Image.network('https://madvertadvertising.com/media/merza/images/payment.jpeg'),
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => DeliverdordersList(nrc: userData.nrc, paramValueOne: 'payment_status', paramValueTwo: 'paid', barTitle: 'Paid orders',)));
}
,
),
),
const SizedBox(width: 8),
],
),
),
),
),
],
),
),
Text('My recent placed orders', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
Container(
child: OrdersList(),
),
SizedBox(height: 10.0),
],
),
),
drawer: DrawerLogged()
),
);
}else{
return LoadingScreenCustom(page:'home');
}
}
);
}else{
return LoadingScreenCustom(page:'home');
}
}
);
}else{
return LoadingScreenCustom(page:'home');
}
}
);
}
}
}
I don't know if it is a good practice to add multiple stream builders on the same page.
Put String count_unpaid = snapshot2.data.length.toString(); line inside the if (snapshot2.hasData) block.
Put String count_awaiting = snapshot3.data.length.toString(); line inside the if (snapshot3.hasData) block.

How can I insert an initial element into Listview.builder

I want to show the defaultUserContainer() in case the stream is empty but also in case it's not, as an initial element of the list.
Currently. I can't seem to make either scenarios work. How can I design this better?
Column(
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 16.0, top: 8.0),
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"users",
style: TextStyle(fontSize: 20.0, color: Colors.black87),
)
),
),
),
],
),
SizedBox(
height: 120,
child: FutureBuilder(
builder: (context, snapshot) {
return StreamBuilder(
stream: _firestore.collection('ts').where('userid', isEqualTo: widget.user.id).snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
defaultUserContainer(); //If there's no users. tried returning it, or doing like it is here. never shows
return Text("");
} else {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot userDoc = snapshot.data.documents[index];
if(index < snapshot.data.documents.length){
return Padding(
padding: const EdgeInsets.only(bottom: 8.0, left: 8.0, right: 8.0),
child: GestureDetector(
onTap: () => {},
child: Container(
child: FittedBox(
child: Material(
color: Colors.white,
elevation: 4.0,
borderRadius: BorderRadius.circular(8.0),
shadowColor: Colors.grey,
child: Row(
children: <Widget>[
Container(
child: myDetailsContainer(userDoc), //This guy works. it's just a more complicated defaultuserContainer()
),
],
),
)
)
),
),
);
}
return ListTile(leading:defaultuserContainer()); //Doesn't show when there's users users (my goal is to always have this as an initial item)
}
);
}
},
);
},
),
)
]
);
Widget defaultUserContainer() {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Container(
height: 120,
width: 120,
color: myColors.blue,
child: Center(
child: Icon(Icons.add, size: 65, color: Colors.white),
),
),
);
}
You can define it as the first/last element of your ListView/GridView/Column/etc.
Here is a simple example with a GridView:
Full source code
import 'dart:math' show Random;
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Random Generator',
home: RandomGeneratorPage(),
),
);
}
class RandomGeneratorPage extends HookWidget {
final int max;
final random = Random();
RandomGeneratorPage({Key key, this.max = 20}) : super(key: key);
#override
Widget build(BuildContext context) {
final numbers = useState<List<int>>([]);
return Scaffold(
appBar: AppBar(title: Text('Random Generator')),
body: GridView.count(
crossAxisCount: 5,
childAspectRatio: 1,
children: [
InkWell(
onTap: () =>
numbers.value = [...numbers.value, random.nextInt(max)],
child: Card(
color: Colors.blue.shade100,
child: Icon(Icons.add),
),
),
...numbers.value
.map(
(number) => InkWell(
onTap: () => numbers.value =
numbers.value.where((x) => x != number).toList(),
child: Card(
child: Center(child: Text(number.toString())),
),
),
)
.toList(),
],
),
);
}
}
In your case:
For your particular case, it would probably look like this: [NOT TESTED]
FutureBuilder(
builder: (context, snapshot) {
return StreamBuilder(
stream: _firestore
.collection('ts')
.where('userid', isEqualTo: widget.user.id)
.snapshots(),
builder: (context, snapshot) => ListView(
scrollDirection: Axis.horizontal,
children: [
InkWell(
onTap: () {},
child: Card(
color: Colors.blue.shade100,
child: Icon(Icons.add),
),
),
...snapshot.data.documents.map(
(doc) => Padding(
padding:
const EdgeInsets.only(bottom: 8.0, left: 8.0, right: 8.0),
child: GestureDetector(
onTap: () => {},
child: Container(
child: FittedBox(
child: Material(
color: Colors.white,
elevation: 4.0,
borderRadius: BorderRadius.circular(8.0),
shadowColor: Colors.grey,
child: Row(
children: <Widget>[
Container(
child: myDetailsContainer(
doc), //This guy works. it's just a more complicated defaultuserContainer()
),
],
),
),
),
),
),
),
)
],
),
);
},
)

Is it possible to minimize objects definition as well(collapse code for objects) (Specifically Dart with flutter)(Android Studio)

I have a long code for example like this.
#override
Widget build(BuildContext context) {
print('building');
WidgetsBinding.instance
.addPostFrameCallback((_) => changeThemeAfterBuild(context));
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
_logEntryDetailBloc.dispose();
Navigator.pop(context);
},
),
title: Text(this.widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(2.0),
child: new Material(
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Container(
width: MediaQuery.of(context).size.width - 20,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(
'Message:',
style: TextStyle(
fontSize: 22,
color: Color.fromRGBO(127, 127, 127, 1)),
),
new Text(
widget.logEntry.message,
style: TextStyle(fontSize: 20),
),
StreamBuilder(
stream: _logEntryDetailBloc.logEntryDetailStream,
builder: (context, snapshot) {
print("has detail data ${snapshot.hasData}");
print(snapshot.data);
if (snapshot.hasData) {
var logEntry = (snapshot.data as LogEntry);
if (logEntry.logLevel.name == ERROR)
return Container(
child: ErrorDetailScreenContainer(),
height:
MediaQuery.of(context).size.height - 156,
);
return Container(
child: Text(snapshot.data.toString()),
height:
MediaQuery.of(context).size.height - 200);
} else {
return new Image(
image: new AssetImage("images/loader.gif"));
}
}),
],
),
),
)),
),
],
),
);
}
Now the problem is since I am doing flutter the objects are nested and each object being child of some other object.
and thus when the Parent object becomes large by nesting. it becomes slightly unreadable and bracket matching becomes tough
Solution What I am looking is something like:
#override
Widget build(BuildContext context) {
print('building');
WidgetsBinding.instance
.addPostFrameCallback((_) => changeThemeAfterBuild(context));
return Scaffold(
appBar: AppBar(...), // I folded here
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(2.0),
child: new Material(
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Container(
width: MediaQuery.of(context).size.width - 20,
child: Column(...) // I foldere here too
)
),
),
),
],
),
);
}
Is Something like this possible.
Or any other alternative that I can perform, so that it looks cleaner.
The Flutter AndroidStudio/IntelliJ plugin supports this today. Here is a screenshot showing your build method collapsing a couple of subtrees. Notice that the widget ui guides also make it easier to keep track of a big widget tree in Intellij.

I want to add a new element after my horizontal listview how can I add it

This is the projected picture that shows how I am able to add an element.
Below is my code in which I want to add an element so that if my list is not present it also shows the element and if it present then the element is the last element.
I have tried this in some different ways.
Can anyone please help me?
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'alllist.dart';
List<Alllist> _alllist = [];
List<Alllist> get alllist => _alllist;
class EduCategory extends StatefulWidget{
final String listcategory;
final int intp;
EduCategory({this.listcategory,this.intp});
#override
EduCategoryState createState() {
return new EduCategoryState();
}
}
class EduCategoryState extends State<EduCategory> {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Container(child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
widget.listcategory,
style: TextStyle(fontWeight: FontWeight.bold),
),
new Row(
children: <Widget>[
new Icon(Icons.play_arrow),
new Text("Watch All", style: TextStyle(fontWeight: FontWeight.bold))
],
)
],
),
Expanded(
child: new Padding(
padding: const EdgeInsets.only(top: 8.0),
child:new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('all list').where("listcategory",isEqualTo: widget.listcategory).snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text("no");
if (snapshot.data.documents.length == 0) return InkWell(
child: Stack(
children: <Widget>[
new Container(
width: 80.0,
height: 80.0,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
border: Border.all(color: Colors.blueGrey),
borderRadius: BorderRadius.circular(5.0),
image: new DecorationImage(
fit: BoxFit.fill,
image: new AssetImage("assets/Plus.png")),
),
margin: const EdgeInsets.symmetric(horizontal: 20.0),
// child: Text(name),
),
Padding(padding: EdgeInsets.only(top: 80.0,left: 20.0),
child: Text("Add Lession",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blueGrey),),
),
],
),
onTap: (){},
);
return new ListView(
scrollDirection: Axis.horizontal,
children: buildGrid(snapshot.data.documents)
);;
}
),
))
],
),
);
}
List<Widget> buildGrid(List<DocumentSnapshot> documents) {
List<Widget> _gridItems = [];
_alllist.clear();
for (DocumentSnapshot document in documents) {
_alllist.add(Alllist.fromDocument(document));
}
for (Alllist alllist in _alllist) {
_gridItems.add(buildGridItem(alllist));
}
return _gridItems;
}
Widget buildGridItem(Alllist alllist,) {
return widget.intp==0?
InkWell(
child: Stack(
children: <Widget>[
new Container(
width: 80.0,
height: 80.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.blue,width: 4.0,style: BorderStyle.solid),
image: new DecorationImage(
fit: BoxFit.fill,
image: new NetworkImage(
alllist.imageUrl)),
),
margin: const EdgeInsets.symmetric(horizontal: 20.0),
// child: Text(name),
),
Padding(padding: EdgeInsets.only(top: 80.0,left: 10.0),
child: Text(alllist.title,style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blueGrey),),
),
],
),
onTap: (){},
):new Row(
children: <Widget>[
InkWell(
child: Stack(
children: <Widget>[
new Container(
width: 80.0,
height: 80.0,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
image: new DecorationImage(
fit: BoxFit.fill,
image: new NetworkImage(
alllist.imageUrl)),
),
margin: const EdgeInsets.symmetric(horizontal: 20.0),
// child: Text(name),
),
Padding(padding: EdgeInsets.only(top: 80.0,left: 10.0),
child: Text(alllist.title,style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blueGrey),),
),
],
),
onTap: (){},
)
]
);
}
}
You can use the ListView.builder with itemCount as snapshot.data.documents.length + 1.
Code sample:
new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('all list').where("listcategory",isEqualTo: widget.listcategory).snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text("no");
var documentsLength = snapshot.data.documents.length;
ListView.builder(itemCount: documentsLength + 1, // +1 for last element
itemBuilder: (context, index) {
if (index == documentsLength) {
//last view which have plus button
} else {
return buildGridItem((Alllist.fromDocument(snapshot.data.documents[index]))
}
});
})
I think that you have to just add condition in itemBuilder checkout in below code;
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final List<int> items=[1,2,3,4,5,6];
#override
Widget build(BuildContext context) {
final title = 'Mixed List';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Container(
height: 100.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: items.length + 1 ,
itemBuilder: (context, index) {
if(index < items.length )
{ return Container(
color: Colors.blue,
width: 100.0,
padding: EdgeInsets.all(8.0),
child: new Center(
child: new Text(index.toString()),
),
);
}
else {
return new Container(
color: Colors.blue,
width: 100.0,
child: new Center(
child: new Text(index.toString() + "differnt"),
),
);
}
},
),
),
),
);
}
}