My code adds text to the first screen from a GridView. I have this GridView, which returns a container that resizes from Text, but the containers are very separate.
How do I join them?
Expanded(
child: GridView.builder(
padding: EdgeInsets.all(10),
itemCount: contacts.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 5.0,
mainAxisSpacing: 5.0,
),
itemBuilder: (BuildContext context, int index) {
return GestureDetector(child: Align(child: ClipRRect(
borderRadius: BorderRadius.circular(15),
clipBehavior: Clip.hardEdge,
child: Container(
padding: const EdgeInsets.all(20.0),
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.7,
),
color: Colors.grey[900],
child: Padding(
padding: EdgeInsets.only(
right: 15, left: 15, top: 10, bottom: 10),
child: Text(contacts[index].texto ?? '',
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
height: 2.5,
letterSpacing: 1,
))))),
),onTap:(){_showContactPage(contact: contacts[index]);});
},
))
If you just want your container to take all the available size just specify the width and height as double.infinity
Hope this helps.
because you set cross spacing and main spacing to 5.0 just change the values.
Related
I am new to flutter and I am making a shop app.
In the homescreen I have used grid view builder to display images of items. Now I want to add two text widgets at the bottom of each grid view item which shows the item name and price of the item.
This gridview builder is a child of a column widget. The column widget has two text widgets and gridview builder as its child.
GridView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: loadedProducts.length,
itemBuilder: (ctx, i) => Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: secondaryColorlight,
borderRadius: BorderRadius.circular(defaultBorderRadius)),
child:
Image.asset(
loadedProducts[i].imageUrl,
height: 132,
),
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
)
This is how my homepage looks right now
This is how I want it to look
You can return Column from itemBuilder. You may also need to increase height from childAspectRatio
itemBuilder: (ctx, i) => Column(
children: [
Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: secondaryColorlight,
borderRadius: BorderRadius.circular(defaultBorderRadius)),
child: Image.asset(
loadedProducts[i].imageUrl,
height: 132,
),
),
// padding if needed
Text("your text"),
],
),
Also you can check GridTile widget.
More about layout in flutter
GridView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: loadedProducts.length,
itemBuilder: (ctx, i) => Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: secondaryColorlight,
borderRadius: BorderRadius.circular(defaultBorderRadius)),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Flexible(
flex: 1,
fit: FlexFit.tight,
child: Container(
height: 190.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child:Image.asset('images/image.png'),
),
),
),
SizedBox(height: 5.0),
Padding(
padding: const EdgeInsets.only(left: 10.0, bottom: 5.0, right: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: Text('Price'),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
Text('Sometext'
maxLines: 1,
overflow: TextOverflow.ellipsis, ),
],
),
Text('title',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
],
),
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
)
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.
I have a GridView.builder to generate 2 'columns' of elements
This is my code:
ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: screenWidth * 0.05),
child: Text(
"MyText",
style: TextStyle(color: Colors.white, fontSize: 22),
),
),
GridView.builder(
padding: EdgeInsets.only(
left: screenWidth * 0.05, right: screenWidth * 0.05),
shrinkWrap: true,
primary: false,
itemCount: 12,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (context, index) {
return Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
height: screenWidth / 4.1 + screenWidth / 11,
child: Column(
children: <Widget>[
Container(
height: screenWidth / 4.1,
width: screenWidth / 2.5,
color: Colors.white,
),
Container(
height: screenWidth / 11,
width: screenWidth / 2.5,
color: Colors.blue,
),
],
),
),
),
);
},
),
],
),
As you can see in the image the title "MyText" and the elements of the GridView aren't vertically aligned. (Even though they have the same padding).
Is there a possibility to make them have the same alignment?
Thanks!
There's probably a better way to fix this, but that may require a redesign of how your widgets are laid out. I worked out a quick fix for you. Code below
ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: screenWidth * 0.05),
child: Text(
"MyText",
style: TextStyle(color: Colors.white, fontSize: 22),
),
),
GridView.builder(
shrinkWrap: true,
primary: false,
itemCount: 12,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (context, index) {
return Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
height: screenWidth / 4.1 + screenWidth / 11,
child: Column(
children: <Widget>[
Container(
height: screenWidth / 4.1,
width: screenWidth / 2.5,
color: Colors.white,
),
Container(
height: screenWidth / 11,
width: screenWidth / 2.5,
color: Colors.blue,
),
],
),
),
),
);
},
),
],
),
You can use the 'Flutter Inspector' - 'Show Debug Paint' to view the layout
I have a GridView and with its bottom overflow as below.
I have tried a lot of ways, which included adding ScrollPhysics() in my GridView, wrapping my GridView inside the Expanded widget, or wrap my Container inside the Expanded widget.
None of the above works. Can someone kindly advise please?
Below is my code:
Widget portfolioRow = StreamBuilder(
stream: Firestore.instance.collection('portfolio').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Text("Loading...");
return GridView.builder(
physics: ScrollPhysics(),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 70.0,
mainAxisSpacing: 70.0,
),
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
portfolioContainer(context, snapshot.data.documents[index]));
},
);
Widget portfolioContainer(
BuildContext context, DocumentSnapshot documentSnapshot) {
return Container(
height: 500,
width: 302,
decoration: myPortfolioDecoration(),
child: Column(children: [
Container(
margin: const EdgeInsets.only(top: 20, bottom: 10),
width: 250,
height: 250,
decoration: myPortfolioDecoration(),
),
Container(
height: 3,
width: 30,
margin: const EdgeInsets.only(top: 15, bottom: 20, right: 210),
decoration: BoxDecoration(color: Colors.yellow)),
Container(
padding: const EdgeInsets.only(bottom: 10, left: 30),
alignment: Alignment.centerLeft,
child: Text.rich(
TextSpan(
text: documentSnapshot.data["title"],
style: Theme.of(context).textTheme.headline5),
),
),
Container(
padding: const EdgeInsets.only(left: 30, bottom: 20),
alignment: Alignment.centerLeft,
child: Text.rich(
TextSpan(
text: documentSnapshot.data["tagline"],
style: Theme.of(context).textTheme.bodyText1),
),
),
]),
);
}
I have succeeded in getting rid of the bottom overflow by adding a childAspectRatio:
childAspectRatio: MediaQuery.of(context).size.height / 1100,```
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(),
),
);
}