Flutter Stack's child is transparent while using linear gradient - flutter

I am using a stack, where the save 60% off widget is on top of the container with white background. I am using gradient on the discount container. I want the gradient to be solid , but as you can see in the image, it is semi-transparent and we can see the white background under it.
My stack code is:
Container(
width: width * 0.38,
height: 250,
child: Stack(
children: [
Positioned(
top: 20,
child: InkWell(
onTap: () {},
child: Container(
width: width * 0.38,
height: 200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 35.h),
buildProductPrice(product, _intros),
buildCheckMark(product),
],
),
),
),
),
Align(
alignment: Alignment.topCenter,
child: buildDiscountText(product, _intros)),
],
),
);
buildDiscountText widget:
return Container(
height: 40,
width: 100,
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.lightBlue.withOpacity(0.8),
Color(0xffCE41FD),
],
end: Alignment.centerRight,
begin: Alignment.centerLeft,
),
borderRadius: BorderRadius.circular(20)),
child: Text(
'SAVE $_rounded %',
style: _saveTextStyle,
),
);
To get solid background I could wrap buildDiscountTextWidget with Container and provide same gradient and do this 2,3 times but I don't think that is the proper way.
here is the screen shot of the widget

You just need to remove .withOpacity(0.8) from the end of Colors.lightBlue.withOpacity(0.8)

Change buildDiscountText widget as follows:
return Container(
height: 40,
width: 100,
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.lightBlue, //remove opacity
Color(0xffCE41FD),
],
end: Alignment.centerRight,
begin: Alignment.centerLeft,
),
borderRadius: BorderRadius.circular(20)),
child: Text(
'SAVE $_rounded %',
style: _saveTextStyle,
),
);

Related

Is there something like mainaxisaligment for a stack?

Is there a way to align the blue chart bars to the bottom instead of the top?
Here's the code, the blue bar is the FractionallySizedBox:
#override
Widget build(BuildContext context) {
return Column(
children: [
Container(
height: 20,
child: FittedBox(
child: Text('\$${getShortForm(spendingAmount)}'),
),
),
SizedBox(
height: 4,
),
Container(
height: 64,
width: 10,
child: Stack(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
color: Color.fromRGBO(220, 220, 220, 1),
borderRadius: BorderRadius.circular(10),
),
),
FractionallySizedBox(
heightFactor: spendingPctOfTotal,
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
),
SizedBox(
height: 4,
),
Text(label)
],
);
}
}
I tried working with the Stack's alignment attribute but that didn't seem to work. It seems I can only work with the order in which the children are stacked, not position. I tried wrapping it with a column but that broke the entire thing.
Mostly we need to wrap stack children with positioned widget like Aling/Positioned. For your case
child: Stack(
alignment: Alignment.bottomCenter,//default is topStart
children: [
Or
Align(
alignment: Alignment.bottomCenter,
child: FractionallySizedBox(
heightFactor: .3,
child: Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10),
),
),
),
),

flutter container Image not fitting to screen

I am trying to fit image to the whole screen. I am using media query to fill the image for the whole screen, but the problem is image is scrolling when using media query full height. Is it possible to show image without scrolling to fill the whole screen.
return Stack(
children: [
Container(
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage('assets/welcome_screen.png'),fit:BoxFit.fill),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 50,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
end: const Alignment(0.0, -1),
begin: const Alignment(0.0, 0.4),
colors: <Color>[const Color(0x8A000000), Colors.black12.withOpacity(0.0)],
),
),
),
),
You can try this:
return Stack(
children: [
Column(
children: [
Expanded(
child: Container(
//height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage('assets/welcome_screen.png'),fit:BoxFit.fill),
),
),
),
]
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 50,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
end: const Alignment(0.0, -1),
begin: const Alignment(0.0, 0.4),
colors: <Color>[const Color(0x8A000000), Colors.black12.withOpacity(0.0)],
),
),
),
),

how to make widget disappear when overflow

i have created spotify login UI by using align widget to make typing comfortable. but the image above doesn't disappear or get pushed like the align widget. is there a solution to remove the image or detect if an overflow occurs?
Stack(
fit: StackFit.expand,
children: [
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xff96979C), Color(0xff636669), Color(0xff25252D)],
stops: [0.2, 0.5, 0.8]
),
),
),
Positioned(
top: medias.viewPadding.top + medias.size.height * 0.1,
left: 0,
right: 0,
child: Container(
height: 200,
width: 200,
child: Image(
image: AssetImage('assets/spotify.png'),
fit: BoxFit.scaleDown,
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: medias.size.height * 0.35,
child: Column(...),
),
),
],
),
Use column with the wrap of singleChildScrollView instead of the stack.
Something like this.
SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xff96979C), Color(0xff636669), Color(0xff25252D)],
stops: [0.2, 0.5, 0.8]
),
),
child: Column(
children: [
Container(
margin: EdgeInsets.only(top: 50),
height: 200,
width: 200,
child: Image(
image: AssetImage('assets/spotify.png'),
fit: BoxFit.scaleDown,
),
),
Container(
height: medias.size.height * 0.35,
child: Column(...),
),
],
),
),
)

