I am making a grid like instagram where every seventh element is of 2X1. The problem is i want to show a different widget at seventh index but my code splits the gridTile widget rather than showing a different widget.
CODE:-
return StaggeredGridView.countBuilder(
shrinkWrap: true,
crossAxisCount: 3,
itemCount: gridTile.length,
itemBuilder: (context,index)=>gridTile[index],
staggeredTileBuilder: (index)=>StaggeredTile.count(
1,(index%7==0)?2:1,
),
);
Just Change your itemBuilder line of code as follow instead of "yourWidget" please write widget name you want to return.
itemBuilder : (context, index) {
return index % 7 == 0 ? yourWidget : gridTile[index];
},
Related
I'm tring to write a log viewer application with flutter.
I've learn that ListView.builder could handle larget number, even infinite number of items.
for example:
const mockLines = [
'this is a short line that will display in one line',
'this is a long line that will display in multiple lines, let us see how long it will be ?',
];
Widget _buildLogView() {
return ListView.builder(
itemCount: 1000,
itemBuilder: (context, index) {
final line = mockLines[index % mockLines.length];
return Text('$index: $line');
}
);
}
which works good, text got wrapped when it's too long.
But scrolling got very slow, and memory got blow when I set itemCount to 1000000.
Tried itemExtent:
Widget _buildLogView() {
return ListView.builder(
itemCount: 1000000,
itemExtent: 20
itemBuilder: (context, index) {
final line = mockLines[index % mockLines.length];
return Text('$index: $line');
}
);
}
Problem solved.
However, text won't wrap anymore, it just got truncated.
It seems that there's no way for me to create a listview can handle millions of lines when keeping the 'wrap' behavior.
Am I miss something, or just a limitation of flutter?
===============
A release build, draging scrollbar to 1/3 with itemCount = 1000000
memory keep crimbing up (would finally eat all)
same on windows:
My data is like this:
I want to display it like this:
In the example, the title and subtitle are static. But in my case, it is dynamic. How do I achieve this UI? I tried like putting it in a ListView.builder with horizontal scrolling. But again do I need another ListView.builder to show restaurant list from rests array? Or any other ways to do this?
That can depend on your layout.
You can look at the use case below.
Using ListView + ListView:
If you want to give your second ListView full height.
ListView(
children: [
// Other widgets ...
ListView.builder(
shrinkWrap: true, // <-- Set this to true
physics: ScrollPhysics(), // <-- Also change physics
itemBuilder: ...,
),
],
)
YES; You can use listview to implement this functionality.
demo(){
List<Map> restaurantArray = [
{"name":"breakfase 1","cover":"http://......."},
{"name":"breakfase 2","cover":"http://......."},
{"name":"breakfase 3","cover":"http://......."},
{"name":"breakfase 4","cover":"http://......."},
{"name":"breakfase 5","cover":"http://......."},
];
return ListView.builder(
itemCount: restaurantArray.length,
itemBuilder: (ctx,index){
// return your cell with data
return Container(
child: Text(restaurantArray[index]["name"].toString()),
);
}
);
}
What I want to achieve is to dynamically change the crosAxisCount in gridview and set it according to the width of the widget.
I mean according to width of the widgets inside the gridview. If the widgets are long and only two can fit in the row then crosAxisCount = 2, if the widgets are small and four can fit in a row than crosAxisCount = 4.
//// You should use staggered grid view. By using that you can achive what you want. Just refer below dart plugin https://pub.dev/packages/flutter_staggered_grid_view
new StaggeredGridView.countBuilder(
crossAxisCount: , //as per your requirement
itemCount: , //as per your requirement
itemBuilder: (BuildContext context, int index) {
if (index % 2 == 0) { //for even row
return; //your required widget
} else { //for odd row
return; //your required widget
}
},
staggeredTileBuilder: (int index) => index % 2 == 0
? new StaggeredTile.fit(2)
: new StaggeredTile.fit(1),
)
I am using this plugin flutter_swiper:
Swiper(
index: currentIndex,
itemCount: 12,
itemBuilder: (BuildContext context,int index){
return PhotoView(
imageProvider: AssetImage("images/c"+(index+1).toString()+".jpg"),
);
},
),
I need the swiper to not swipe from last index to first index and not from first to last only swipe between indexes.
what I have is a list of images to swipe between
Is it possible to do this using this plugin or I should use another one ?
if so please provide another solution
It seems to be that Swiper has a property for that, called loop:
/// Set to false to disable continuous loop mode.
final bool loop;
So you could try:
Swiper(
loop: false,
...
I want to put google ads after specific number of items list or in between the items list in ListView.separated.
I have attached an image to make it clear how I want to do it:
Source of the image is: this article
First, I believe, you need to install a library to display Google AdMob ads. For instance - admob_flutter lib.
Then you only need to set up the separatorBuilder property of your ListView.separated widget.
e.g.
ListView.separated(
itemCount: 25,
separatorBuilder: (BuildContext context, int index) {
if (index % 5 == 0) { // Display `AdmobBanner` every 5 'separators'.
return AdmobBanner(
adUnitId: /* AdMob Unit ID */,
adSize: AdmobBannerSize.BANNER,
);
}
return Divider();
},
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('item $index'),
);
},
)
This code is only an example, you might encounter Infinite Height layout errors as I am not aware of how admob_flutter widgets operate.
However, this should be enough to get you started with your idea.
Let me know if this helped.