How do I mix multiple gradients in Flutter? - flutter

The designer has combined two or more gradients in the Figma Design they created. In some designs, they have combined a radial gradient with a linear gradient; while some other designs, they have combined a linear gradient with another linear gradient.
This is something that can easily be done with CSS, but, in Flutter, I haven't been able to implement it. I have read almost all documentation of Flutter but no solution seems to be in sight. Is there anyway I can combine two gradients without having the designer change the design?

Had to wrap container in a container to achieve this effect:
Container(
decoration: BoxDecoration(
gradient: gradientOne,
),
child: Container(
decoration: BoxDecoration(
gradient: gradientTwo,
),
child: content,
),
)
Works with semi-transparent gradients.

gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [Colors.blue, Colors.red])),

if you mix multiple gradient like this
so try to this way
body: Column(
children:<Widget> [
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [Colors.blue, Colors.red])),
child: Center(
child: Text(
'',
style: TextStyle(
fontSize: 48.0,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [Colors.white, Colors.black])),
child: Center(
child: Text(
'',
style: TextStyle(
fontSize: 48.0,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
)
],
));

try to use
Gradient.lerp(yourFirstGradient, yourSecondGradient, 0.5)
both have to be the same type

gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [ Color.fromARGB(255, 255, 255, 255),
Color.fromARGB(255, 218, 217, 217)]
)
),
//This is what I did in a container. I wrapped my entire scafffold in a container to get the background to be a white to light grey color.

Related

Flutter about color ib boxdecoration

Cannot provide both a color and a decoration To provide both, use
"decoration: BoxDecoration(color: color)".
'package:flutter/src/widgets/container.dart': container.dart:1 Failed
assertion: line 273 pos 15: 'color == null || decoration == null'
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(15),
child: Text(title),
color: color,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),
color,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(15),
),
);
}
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(15),
child: Text(title),
color: color, ***--> Delete this line!***
decoration: BoxDecoration(
color: color, ***--> Put that line here!***
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),
color,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(15),
),
);
}
You seem to have set a decoration already, so drop the color: color,, because it's not suported to do both.
the error is self explanatory it says that when you are using decoration n the container you have to pass the color inside the decoration like this
decoration: BoxDecoration(
color: //color
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),
color,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(15),
),

How can i achieve this kind of border in Flutter

am trying to get this kind of half border with gradient color only top right and bottom left
Refer below snippet
Container(
height: 200,
width: 200,
// change padding value to modify width of border
padding: EdgeInsets.all(2),
// space between content and outer border
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
),
padding: EdgeInsets.all(5),
// main content
child: Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.black45),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text("Content"),
),
),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [Colors.blue, Colors.white, Colors.white, Colors.blue],
begin: Alignment.topRight,
end: Alignment.bottomLeft,
stops: [0.1, 0.2, 0.8, 0.9],
),
),
)

How to add gradient color in card?

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

How to make the Flutter app bar with gradient background

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");
},
)
],
)

Replicate Android gradient in Flutter

I have this gradient on Android:
<gradient
android:angle="45.0"
android:centerColor="#ffeeeeee"
android:endColor="#ffbbbbbb"
android:startColor="#ffcccccc" />
I want it to replicate it on flutter but I'm unable to do so.
I have tried to use a LinearGradient but doesn't even come close to the one on Android.
I tried this:
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color(0xffeeeeee),
Color(0xffcccccc),
Color(0xffbbbbbb),
],
begin: Alignment(-1.0, -4.0),
end: Alignment(1.0, 4.0),
),
),
Thanks
You can do it like this
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.red, Colors.orange],
begin: Alignment.topLeft,
end: Alignment.bottomRight)),
child: Container(
)),
);
For color code, do like this
Color hexToColor(String code) {
return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
}
Try to add gradient as:
Container(height: 200,
width: 350,
decoration: BoxDecoration(
color: Colors.white,
gradient: LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: [
Color.fromRGBO(0, 0, 0, 0.0),
Color.fromRGBO(0, 0, 0, 0.25),
Color.fromRGBO(0, 0, 0, 0.7),
],
stops: [0.5, 0.7, 0.9],
)),
),