This question is being posted here as the related question here on stack overflow has only workarounds but no straight to the point approach.
this can be achieved with the fallowing code
AppBar(
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.cyan, Colors.yellow], stops: [0.5, 1.0],
),
),
),
),
See the Pen
bGVBVpz by yadunandan (#iamyadunandan)
on CodePen.
For a simple appbar with a gradient background and centered title,
AppBar(
automaticallyImplyLeading: false, // hides default back button
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.topRight,
colors: [
Color(0xffB1097C),
Color(0xff0947B1),
]),
)),
title: Text("WishList", style: TextStyle(fontSize: 20.0, color: Colors.white)),
centerTitle: true,
),
For a more complex appbar with a gradient background and action icons
AppBar(
automaticallyImplyLeading: false, // hides default back button
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.topRight,
colors: [
Color(0xffB1097C),
Color(0xff0947B1),
]),
)),
title: Text("Welcome guest", style: TextStyle(fontSize: 20.0, color: Colors.white)),
actions: [
IconButton(
icon: SvgPicture.asset("enter svg path",height: 20.0, width: 20.0),
onPressed: () {
print("Icon 1 pressed");
}),
IconButton(
icon: SvgPicture.asset("enter svg path", height: 20.0, width: 20.0),
onPressed: () {
print("Icon 2 pressed");
},
)
],
)
Related
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: const BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topRight,
colors: [
Colors.orange,
Colors.red,
],
),
),
child: const CircleAvatar(
radius: 70,
backgroundColor: Colors.transparent,
child: Text(
"S",
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
),
),
),
)
],
),
I want the profile color created for each user to be produced only once and remain constant.
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: const BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topRight,
colors: [
Colors.orange,
Colors.red,
],
),
),
child: const CircleAvatar(
radius: 70,
backgroundColor: Colors.transparent,
child: Text(
"S",
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
),
),
),
)
],
),
This will generate linear gradient which will be unique for each initial
LinearGradient getGredient(String inital){
List<Color> colors = [
Colors.red,
Colors.blue,
Colors.green,
Colors.purple,
Colors.orange,
Colors.pink,
Colors.yellow,
Colors.teal,
Colors.indigo,
Colors.brown,
Colors.cyan,
Colors.lime,
Colors.lightBlue,
Colors.lightGreen,
Colors.deepOrange,
Colors.deepPurple,
Colors.amber,
Colors.blueGrey,
Colors.brown,
Colors.cyan,
Colors.deepOrange,
Colors.deepPurple,
Colors.green,
Colors.indigo,
Colors.lightBlue,
Colors.lightGreen,
Colors.lime,
Colors.orange,
Colors.pink,
Colors.purple,
Colors.red,
Colors.teal,
Colors.yellow,
];
int index = inital.codeUnitAt(0) % colors.length;
return LinearGradient(
colors: [colors[index], colors[index + 1]],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
);
}
Simply Try this:
...
import 'dart:math' as math;
...
var randomColor = Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
...
Container(
decoration: const BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topRight,
colors: [
randomColor,
randomColor
],
),
),
I am trying to create a button similar to this one, I don't have exact colors so using yellow and black.
Want this
My Code Output
here is my code:
class CustomButton extends StatelessWidget {
final String? text;
double width;
final Function()? onPressed;
CustomButton({this.width = 0.8, this.text, this.onPressed});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPressed,
child: Container(
child: Center(
child: CustomTextWidget(
text: text!,
textColor: AppColors.BLACK_COLOR,
fontWeight: FontWeight.bold,
fontSize: 1.1,
)),
height: ScreenSize.heightSize * 0.08,
width: width.sw,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.yellow, Colors.black],
begin: Alignment.topRight,
end: Alignment.topRight,
stops: [0.7, 0.8],
tileMode: TileMode.repeated,
),
borderRadius: BorderRadius.circular(4)),
),
);
}
}
Kindly help how to do this.
Try below code hope its help to you and used FractionalOffset
GestureDetector(
onTap: () {},
child: Container(
alignment: Alignment.center,
height: 44.0,
width: 100,
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.yellow,
Colors.black,
],
begin: FractionalOffset.bottomCenter,
end: FractionalOffset.topCenter,
),
),
child: const Text('SIGN UP'),
),
),
Result->
You can modify your Linear Gradient like this:
LinearGradient(
colors: [
Color.fromARGB(100, 137, 90, 11),
Color.fromARGB(255, 252, 208, 72)
],
begin: Alignment.topCenter,
end: Alignment.center,
stops: [0.0, 0.3],
),
Output:
IMO, It looks better.
P.S. You can change colors & stops property as per your need.
Use this code for Gradient color in Elevated Button
Container(
height: 44.0,
decoration: BoxDecoration(gradient: LinearGradient(colors: [Colors.orange, Colors.green])),
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(primary: Colors.transparent, shadowColor: Colors.transparent),
child: Text('Elevated Button'),
),
)
How to add gradient color in the background of a card ? Should I reproduce this card with a container and a box decoration or is there another simple way ?
Try below code hope it help to you in below answer change Color your need.
Card(
child: Container(
height: 50,
width: 150,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.yellow,
Colors.orangeAccent,
Colors.yellow.shade300,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Container(), //declare your widget here
),
),
Your Card look like->
If you are gradient background to card or gradient border to card try below code
Container(
height: 50,
width: 150,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.yellow,
Colors.orangeAccent,
Colors.yellow.shade300,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child:Card(
color:Colors.white,
child: Container(), //declare your widget here
),
),
Your Screen like ->
I know it's a bit late, but you can try this to achieve a card gradient with border radius
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black38,
blurRadius: 3.0,
spreadRadius: 0.0,
offset: Offset(1.0, 1.0),
)
],
gradient: LinearGradient(
colors: [startColor, endColor],
begin: Alignment.bottomLeft,
end: Alignment.topRight,
),
),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 6),
TextWidget(
title,
typographyToken: TypographyToken.text14SemiBold,
color: Colors.white,
),
const SizedBox(height: 8),
TextWidget(
"$pendingCount Pending Request",
typographyToken: TypographyToken.text10,
color: Colors.white,
),
const SizedBox(height: 6),
],
),
),
);
Result :
result
This is a sample that I tried just now. Works fine for me.
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.black, Colors.white],
begin: Alignment.topLeft,
end: Alignment.bottomRight)),
)
Another way and probably the best in my opinion:
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerRight,
end: Alignment.center,
colors: [Colors.deepOrangeAccent, Colors.orange],
),
),
width: 300,
height: 300,
child: Card(
color: Colors.transparent,
),
),
Output:
Click here to view
Card(
shadowColor: tabColorAmber,
elevation: 10,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
gradient: LinearGradient(colors: [
Colors.orangeAccent,
Colors.orangeAccent,
Colors.yellow.shade100,
])),
width: double.infinity,
height: 140,
),
),
Example
Is it posible to make a button like the image? It doesn't need to be circular just is there anyway to gice that groove effect to a raised button and text field areas in Flutter?
Also link of the image is "https://css-tricks.com/circular-3d-buttons/"
This is made by css, if there is any trick to do it with material design maybe css import for such a work?
You can get a pretty similar result by using a RawMaterialButton and a decorated Container.
Container(
padding: EdgeInsets.all(10),
decoration: new BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
stops: [0.0 , 0.5, 1.0],
colors: [Colors.white, Colors.white, Colors.grey[200]]
)
),
child: RawMaterialButton(
onPressed: () {},
child: new Icon(
Icons.settings,
color: Colors.grey[600],
size: 28.0,
),
shape: new CircleBorder(),
elevation: 2.0,
fillColor: Colors.white,
padding: const EdgeInsets.all(18.0),
)
);
Try this
new RawMaterialButton(
onPressed: () {},
child: new Icon(
Icons.pause,
color: Colors.blue,
size: 35.0,
),
shape: new CircleBorder(),
elevation: 2.0,
fillColor: Colors.white,
padding: const EdgeInsets.all(15.0),
),
I have an IconButton with a gradient overlay, like this
Container(
foregroundDecoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.orange.shade100, Colors.orange.shade900],
begin: Alignment(0, 0),
end: Alignment(0, 1)
),
backgroundBlendMode: BlendMode.screen
),
child: IconButton(
icon: Icon(Icons.add_box),
iconSize: 36,
color: Colors.black,
onPressed: (){},
),
);
It looks great until I tap it, then suddenly the gradient covers the entire Container (and the IconButton) and stays there.
Is there a way to prevent it from breaking when tapping it?
It may be an option for you to wrap your Icon in the Container, instead of wrapping the whole IconButton.
IconButton(
icon: Container(
foregroundDecoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.orange.shade100, Colors.orange.shade900],
begin: Alignment(0, 0),
end: Alignment(0, 1),
),
backgroundBlendMode: BlendMode.screen),
child: Icon(Icons.add_box),
),
iconSize: 36,
color: Colors.black,
onPressed: () {},
),