Flutter end of ListView from data in Firestore - flutter

I'd like to add some empty tiles to the bottom of my listview so that I can put "See More" button on that part. The data I'm displaying on the screen is from Firebase. This is something I learned from YouTube tutorials. This is my code.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: StreamBuilder(
stream: Collection.limit(10).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
return !snapshot.hasData
? Center(child: CircularProgressIndicator())
: ListView(
children: snapshot.data!.docs.map((document) {
return Center(
child: Card(
margin: EdgeInsets.fromLTRB(5.0, 0.0, 5.0, 1.0),
child: ListTile(
title: Text(document['title']),
subtitle: Text(document['body']),
onTap: () async { },
),
),
);
}).toList(),
);
},
),
);
}
Some people have already advised me to use ListView.builder, but I'm not sure how to implement it in my code. I repeatedly tried to use ListView.Builder but failed.

Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: StreamBuilder(
stream: Collection.limit(10).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (_, index) => Center(
child: Column(
children: [
Card(
margin: EdgeInsets.fromLTRB(5.0, 0.0, 5.0, 1.0),
child: ListTile(
title: Text("some text"),
subtitle: Text("some text"),
onTap: () async {},
),
),
TextButton(onPressed: () {}, child: Text("See More"))
],
)));
},
),
);
}

Related

I am trying to create a layout where i need to fetch data from two collections parallel from firestore

I am trying to create a layout where i need to fetch data from two collections parallel. I mean one after the other and than again from first collection. I'm trying using listview and listview.builder but its not working as one of the snapshot is repeating.
I'm providing code and screenshot of output 1. Anyone please help me.As you can see 'proposal2' is repeating twise but i want it in this sequence. 'proposal1,proposal2,proposal1,proposal2' and so on
Here is the source code as well.
View output
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class proposal1 extends StatefulWidget {
const proposal1({super.key});
#override
State<proposal1> createState() => _proposal1State();
}
class _proposal1State extends State<proposal1> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Convenor Portal"),
),
body: SafeArea(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Convenor')
.doc('classes')
.collection('BS Information Technology')
.doc('groups')
.collection('projects')
.doc('proposals')
.collection('proposal1')
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Card(
child: Column(children: [
ListView(
shrinkWrap: true,
children: snapshot.data!.docs.map((document) {
return Column(
children: [
Row(
children: [
const Text('Proposal 1'),
const SizedBox(
width: 20,
),
Text(
document["project-title"],
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold),
),
],
),
method2(),
const SizedBox(
height: 30,
),
],
);
}).toList(),
),
]),
);
}),
));
}
}
method2() {
return SafeArea(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Convenor')
.doc('classes')
.collection('BS Information Technology')
.doc('groups')
.collection('projects')
.doc('proposals')
.collection('proposal2')
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Card(
child: ListView(
shrinkWrap: true,
children: snapshot.data!.docs.map((document) {
return Row(
children: [
const Text('Proposal 2'),
const SizedBox(
width: 20,
),
Text(
document["project-title"],
style: const TextStyle(
fontSize: 24, fontWeight: FontWeight.bold),
),
],
);
// method2(),
}).toList()),
);
}));
}

How to add multiple child inside a Scaffold

I want to add a button, below the listview to navigate to the next page! But I am unsure of the layout.
#override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title:const Text("Welcome"),
centerTitle: true,
),
body:Center(
child:SizedBox(
child:ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 50,
color: Colors.amber[colorCodes[index]],
child: Center(child: Text('Entry ${entries[index]}')),
);
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
)
child: TextButton(onPressed: onPressed, child: child) // Where am i suppose to add this?
)
)
);
I am unsure of the layout! Any help will be greatly appreciated!
Simply wrap the ListView with a Column:
#override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title:const Text("Welcome"),
centerTitle: true,
),
body:Column(
children: [
Center(
child:SizedBox(
child:ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 50,
color: Colors.amber[colorCodes[index]],
child: Center(child: Text('Entry ${entries[index]}')),
);
},
separatorBuilder: (BuildContext context, int index) => const Divider(),
)
child: TextButton(onPressed: onPressed, child: child) // Where am i suppose to add this?
)
),
],
);
After you done this, you can add as many widgets as you want.
Hope it helps!

Flutter : RefreshIndicator with ListView Scroll

