How to display items from API - flutter

I have success to display the items with ListView.builder, now I want to display the items based on their ACTIVE or INACTIVE status at API. So when I want to display ACTIVE, it only shows the active items, and goes the same with INACTIVE.
The API is like this:
I don't have to attach identity 1100, because it is ACTIVE like identity 1200
And my code is like this:
BlocBuilder<ExcavatorBloc, ExcavatorState>(
builder: (context, state) {
return ListView.builder(
itemCount: state.excavator.length,
itemBuilder: (context, index) {
return Row(
children: [
const SizedBox(
height: 10,
width: 10,
child: CircleAvatar(
foregroundColor:
ColorName.brandSecondaryGreen,
backgroundColor:
ColorName.brandSecondaryGreen,
),
),
const SizedBox(
width: 5,
),
Text(
state.excavator[index].identity,
style: subtitle1(),
),
],
);
},
);
},
),

I'd do it like this without having to create an extra counter for active devices:
return ListView.builder(
itemCount: state.excavator.length,
itemBuilder: (context, index) {
return Visibility(
visible: state.excavator[index].status == "ACTIVE" ? true : false,
child: Row(
children: [
const SizedBox(
height: 10,
width: 10,
child: CircleAvatar(
foregroundColor: ColorName.brandSecondaryGreen,
backgroundColor: ColorName.brandSecondaryGreen,
),
),
const SizedBox(
width: 5,
),
Text(
state.excavator[index].identity,
style: subtitle1(),
),
),
],
);

Related

ListView.builder with Wrap widget duplicates data

I have a list which contains days. I displayed this list of days using Wrap widget in a ListView.builder, the data displays but it duplicates...
How can I remove the duplications?
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: days == null ? 0 : days.length,
itemBuilder: (BuildContext context, int index) {
return Wrap(
children: <Widget>[
...days.map((date) {
return Container(
width: MediaQuery.of(context).size.width / 2.4,
decoration: const BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(20.0)),
color: Colors.white,
),
child: ListTile(
onTap: () {
Navigator.pushNamed(context, '/logEntry');
},
title: const DefaultText(
size: 18,
text: "Day 1",
color: Colors.green,
weight: FontWeight.w500,
),
subtitle: DefaultText(
size: 15,
text: "${date.day}/${date.month}/${date.year}"
.toString(),
color: Colors.green,
weight: FontWeight.w500,
),
trailing: const Icon(Icons.arrow_forward_ios),
),
);
}).toList(),
],
);
},
),
You don't need to use ListView.builder like with wrap like this. You can shift to ListView just to handle height-overflow.
ListView(
children: [
Wrap(),
],
),

How to show ListView.builder with bloc

I want to showing list of items with ListView.builder, which wrap with BlocBuilder. My code is success connect to API, but the problem is I do not know how to display the items, instead the length of the items like picture below.
Here I attach the code:
SizedBox(
height: 350,
width: 290,
child: Padding(
padding: const EdgeInsets.only(left: 30, top: 20),
child: BlocBuilder<ExcavatorBloc, ExcavatorState>(
builder: (context, state) {
return ListView.builder(
itemCount: state.excavator.length,
itemBuilder: (context, index) {
return Row(
children: [
const SizedBox(
height: 10,
width: 10,
child: CircleAvatar(
foregroundColor:
ColorName.brandSecondaryGreen,
backgroundColor:
ColorName.brandSecondaryGreen,
),
),
const SizedBox(
width: 5,
),
Text(
state.excavator.length.toString(), //The problem is here----------
style: subtitle1(),
),
],
);
},
);
},
),
),
),
Instead of this
Text(
state.excavator.length.toString(),
style: subtitle1(),
),
use the index of the list builder
Text(
state.excavator?[index].attributeName ?? 'No value',
style: subtitle1(),
),

How to put space in a text loop

