Why is there extra top margin when using GridView in Flutter? - flutter

When I use GridView in Flutter, there is extra margin at the top, I set the padding and margin to 0 and still the problem persists.
Here is my code:
Container(
padding: EdgeInsets.only(top: 25, bottom: 0, right: 25, left: 25),
margin: EdgeInsets.zero,
child: Text(
'Let\'s begin',
style: TextStyle(
fontSize: 2,
fontWeight: FontWeight.w700),
),
),
GridView(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
children: [
Container(
margin: EdgeInsets.all(20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: darkModeOn ? Color.fromRGBO(10, 10, 50, 1) : Colors.white,
border: darkModeOn ? Border.all(color: Colors.white, width: 2) : null,
boxShadow: darkModeOn
? null
: [
BoxShadow(
color: Colors.black.withOpacity(0.175),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
),
),
Container(
margin: EdgeInsets.all(20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: darkModeOn ? Color.fromRGBO(10, 10, 50, 1) : Colors.white,
border: darkModeOn ? Border.all(color: Colors.white, width: 2) : null,
boxShadow: darkModeOn
? null
: [
BoxShadow(
color: Colors.black.withOpacity(0.175),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
),
),
Container(
margin: EdgeInsets.all(20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: darkModeOn ? Color.fromRGBO(10, 10, 50, 1) : Colors.white,
border: darkModeOn ? Border.all(color: Colors.white, width: 2) : null,
boxShadow: darkModeOn
? null
: [
BoxShadow(
color: Colors.black.withOpacity(0.175),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
),
),
Container(
margin: EdgeInsets.all(20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: darkModeOn ? Color.fromRGBO(10, 10, 50, 1) : Colors.white,
border: darkModeOn ? Border.all(color: Colors.white, width: 2) : null,
boxShadow: darkModeOn
? null
: [
BoxShadow(
color: Colors.black.withOpacity(0.175),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
),
),
],
),
I wrapped the above code with ListView. The extra margin on top makes the page look bad. I searched about this problem and found nothing.
The output:
What I actually want:

I fixed It! I added
padding: EdgeInsets.zero
to the GridView

Related

flutter problem : How to create a button like this?

I want to create like this button
But I am creating like this
when I m removing this line then I got error
right: BorderSide(
color: skyBlue,
width: 2.0,),
This is my code
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
setState(() {
tabValue = 'Growth';
});
print(tabValue);
},
child: Padding(
padding: const EdgeInsets.only(
bottom: 10, top: 10),
child: Container(
width:MediaQuery.of(context).size.width/2.24,
decoration: BoxDecoration(
border: Border(
left: BorderSide(
color: skyBlue,
width: 2.0,
style: BorderStyle.solid),
top: BorderSide(
color: skyBlue,
width: 2.0,
style: BorderStyle.solid),
bottom: BorderSide(
color: skyBlue,
width: 2.0,
style: BorderStyle.solid),
right: BorderSide(
color: skyBlue,
width: 2.0,),
),
boxShadow: tabValue == 'Growth' ? <BoxShadow>[
BoxShadow(
color: Colors.grey,
blurRadius: 2,
offset: Offset(0.50, 1)
)
]:<BoxShadow>[
BoxShadow(
color: Colors.white,
blurRadius: 0,
offset: Offset(0,0)
)
],
color: tabValue == 'Growth'
? skyBlue
: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
bottomLeft: Radius.circular(5),
),),
padding: EdgeInsets.symmetric(
horizontal: 20, vertical: 15),
child: Text(
"Growth",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: tabValue == 'Growth'
? Colors.white
: skyBlue),
textAlign: TextAlign.center,
)),
),
),
InkWell(
onTap: () {
setState(() {
tabValue = 'Home Loan';
});
print(tabValue);
},
child: Padding(
padding: const EdgeInsets.only(
bottom: 10, top: 10),
child: Container(
width:MediaQuery.of(context).size.width/2.4,
decoration: BoxDecoration(
border: Border.all(
color: green2Color,
width: 2.0,
style: BorderStyle.solid),
boxShadow: tabValue == 'Home Loan' ? <BoxShadow>[
BoxShadow(
color: Colors.grey,
blurRadius: 2,
offset: Offset(0.50, 1)
)
]:<BoxShadow>[
BoxShadow(
color: Colors.white,
blurRadius: 0,
offset: Offset(0,0)
)
],
color: tabValue == 'Home Loan'
? green2Color
: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(5),
bottomRight: Radius.circular(5),
),),
padding: EdgeInsets.symmetric(
horizontal: 10, vertical: 15),
child: Text(
"Home Loan",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: tabValue == 'Home Loan'
? Colors.white
: green2Color),
textAlign: TextAlign.center,
)),
),
)
],
),
You can allow set borderRadius only when you define all border side same or not define each one of them, try this:
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
setState(() {
tabValue = 'Growth';
});
print(tabValue);
},
child: Container(
margin: EdgeInsets.only(bottom: 10, top: 10),
padding: EdgeInsets.only(top: 2, left: 2, bottom: 2),
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
bottomLeft: Radius.circular(5),
),
color: Colors.blue,
),
child: Container(
width: MediaQuery.of(context).size.width / 2.24,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
bottomLeft: Radius.circular(5),
),
boxShadow: tabValue == 'Growth'
? <BoxShadow>[
BoxShadow(
color: Colors.grey,
blurRadius: 2,
offset: Offset(0.50, 1))
]
: <BoxShadow>[
BoxShadow(
color: Colors.white,
blurRadius: 0,
offset: Offset(0, 0))
],
color:
tabValue == 'Growth' ? Colors.blue : Colors.white,
),
padding:
EdgeInsets.symmetric(horizontal: 20, vertical: 15),
child: Text(
"Growth",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: tabValue == 'Growth'
? Colors.white
: Colors.blue),
textAlign: TextAlign.center,
)),
),
),
InkWell(
onTap: () {
setState(() {
tabValue = 'Home Loan';
});
print(tabValue);
},
child: Container(
margin: EdgeInsets.only(bottom: 10, top: 10),
padding: EdgeInsets.only(top: 2, right: 2, bottom: 2),
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(5),
bottomRight: Radius.circular(5),
),
color: Colors.green,
),
child: Container(
width: MediaQuery.of(context).size.width / 2.4,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(5),
bottomRight: Radius.circular(5),
),
boxShadow: tabValue == 'Home Loan'
? <BoxShadow>[
BoxShadow(
color: Colors.grey,
blurRadius: 2,
offset: Offset(0.50, 1))
]
: <BoxShadow>[
BoxShadow(
color: Colors.white,
blurRadius: 0,
offset: Offset(0, 0))
],
color: tabValue == 'Home Loan'
? Colors.green
: Colors.white,
),
padding:
EdgeInsets.symmetric(horizontal: 10, vertical: 15),
child: Text(
"Home Loan",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: tabValue == 'Home Loan'
? Colors.white
: Colors.green),
textAlign: TextAlign.center,
)),
),
)
],
)
Try Changing the right border color as transparent.
right: BorderSide(
color: Colors.transparent,
width: 2.0,),

