gridview builder with random width of its items in flutter - flutter

i need to show something like this
can some one help on this?
I can able to achieve following out put
i am using following code ::
GridView.builder(
scrollDirection: Axis.vertical,
physics: ScrollPhysics(),
shrinkWrap: true,
itemCount: 6,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 4),),
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
child: Container(
padding: EdgeInsets.all(0.0),
margin: EdgeInsets.only(
left: 10.0, right: 10.0, top: 10.0, bottom: 10.0),
decoration: BoxDecoration(
border: Border.all(color: Color(0xFF282f61),width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(
14.0) // <--- border radius here
),
),
child: Center(
child: Text(
'Avg-project',
style: TextStyle(
color: Color(0xFF17b01b),
fontSize: 14.0,
),
),
),
),
onTap: () {},
);
},
))
how can i achieve gridview with random width in flutter?

gridview is not support random width so instead of gridview you can use wrap widget to achieve your desired output.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: BackAppBar("Notifications", context),
body: Wrap(
children: [
"Help Moviing",
"Furniture Assembly",
"Mounting",
"Home Repairs",
"Personal Assistant",
"Delivery",
"Hard Work",
"Practice & Unpacking",
"Painting",
"Cleaning",
"Heavy Liffing"
].map((f) => GestureDetector(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
margin: EdgeInsets.only(
left: 5.0, right: 5.0, top: 10.0, bottom: 10.0),
decoration: BoxDecoration(
border: Border.all(color: Color(0xFF282f61), width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(
10.0) // <--- border radius here
),
),
child: Text(f,
style: TextStyle(
color: Color(0xFF17b01b),
fontSize: 16.0,
),
),
),
onTap: () {},
))
.toList(),
),
);
}

Related

Another exception was thrown: RenderBox was not laid out: RenderFlex#6ac69

I want to display list and the data come from API. And I got error
Another exception was thrown: RenderBox was not laid out: RenderPadding#64692 relayoutBoundary=up5
NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I don't know what kind of error is this..
Bellow is my issue code
return AlertDialog(
insetPadding: EdgeInsets.only(bottom: 520),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
title: Container(
color: Color.fromARGB(255, 253, 253, 253),
child: Text('Comment', style: TextStyle(color: Color.fromARGB(255, 4, 4, 4))),
padding: const EdgeInsets.all(17),
margin: const EdgeInsets.all(0),
),
titlePadding: const EdgeInsets.all(0),
content: SingleChildScrollView(
child: Column(mainAxisSize: MainAxisSize.min, children: < Widget > [
GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
),
itemCount: commentList.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(2.0),
child: Container(
child: InkWell(
child: Container(
padding: EdgeInsets.only(bottom: 10, top: 10, left: 20, right: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Color(0xffD4D4D4), width: 2.0)),
child: Column(
children: < Widget > [
SizedBox(height: 15),
Expanded(
child: Text(
"test",
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 15, fontWeight: FontWeight.normal),
))
],
)),
)),
);
},
),
])),
);
The SingleChildScrollView can only have one child, and the GridView expands to fill the available space.
To fix the problem, wrap your GridView with a SizedBox and give it some height/width:
AlertDialog(
insetPadding: EdgeInsets.only(bottom: 520),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
title: Container(
color: Color.fromARGB(255, 253, 253, 253),
child: Text('Comment',
style: TextStyle(color: Color.fromARGB(255, 4, 4, 4))),
padding: const EdgeInsets.all(17),
margin: const EdgeInsets.all(0),
),
titlePadding: const EdgeInsets.all(0),
content: SingleChildScrollView(
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
// <--- Add SizedBox here
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.5,
child: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
),
itemCount: commentList.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(2.0),
child: Container(
child: InkWell(
child: Container(
padding: EdgeInsets.only(
bottom: 10, top: 10, left: 20, right: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border:
Border.all(color: Color(0xffD4D4D4), width: 2.0)),
child: Column(
children: <Widget>[
SizedBox(height: 15),
Expanded(
child: Text(
"test",
textAlign: TextAlign.justify,
style: TextStyle(
fontSize: 15, fontWeight: FontWeight.normal),
))
],
)),
)),
);
},
),
),
])),
);

