I need to design like this Grid View using Flutter
try this, flutter_staggered_grid_view:
StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: 8,
itemBuilder: (BuildContext context, int index) => new Container(
color: Colors.green,
child: new Center(
child: new CircleAvatar(
backgroundColor: Colors.white,
child: new Text('$index'),
),
)),
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 2 : 1),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
)
Related
I'm trying to get the 2 images next to each other instead of beneath each other but I have no clue how to go about something like this, I tried something with an I variable to influence the index but that did not really work out. normally I could do this but with the index, I have no clue how to go about this.
code:
Center(
child: Column(
children: [
_items.isNotEmpty
? Expanded(
child: ListView.builder(
itemCount: _items.length,
itemBuilder: (context, index) {
return Column(
children: [
RawMaterialButton(
child: Image(
image: NetworkImage(_items[index]["portrait"]),
height: 200,
width: 140,
),
onPressed: () {
Navigator.push(context, _AnimatedNavigation(SpecificCharacterRoute(
_items[index]["name"], _items[index]["name"], _items[index]["shop_background"], _items[index]["overview"],
_items[index]["difficulty"], "images/dbdsurvivorlogo.png")
)
);
},
),
],
);
},
),
)
: Container()
],
),
),
Here's an image of what I mean
Here's an image of what I have
You can use GridView.count( with crossAxisCount: 2,
GridView.count(
crossAxisCount: 2,
children: [...],
);
Or with .builder
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: ,
itemBuilder: (context, index) {...},
);
You can use Gridview for 2 in a row. If you want to change from 2 to 3 in row then you can change crossAxisCount.
GridView.builder(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
crossAxisCount: 2,
childAspectRatio: 3 / 2,
crossAxisSpacing: 20,
mainAxisSpacing: 20),
itemCount: _items.length,
itemBuilder: (BuildContext ctx, index) {
return Column(
children: [
RawMaterialButton(
child: Image(
image: NetworkImage(_items[index]["portrait"]),
height: 200,
width: 140,
),
onPressed: () {
Navigator.push(context, _AnimatedNavigation(SpecificCharacterRoute(
_items[index]["name"], _items[index]["name"], _items[index]["shop_background"], _items[index]["overview"],
_items[index]["difficulty"], "images/dbdsurvivorlogo.png")
)
);
},
),
],
);
}),
I have gridView builder which is displaying 2 items in a row. like below image
I want to display like this image
\n
Please help me to achieve this in flutter
GridView.builder(
gridDelegate:
SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
childAspectRatio: 2.5 / 3,
crossAxisSpacing: 10,
mainAxisSpacing: 10),
shrinkWrap: true,
primary: false,
itemCount: nListLevels.modules.length,
itemBuilder: (context, j) {
// displaying contents here
}
));
I guess you can return "column" and inside the "column", you can add "Row" widget to suit your UI
for different size of grid items, you need flutter_staggered_grid_view:
StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: 8,
itemBuilder: (BuildContext context, int index) => new Container(
color: Colors.green,
child: new Center(
child: new CircleAvatar(
backgroundColor: Colors.white,
child: new Text('$index'),
),
)),
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 2 : 1),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
)
I've been trying to create the following screen in Flutter:
https://i.imgur.com/meBdNFz.png
So far I've made this with the package "Staggered Grid View":
https://i.imgur.com/mR6pQ3A.png
Sorry for not being able to upload the images..
However, I still can't figure out how to use different widths for the tiles. The first tile needs to fill around 70% of the containers size and the right one the rest.
At the moment I have the following code:
new StaggeredGridView.countBuilder(
padding: EdgeInsets.all(30),
crossAxisCount: 2,
itemCount: 7,
itemBuilder: (BuildContext context, int index) => new Container(
margin: EdgeInsets.all(4),
color: Colors.green,
child: new Center(
child: new CircleAvatar(
backgroundColor: Colors.white,
child: new Text('$index'),
),
)),
staggeredTileBuilder: (int index) => (index == 0)
? new StaggeredTile.count(2, 1)
: (index % 2 == 0)
? new StaggeredTile.count(1, 0.8)
: new StaggeredTile.count(1, 0.8),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
)
You don't need to have
itemCount: 7
instead, just use
staggeredTiles: [StaggeredTile.extent(width in crossAxisCount, height in units)]
Here's my code. Haven't tested it but it should be close to what you want.
body: StaggeredGridView.count(
crossAxisCount: 10, //this indicates how much elements (or cards) you want to put in a row
crossAxisSpacing: 6.0,
mainAxisSpacing: 6.0,
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
children: [
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
],
staggeredTiles: [
StaggeredTile.extent(10, 200), //second parameter is for adjusting the height of the element
StaggeredTile.extent(6, 150),
StaggeredTile.extent(4, 150),
StaggeredTile.extent(3, 150),
StaggeredTile.extent(5, 150),
StaggeredTile.extent(2, 150),
StaggeredTile.extent(6, 150),
StaggeredTile.extent(4, 150),
],
),
I hope my answer satisfies you :)
I have tried using Flutter_staggered_grid_view, but it seems as though that its built better for vertical scrolling.
My goal is to have a horizontal gridview with dynamic widths to make the grid feel natural and not so spread apart
This is what I have (I removed some of the UI code, but its essentially the same)
GridView.builder(
scrollDirection: Axis.horizontal,
itemCount: sourceList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: .3),
itemBuilder: (context, i) {
final e = sourceList[i];
return Text(
e.name,
);
},
);
And this is what I am looking for
This is the max I could achieve by using ternary operator to adjust mainAxisCellCount. You can adjust the the count according to the length of you shortest and longest string.
Container(
height: 180.0,
child: StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: 8,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) => Container(
child: Center(child: Container(
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
child: Chip(
label: Text('${fruits[index]}', overflow: TextOverflow.visible, maxLines: 1, style: TextStyle(),)))),
),
staggeredTileBuilder: (int index) => StaggeredTile.count(2, fruits[index].length > 3 ? fruits[index].length > 8 ? fruits[index].length > 2 ? 4 : 3 : 2 : 1),
mainAxisSpacing: 0.0,
crossAxisSpacing: 0.0,
),
),
How to set the different color of every box of Staggered GridView in Flutter?
StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: 10,
itemBuilder: (BuildContext context, int index) => Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
color: Colors.white,
child: Stack(
children: <Widget>[
],
),
),
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 2.5 : 1.7),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
),
),
If you want random colors try random_color package
import 'package:random_color/random_color.dart'; //You can use this package
itemBuilder: (BuildContext context, int index) => Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
color: setColor(index),
child: Stack(
children: <Widget>[
],
),
),
staggeredTileBuilder: (int index) =>
StaggeredTile.count(2, index.isEven ? 2.5 : 1.7),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
),
Color setColor(int index){
return index.isEven ? Colors.white : Colors.lime[300]; //if you want to change between 2 colors
//You can use random_color package
return randomColor.randomColor(
colorHue: ColorHue.multiple([ColorHue.white, ColorHue.blue] //Define all the hues you want in your range
)
}
You can define your own list of colors and choose it based on the index, or just use the random color package and let it choose it for you