Shadow Effect only to the left of a card In flutter?

So here is the output image which I wanted to achieve.
and this is the result that I have achieved.
So you can see the shadow effect is in the left and slowly fading towards right.
here is my code for this
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20)
),
child: Card(
elevation: 5,
),
),
So elevation provides me with shadow but only towards bottom but I want it to be on the left to right fading... any way to achieve this?
Thanks.
Try the following code:
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: const Offset(-7.5, 7.5),
),
],
),
child: Card(),
),
Container(
margin: EdgeInsets.all(3),
padding: EdgeInsets.zero,
width: MediaQuery.of(context).size.width * 0.5,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
color: Colors.blueAccent,
boxShadow: [
BoxShadow(blurRadius: 8.0),
BoxShadow(color: Colors.white, offset: Offset(0, -16)),
BoxShadow(color: Colors.white, offset: Offset(0, 16)),
BoxShadow(color: Colors.white, offset: Offset(-16, -16)),
BoxShadow(color: Colors.white, offset: Offset(-16, 16)),
],
),
child: Card(color: Colors.white,elevation: 0),
),
You can customize it according to your own code
You can Simply Do This In Card Using RoundedRectangleBorder
Card(
elevation:12.0,
color: Colors.grey[900],
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.white70, width: 1),
borderRadius: BorderRadius.only(topLeft: Radius.circular(8.0),
bottomLeft: Radius.circular(8.0),
),
),
margin: EdgeInsets.all(20.0),
child: Container(
child: Column(
children: <Widget>[
ListTile(
title: Text(
'example',
style: TextStyle(fontSize: 18, color: Colors.white),
),
),
],
),
),
),
You can set the shadow of the container box with this one.
Container(
margin: EdgeInsets.all(3),
padding: EdgeInsets.zero,
width: MediaQuery.of(context).size.width * 0.45,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
spreadRadius: 5,
blurRadius: 6,
offset: const Offset(-6.0, 6.00),
),
],
borderRadius: BorderRadius.circular(20),
color: Colors.white
),
child: Card(color: Colors.white,elevation: 0),
),

How to set width responsive in Flutter?

enter image description here
I'm designing to be responsive in Flutter. I want to process width auto, 100%, how do I do it? I have processed double.infinity.
Widget _bottomSubText(subtext) {
return Container(
padding: const EdgeInsets.only(top: 15),
width: 977,
height: 70,
decoration: BoxDecoration(
color: const Color.fromARGB(255, 245, 245, 245),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 3,
blurRadius: 7,
offset: const Offset(0, 3),
)
],
),
child: Text(
subtext,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 30),
),
);
}
Use width: MediaQuery.of(context).size.width in your container.
Widget _bottomSubText(subtext) {
return Container(
padding: const EdgeInsets.only(top: 15),
//Set this width, if you want to half of screen multiplied it by 0.5 and so on...
width: MediaQuery.of(context).size.width,
height: 70,
decoration: BoxDecoration(
color: const Color.fromARGB(255, 245, 245, 245),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 3,
blurRadius: 7,
offset: const Offset(0, 3),
)
],
),
child: Text(
subtext,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 30),
),
);
}
Wrap your Container by Widget call (Expanded)
https://api.flutter.dev/flutter/widgets/Expanded-class.html