I want to give some width or space in "Your Likes" section between the users. How can I put some space, please give some suggestion and example.
children: [
UserImageSmall(
height: 64,
width: 64,
url: inactiveMatches[index].matchedUser.imageUrls),
Text(
inactiveMatches[index].matchedUser.name,
style: Theme.of(context).textTheme.bodyText1,
),
],
How can I remove the error showed in the "Your Chats" section here I want some help. Please let me know how can I do it. Please check the code and image below.
Text('Your Chats',
style: Theme.of(context).textTheme.headline5),
ListView.builder(
shrinkWrap: true,
itemCount: activeMatches.length,
itemBuilder: (context, index) {
return InkWell (
onTap: () {
Navigator.push(
context, MaterialPageRoute(
builder: (context) => ChatScreen(userMatch: userMatch,),
),
);
},
child: Row(children: [
UserImageSmall(
height: 60,
width: 60,
url: activeMatches[index].matchedUser.imageUrls,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
activeMatches[index].matchedUser.name,
style: Theme.of(context).textTheme.headline6,
),
SizedBox(height: 10),
Text(
activeMatches[index].chat![0].messages[0].message,
style: Theme.of(context).textTheme.bodyText1,
),
],
),
],
),
);
}
)
This is the exact emulator screen running with this given code.
In Your Likes section, give width to UserImageSmall Widget such as:
Container( width: 16, child: UserImageSmall())
Check index 0 exists or not, as in the image seems like null on index: 0
activeMatches[index].chat![0].messages[0].message
I have modified the code, hope this helps!
Text('Your Chats',
style: Theme.of(context).textTheme.headline5),
ListView.builder(
shrinkWrap: true,
itemCount: activeMatches.length,
itemBuilder: (context, index) {
return InkWell (
onTap: () {
Navigator.push(
context, MaterialPageRoute(
builder: (context) => ChatScreen(userMatch: userMatch,),
),
);
},
child: Row(children: [
UserImageSmall(
height: 60,
width: 60,
url: activeMatches[index].matchedUser.imageUrls,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
activeMatches[index].matchedUser.name,
style: Theme.of(context).textTheme.headline6,
),
SizedBox(height: 10),
Text(
activeMatches[index].chat![0]==null ? ""
: activeMatches[index].chat![0].messages[0].message,
style: Theme.of(context).textTheme.bodyText1,
),
],
),
],
),
);
}
)

Flutter Visibility with Condition (How can I hide only one widget with condition)