Flutter GridView.builder how to put children symmetric

i use GridView.builder to build my catalog and my catalog looks bad, how can i put my grid symmetrically :
i want my catalog to look like this :
it is my GridView.builder code:
Widget buildCatalog(List<Product> product) => GridView.builder(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 300,
childAspectRatio: 3 / 5,
crossAxisSpacing: 5,
mainAxisSpacing: 10),
itemCount: product.length,
itemBuilder: (context, index) {
final productItem = product[index];
final media = product[index].media?.map((e) => e.toJson()).toList();
final photo = media?[0]['links']['local']['thumbnails']['350'];
return Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 10),
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 10),
decoration: BoxDecoration(color: Colors.white,
borderRadius: BorderRadius.circular(20)),
child: Column(
children: <Widget>[
InkWell(
child: Container(
margin: EdgeInsets.all(8),
child:
// Image.network(productItem.media),
Image.network(media != null ? 'https://cdn.yiwumart.org/${photo}' : 'https://yiwumart.org/images/shop/products/no-image-ru.jpg'),
),
),
SizedBox(height: 10,),
Text(productItem.name, textAlign: TextAlign.center, style: TextStyle(fontSize: 20),),
Container(
width: MediaQuery.of(context).size.width * 0.8,
child: ElevatedButton(onPressed: () {}, child: Text('5000 тг')))
],
),
);
},
);
Try this:
Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 10),
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 10),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(20)),
child: Column(
children: <Widget>[
InkWell(
child: Container(
margin: EdgeInsets.all(8),
child:
// Image.network(productItem.media),
Image.network(media != null
? 'https://cdn.yiwumart.org/${photo}'
: 'https://yiwumart.org/images/shop/products/no-image-ru.jpg'),
),
),
SizedBox(
height: 10,
),
Text(
productItem.name,
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20),
),
Spacer(),
Container(
width: MediaQuery.of(context).size.width * 0.8,
child: ElevatedButton(
onPressed: () {}, child: Text('5000 тг')))
],
),
),

GridView Flutter

i'm trying to do this page
but what i get is this
this is my code
SingleChildScrollView(
child: Column(
children: [
Center(
child: Container(
margin: EdgeInsets.all(20),
width: 230,
height: 40,
child: ElevatedButton(
onPressed: () {
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Color(0xff28CC61)),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
),
),
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text("Add a book", style: TextStyle(
fontSize: 20
),),
),
),
),
),
Container(
child: Text("Explore between 10 books", style: TextStyle(
fontSize: 20
),),
),
Container(
//adding: EdgeInsets.all(12),
child: SingleChildScrollView(
child: GridView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 1,
crossAxisSpacing: 20,
crossAxisCount: 2,
mainAxisSpacing: 8),
itemCount: books.length,
itemBuilder: (BuildContext ctx, index) {
return Container(
width: 50,
height: 100,
child: Card(
color: Color(0xFFEDEDED),
child: Column(
children: [
Image.asset(books[index].image, height: 150
,
width: 150,fit: BoxFit.cover,),
],
),
),
);
}),
),
),
],
),
),
ignore the colors and styles i want how to do the page
i stucked on gridview and how to write text below the picture + How can I fix my code to do it? So each image has same size as others? i tried to see the grid view documentation and i could not find anything.
Change childAspectRatio : 1 to childAspectRatio:0.7. childaspectRatio defines the ratio between width and height of children. If you set it as 1 you will get a square shaped widget. More than 1 is horizontal rectangle and less than 1 gives vertical rectangle.

How to increase Card size inside GridView