How can I position text over image each other in a stack?

I've been trying to create a little card in flutter, and I've been trying to get the hang of stacks and how to position items over each other. I've moved the items around each other within the code a ton, but I can't seem to figure out how to position the text over the image, as shown. Any help on how to do this would be fantastic!
image of problem
body: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 20),
child: Stack(children: <Widget>[
Center(
child: InkWell(
onTap: null,
child: Container(
padding: EdgeInsets.all(5),
height: 200,
width: 300,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomLeft,
end: Alignment.topRight,
colors: [Colors.red, Colors.redAccent],
),
borderRadius: BorderRadius.circular(25)),
child: Column(),
)),
),
Center(
child: Align(
alignment: Alignment.bottomCenter,
child: Image.asset(
'assets/running.png',
height: 180,
width: 300,
))),
Center(
child: Text(
"Events",
style: GoogleFonts.poppins(
color: Colors.white,
fontWeight: FontWeight.w600,
fontSize: 30),
)),
]))
],
),
Use Stack Widget with below code
Stack(
children: <Widget>[
Card(
child: Container(
height: 200,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
alignment: FractionalOffset.center,
image: AssetImage('assets/images/jitesh.jpg'),
)),
),
),
Align(
alignment: Alignment.topCenter,
child: Text(
'jitesh',
style: TextStyle(fontSize: 20, color: Colors.white),
)),
],
),

Why is there a white line between 2 containers flutter

I am making a drawer for my app and it has 2 containers in the column widget, there seems to be a line in between these containers that ruins the appearance, i have tried this for the upper container
margin: EdgeInsets.only(bottom: 0),
padding: EdgeInsets.only(bottom: 0),
and this for the lower container
margin: EdgeInsets.only(top: 0),
padding: EdgeInsets.only(top: 0),
still the line remains. How to remove that line, any help would be appreciated. Here is the code for the drawer:
Drawer(
child: Column(
children: <Widget>[
Container(
height: globals.heightofdevice * 0.30,
width: double.infinity,
margin: EdgeInsets.only(bottom: 0),
padding: EdgeInsets.only(bottom: 0),
child: Stack(
children: <Widget>[
Image.asset(
'./images/drawerbackground.jpg',
fit: BoxFit.fitWidth,
),
Positioned(
left: globals.widthofdevice * 0.07,
bottom: 20,
child: Text(
globals.username,
style: GoogleFonts.quicksand(
textStyle: TextStyle(
// fontWeight: FontWeight.w700,
fontSize: 32,
color: Colors.white,
),
),
),
)
],
),
),
Container(
height: globals.heightofdevice * 0.70,
margin: EdgeInsets.only(top: 0),
padding: EdgeInsets.only(top: 0),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.white],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
child: Stack(children: <Widget>[
Image.asset(
'./images/uni.jpg',
fit: BoxFit.cover,
width: double.infinity,
),
Column(
children: <Widget>[
listTileBuilder(Icons.history, 'History'),
listTileBuilder(Icons.info_outline, 'About the app'),
listTileBuilder(Icons.account_circle, 'Logout'),
listTileBuilder(Icons.exit_to_app, 'Exit'),
],
),
]),
)
],
),
)
You can clearly see the problem in this picture
it must be something with the image or the stack
another example lets try
return Scaffold(
appBar: new AppBar(
title: new Text("Drawer"),
),
drawer: Drawer(
child: Column(
children: <Widget>[
Container(
color: Colors.yellow,
height: MediaQuery.of(context).size.height * 0.30,
width: double.infinity,
child: new Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("./images/drawerbackground.jpg"), fit: BoxFit.fill)),
child: new Align(
alignment: Alignment.bottomLeft,
child: new Padding (
padding: EdgeInsets.only(left: 20.0, bottom: 10.0),
child: Text(
"globals.username",
style: TextStyle(color: Colors.white),
),
),
),
),
),
Container(
height: MediaQuery.of(context).size.height * 0.70,
width: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("./images/uni.jpg"), fit: BoxFit.fill),
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.white],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
child: Column(
children: <Widget>[
listTileBuilder(Icons.history, 'History'),
listTileBuilder(Icons.info_outline, 'About the app'),
listTileBuilder(Icons.account_circle, 'Logout'),
listTileBuilder(Icons.exit_to_app, 'Exit'),
],
),
)
],
),
),
body: Container(
child: Text('Add Moka'),
),
);
The problem is most likely the fact that your top widget's image is set to fit: BoxFit.fitWidth,. Since it is trying to fill the horizontal plain fully it will stop once it has done so and not scale vertically to cover the remaining white space.
To fill the space of the container not taken by your image with a color so it isn't a white line you can try this (changes background to black):
Container(
height: globals.heightofdevice * 0.30,
width: double.infinity,
margin: EdgeInsets.only(bottom: 0),
padding: EdgeInsets.only(bottom: 0),
colors: Colors.black, // Added color
child: Stack(
...
),
),
Or to try and make the image take up all the available space of the container, regardless of the image's size and aspect ratio you can change the BoxFit to fill:
Image.asset(
'./images/drawerbackground.jpg',
fit: BoxFit.fill, // BoxFit fill
),