Flutter how to add box-shadow or drop-shadow onto text input field?

I don't think this is possible but thought I would ask and see if anyone has a solution for this I want to add box-shadow or drop-shadow onto the text input field but I don't want it to get messed up when using error text or hint text. Attached screenshot showing the issue. I tried Material, Physical Mode, Container still getts messed I don't think there is a way as error text is inside the input widget, and once it shows the height grows.
Maybe You Could Try This One
Padding(
padding: const EdgeInsets.only(
left: 16, right: 16, bottom: 16, top: 8),
child: Container(
height: 48,
decoration: BoxDecoration(
color: Colors.red.shade300,
borderRadius: const BorderRadius.all(Radius.circular(24.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey.withOpacity(0.6),
blurRadius: 8,
offset: const Offset(4, 4),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius: const BorderRadius.all(Radius.circular(24.0)),
highlightColor: Colors.transparent,
onTap: () {
yourfunction();
},
child: Center(
child: loadingButton
? SizedBox(
height: 30.0,
width: 30.0,
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation(Colors.white),
),
)
: Text(
'Apply',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
color: Colors.white),
),
),
),
),
),
)
This is probably not the most elegant way, but you could tinker with something like this:
Stack(
children: [
Container(
height: 40.0,
width: 200.0,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.3),
spreadRadius: 4,
blurRadius: 12,
offset: Offset(0, 8),
),
],
),
),
Container(
width: 200.0,
child: TextFormField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: 'Name',
errorText: 'Field cannot be empty',
),
),
),
],
);

Shadows in a rounded rectangle in Flutter

I tried adding a shadow to a rounded container but Flutter adds shadows as if the container is a perpendicular rectangle, which I don't want. I looked up for this problem on Google but couldn't find any appropriate solution, please help me out.
Code for Container
Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Color(0xFFF9D276),
borderRadius: BorderRadius.circular(35),
boxShadow: [
BoxShadow(
offset: Offset(0, 4),
color: Color(0xFFF9D276).withOpacity(0.15),
spreadRadius: 4,
blurRadius: 50
)
]
),
)
UPDATE
Answer from #HardikKumar & How I actually want it
You can use from this code for rounded rectangle :)
Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
decoration: BoxDecoration(
color: Colors.green[700],
shape: BoxShape.rectangle,
borderRadius:BorderRadius.all(
Radius.circular(25)
)
),
margin: EdgeInsets.only(right: 8,top: 8),
child: IconButton(
icon: Icon(
Icons.send,
color: Colors.yellow[600],
),
onPressed:() {}
),
)
I'm sorry but I tested your code and the shadow shape appears with the same border as the decoration box
Image
I just edit the color of the shadow to black so i can see it better.
Maybe u want use a button like that , u need to use ButtonTheme to use height and width ,
Final edition:
Use Material , it has elevation property, shapeBorder and BorderRadiusGeometry, u can use an container as parent of Material for the width and height.
Link to Material Widget on Flutter
Problems in the code:
With the blurRadius of 50, you won't see any difference in shadow when you change the spreadRadius or the offset
With the opacity of 0.15, you will barely see any shadow (both in black or white backgound color)
Try this code:
Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Color(0xFFF9D276),
borderRadius: BorderRadius.circular(35),
boxShadow: [
BoxShadow(
//offset: Offset(0, 4),
color: Color(0xFFF9D276), //edited
spreadRadius: 4,
blurRadius: 10 //edited
)
),
),
How it looks like:
If you need anything else, just let me know.
As far I get some idea from your first image, you can check the code below.
Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xff000000),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Color(0x73000000),
blurRadius: 5.0,
spreadRadius: 1,
offset: new Offset(-10.0, 0.0),
),
],
),
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.25,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
// padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Color(0xFFF9D276),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Color(0xff000000),
blurRadius: 0.0,
spreadRadius: -2,
offset: new Offset(2.0, 0.0),
),
],
borderRadius: BorderRadius.circular(35),
),
),
),
),
)
You can try this
Stack(
children: [
Container(
margin: EdgeInsets.only(left: 2, top: 2),
child: Align(
alignment: Alignment.center,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
primary: Colors.red,
backgroundColor: Colors.gray,
fixedSize: const Size(200, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
side: const BorderSide(width: 3.0, color: ColorsConst.gray9),
),
child: const Text("Text"),
onPressed: () => print('Text'),
),
),
),
Container(
margin: EdgeInsets.only(right: 2, bottom: 2),
child: Align(
alignment: Alignment.center,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
primary: Colors.red,
backgroundColor: Colors.gray,
fixedSize: const Size(200, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
side: const BorderSide(width: 3.0, color: ColorsConst.gray9),
),
child: const Text("Text"),
onPressed: () => print('Text'),
),
),
)
]
)