Here is my code:
GridView.count(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
crossAxisCount: 3,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
primary: false,
shrinkWrap: true,
children: tempSearchStore.map((element) {
return InkWell(
onTap: (){
print(element['ID'].toString());
Navigator.push(context, MaterialPageRoute(
builder: (context) => SearchResultScreen(ID: element['ID'],Pin:widget.pin),
));
},
child: Container(
height: 20,
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
elevation: 5.0,
child: Container(
height: 20,
child: Center(
child: Text(element['Name'],
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
),
)
)
)
),
),
);
}).toList()
)
I want to change the height and width of my card but I'm not able to do so, even I also tried wrapping Card into Container and give height and width but failed. Only I'm able to change by changing the value of CrossAxisCount in GridView and that also changes according to itself.
Here is my Image:
I want it's height to be more and width is Okay.
The key is childAspectRatio. This value is used to determine the layout in GridView. In order to get the desired aspect, you have to set it to the (itemWidth / itemHeight).
GridView.count(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
crossAxisCount: 3,
childAspectRatio: (itemWidth / itemHeight),
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
primary: false,
shrinkWrap: true,
children: tempSearchStore.map((element) {
return InkWell(
onTap: (){
print(element['ID'].toString());
Navigator.push(context, MaterialPageRoute(
builder: (context) => SearchResultScreen(ID: element['ID'],Pin:widget.pin),
));
},
child: Container(
height: 20,
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
elevation: 5.0,
child: Container(
height: 20,
child: Center(
child: Text(element['Name'],
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
),
)
)
)
),
),
);
}).toList()
)
and if this doesn't work for you look at this package: https://pub.dartlang.org/packages/flutter_staggered_grid_view

Why scroll direction horizontal is not working?

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
class Screen extends StatefulWidget {
#override
_UserProfileScreenState createState() => _UserProfileScreenState();
}
class _UserProfileScreenState extends State<Screen> {
Widget randomjobs() {
final dbRef = FirebaseDatabase.instance
.reference()
.child("Add Job Details")
.limitToFirst(3);
return FutureBuilder(
future: dbRef.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
List<Map<dynamic, dynamic>> list = [];
if (snapshot.data.value == null) {
return Text("no Data");
}
for (String key in snapshot.data.value.keys) {
list.add(snapshot.data.value[key]);
}
return ListView.builder(
itemCount: snapshot.data.value.length,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: EdgeInsets.only(top: 20.0),
height: 220.0,
child: PageView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 8, right: 8),
padding: EdgeInsets.only(
top: 20.0, left: 20.0, right: 20.0),
width: 240.0,
height: 240.0,
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(20.0)),
gradient: LinearGradient(
begin: Alignment.bottomRight,
end: Alignment.topLeft,
stops: [0.2, 0.6],
colors: [
Color(0xff7500bf),
Color(0xff5b1ad1),
],
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Text(list[index]["Job Title"],
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w500,
color: Color(0xfffefeff))),
),
Container(
margin: EdgeInsets.only(top: 10.0),
child: Opacity(
opacity: 0.6,
child: Text(list[index]["Job Title"],
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w600,
color: Color(0xffffffff))),
),
),
Container(
margin: EdgeInsets.only(top: 10.0),
child: Opacity(
opacity: 0.49,
child: Text(list[index]["Job Title"],
style: TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.w600,
color: Color(0xffffffff))),
),
),
Container(
margin: EdgeInsets.only(top: 60.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
GestureDetector(
onTap: () {},
child: Container(
padding: EdgeInsets.only(
top: 12.0,
left: 15.0,
right: 15.0,
bottom: 12.0),
decoration: BoxDecoration(
color: new Color.fromRGBO(
255, 255, 255, 0.5),
borderRadius: BorderRadius.all(
Radius.circular(12.0))),
child: Opacity(
opacity: 0.7,
child: Text('APPLY',
style: TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.w600,
color: Color(0xfffefeff))),
),
),
)
],
),
)
],
),
)
],
));
});
}
return CircularProgressIndicator();
},
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Job')),
body: randomjobs(),
);
}
}
This is the total page code. it prints in the vertical direction whereas i need it in horizontal.Even though I am using scroll direction horizontal I am not able to make it happen.Can you help. scrollDirection: Axis.horizontal I have used this in my code but the result is still I am not able to get horizontal scroll below my app bar.
Thank you
Is this what your trying?
body: ListView.builder(
shrinkWrap: true,
//add item count depending on your list
//itemCount: list.length,
itemCount: 4,
//added scrolldirection
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
//Removed your pageview
return Container(
height: 240.0,
margin: EdgeInsets.only(left: 8, right: 8, top: 20),
padding: EdgeInsets.only(top: 20.0, left: 20.0, right: 20.0),
// rest of your code