i have ListView. and listview have many item. Scroll through the screen.(vertical)
and if scroll position is Top, i want to RefreshIndicator work.
i don't know what i have to do..
now, only worked listview scroll. but when scroll position is Top, not work RefreshIndicator..
this is my code..
#override
Widget build(BuildContext context) {
return FutureBuilder<List<BoadModel>>(
future: getData(),
builder: (context, snapshot) {
List<BoadModel> dataList = snapshot.data ?? [];
if (snapshot.hasError) print(snapshot.error);
return RefreshIndicator(
onRefresh: () async {
setState(() {});
},
child: SingleChildScrollView(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Opacity(
opacity: 0.0,
child: ElevatedButton(
onPressed: () {}, child: Text("button"))),
Text(getCategoryName(widget.categoryPage)),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => WriteBoadPage(
category: widget.categoryPage,
),
),
);
},
child: Text("글쓰기"))
],
),
snapshot.hasData
? Column(
children: [
ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: dataList.length,
itemBuilder: (BuildContext context, int index) {
return ListViewItem(
title: dataList[index].title,
nickname: dataList[index].nickname,
time: dataList[index].time,
view: dataList[index].view,
commentCount: dataList[index].comment);
},
),
Row(
children: [Text("dafs")],
)
],
)
: Center(
child: CircularProgressIndicator(),
)
],
),
),
);
},
);
}
I found a solution but there is another problem.
The problem is that I have SingleChildScrollView already in parent widget like shown below:
return SingleChildScrollView( //have SingleChildScrollView already
child: Column(
children: [
RefreshIndicator(
child: SingleChildScrollView(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListViewItem();
},
),
),
onRefresh: () async {}),
],
),
);
So I did it like shown below:
#override
Widget build(BuildContext context) {
return RefreshIndicator(
child: SingleChildScrollView(
child: Column(
children: [
ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListViewItem();
},
),
],
),
),
onRefresh: () async {},
);

There are multiple heroes that share the same tag within a subtree without using FAB

I am trying to navigate to a route and this exception happen. Have look at my code and I don't think I have a FAB and Hero inside this route. Is it because when you tap on each List item, it will show a dialog with Gridview on it? Someone care to explain to me how can this exception happened? It doesn't produce any error on user thought, just throw an exception.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Daftar Dokter")),
body: ListView.separated(
separatorBuilder: (BuildContext context, int i) => Divider(color: Colors.grey[400]),
itemCount: widget.data.length,
itemBuilder: (context, index) {
Doctor doctor = widget.data[index];
return InkWell(
onTap: (){
_buildDialog(context, scheduleService, doctor.doctorId);
},
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: 50,
height: 50,
child: Placeholder(),
),
),
Flexible(
child: SizedBox(
child: ListTile(
title: Text(doctor.name),
subtitle: Text(doctor.specializationName),
),
)
)
],
),
);
}
)
);
}
}
Hope it will solve your issue:
onItem Builder:
itemBuilder: (context, index) {
return Hero(
tag: "itemTag $index",
child: Material(
child: InkWell(
onTap: () {
// _buildDialog(context, scheduleService, doctor.doctorId);
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => Wisgets(
tag: "itemTag $index",
)));
},
child: Row(
children: <Widget>[
Text("Item $index"),
],
),
),
),
);
},
Child Widget for row
class Wisgets extends StatelessWidget {
final String tag;
const Wisgets({Key? key, required this.tag}) : super(key: key);
#override
Widget build(BuildContext context) {
return Hero(
tag: tag,
child: Scaffold(
appBar: AppBar(
title: Text("Child"),
),
body: Column(
children: [
Text("got $tag"),
],
),
),
);
}
}

FutureBuilder inside Row flutter

Hello friends I am learning to build apps in Flutter. I have a FutureBuilder widget which retrieves Data from the server and displays them.
Here is the code:
FutureBuilder(
future: fetchBoxes(),
builder: (BuildContext context,
AsyncSnapshot<List<Box>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('no connection');
case ConnectionState.active:
case ConnectionState.waiting:
return Center(
child: CircularProgressIndicator(),
);
break;
case ConnectionState.done:
if (snapshot.hasError) {
return Text('error');
} else {
if (snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context,
int position) {
/* String price =
snapshot.data[position]['price'];*/
/* print(snapshot
.data[1].user_id);*/
// inspect(snapshot.data);
return Card();
and I want to implement it inside this Widget for getting scrollable widget:
My code :
#override
Widget build(BuildContext context) {
return SafeArea(
minimum: const EdgeInsets.only(
top: 20.0, right: 5.0, left: 5.0, bottom: 10.0),
child: Center(
child: Scaffold(
backgroundColor: Color(0xFFF6F7F8),
body: Stack(
children: [
Container(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.all(10),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
primary: KBlue,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(30))),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) =>
_addNewBox(context),
);
},
label: Text(
'Ajouter un nouveau box',
style: TextStyle(color: Colors.white),
),
icon: Icon(
CommunityMaterialIcons.plus,
color: Colors.white,
),
),
);
How can I implement scrollable widget correctly?