I have a problem with using Visibility widget.
What I want to do is hiding certain listview's by depending user's click on my Row. But when User click on row, all Listview is showing. When clicked again, all listview widget's are going away.
What I want to do is with images:
This is how my page looks
This is what happens when I click on arrow button or "Sezon 1" Text
This is what I want to do when I click on arrow button or "Sezon 1" Text
What I'm trying to do is When I click Season 2, it will show season 2 episodes. When I click season 3 it will show season 3 episodes etc.
Here is my code (I know It's a little messy for right now, apologize for that) :
GestureDetector is working same for every click.
bool viewVisible = false;
void hideWidget() {
setState(() {
viewVisible = !viewVisible;
print(viewVisible);
});
StreamBuilder<QuerySnapshot>(
stream: seasons.snapshots(),
builder:
(BuildContext context, AsyncSnapshot asyncSnapshot) {
if (asyncSnapshot.hasError) {
return Center(
child:
Text('Error'));
} else {
if (asyncSnapshot.hasData) {
List<DocumentSnapshot> listOfDocumentSnap =
asyncSnapshot.data.docs;
return Padding(
padding: const EdgeInsets.only(top: 0, left: 0),
child: Align(
alignment: Alignment.topLeft,
child: Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount: listOfDocumentSnap.length,
itemBuilder: (context, index) {
var episodes = firestore
.collection('shows')
.doc(data.id)
.collection('Seasons')
.doc(listOfDocumentSnap[index].id)
.collection('Episodes');
return Column(
crossAxisAlignment:
CrossAxisAlignment.start,
mainAxisAlignment:
MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
top: 0, left: 18, right: 18),
child: GestureDetector(
onTap: () {
hideWidget();
},
child: Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(
8),
border: Border.all(
color: Colors.pink)),
child: Row(
children: [
SizedBox(
width: 20,
),
Text(
listOfDocumentSnap[index]
.get('name')
.toString()
.toUpperCase(),
style: TextStyle(
fontSize: 20,
color:
Colors.grey[800],
fontWeight:
FontWeight.w700),
),
Spacer(flex: 1),
Icon(
Icons.arrow_drop_down,
size: 45,
color: Colors.pink,
),
],
),
),
),
),
StreamBuilder<QuerySnapshot>(
stream: episodes.snapshots(),
builder: (BuildContext context,
AsyncSnapshot asyncSnapshot) {
if (asyncSnapshot.hasError) {
return Center(
child: Text(
'Error'));
} else {
if (asyncSnapshot.hasData) {
List<DocumentSnapshot>
listOfDocumentSnap =
asyncSnapshot.data.docs;
return Padding(
padding:
const EdgeInsets.only(
top: 5, left: 18.0),
child: Visibility(
visible: viewVisible,
child: Align(
alignment:
Alignment.topLeft,
child: Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount:
listOfDocumentSnap
.length,
itemBuilder:
(context,
index) {
return ListTile(
onTap: () {
setState(
() {
selectedIndex =
index;
});
},
trailing:
Icon(Icons
.play_arrow),
title: Text(listOfDocumentSnap[
index]
.id
.toString()),
);
/* return Text(
listOfDocumentSnap[
index]
.get(
'name'));*/
},
),
],
),
),
),
);
} else {
return Center(
child:
CircularProgressIndicator());
}
}
},
),
SizedBox(
height: 30,
)
],
);
},
),
],
),
),
);
} else {
return Center(child: CircularProgressIndicator());
}
}
},
),
Sorry, I did not check what is wrong with your code. But, instead of writing your own hide/show logic for each row, try to use Flutter's ExpansionTile widget. It will handle the expanded/collapsed states for you:
https://medium.com/flutterdevs/expansion-tile-in-flutter-d2b7ba4a1f4b
There are two different way I know to handle widget visibility. Create a bool to switch visibility. I'm using _show.
Using if condition
inside (column or any widget)
if (_show) Container(),
Using Visibility Widget
Column(
children: [
Visibility(
visible: _show,
child: Container(),
),
)

How to make scroll in silverList?

I am trying to make silver app bar,but stuck in a problem. I have used infitelist to build a list as i found infitelist handle the list view more niecly , due to which app bar won't go on top as shown in blow gif. As infite list is not supported i have wrapped it with container.
Below is the code
SliverList(
delegate: SliverChildListDelegate(
<Widget>[
Container(
height: 500,
child: Column(
children: [
TopListView(posts: state.posts),
Expanded(
child: InfiniteList(
loadingBuilder: (context) {
return const SizedBox(
height: 100,
child: LoadingIndicator(
message: 'Fetching older posts',
),
);
},
hasError: state.status.isFailure,
isLoading: state.status.isLoading,
itemCount: state.posts.length,
hasReachedMax: state.hasReachedMax,
onFetchData: () {
context.read<PostsBloc>().add(const PostsRequested());
},
itemBuilder: (context, index) {
final post = state.posts[index];
return PostCard(post: post);
},
errorBuilder: (context) {
return SizedBox(
height: 100,
child: Center(
child: ReloadButton(
onPressed: () {
context
.read<PostsBloc>()
.add(const PostsRequested());
},
message: 'Failed to fetch more posts',
),
),
);
},
emptyBuilder: (context) {
return Center(
child: ReloadButton(
onPressed: () {
context
.read<PostsBloc>()
.add(const PostsRequested());
},
message: 'No Posts Found',
),
);
},
),
),
],
),
)
],
),
)
How can we solve this?
You can use SingleChildScrollView instead of Container.
Actually, all you need is CustomScrollView and SliverAppBar.
here is a widget example:
Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
pinned: false,
snap: true,
floating: true,
expandedHeight: 160.0,
flexibleSpace: const FlexibleSpaceBar(
title: Text('SliverAppBar'),
background: FlutterLogo(),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
color: index.isOdd ? Colors.white : Colors.black12,
height: 100.0,
child: Center(
child: Text('$index', textScaleFactor: 5),
),
);
},
childCount: 20,
),
),
],
),
);
If you want to know more this is SliverAppBar docs