Flutter Complex Linear Gradient Not Working - flutter

Above is the expected output.
I need to implement this gradient in Flutter but it's not coming out as excepted.
Color: #6646E7
The color shift is very strict and not smooth as shown in the image.
Below is the container where I wanted to use the
Complete Code:
Stack(
alignment: Alignment.bottomRight,
children: [
Container(
margin: const EdgeInsets.only(left: 10, right: 10, top: 15),
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.white,
border: Border.all(color: Colors.white, width: 2),
// gradient: LinearGradient(
// begin: Alignment.topLeft,
// end: Alignment.bottomRight,
// stops: const [
// 0.01,
// 0.1,
// 0.9
// ],
// colors: [
// const Color(0xff6646E7).withOpacity(0.4),
// Colors.white,
// const Color(0xff6646E7).withOpacity(0.4),
// ]),
// boxShadow: [
// const BoxShadow(
// color: Color.fromRGBO(0, 0, 0, 0.25),
// blurRadius: 3,
// spreadRadius: 1,
// offset: Offset(0, 4))
// ]),
),
child: Column(
children: [
Row(
children: [
SvgPicture.asset('assets/profile/$icon.svg'),
const SizedBox(
width: 10,
),
Text(
title,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
fontFamily: GoogleFonts.poppins().fontFamily),
)
],
),
const SizedBox(
height: 20,
),
child
],
),
),
Positioned(
right: 10,
child: GestureDetector(
onTap: onEditPressed,
child: SvgPicture.asset('assets/profile/edit_pen.svg')),
)
],
);
Thanks for your help

Try this,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.black38,
Colors.black12,
Colors.black12,
Colors.black38,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
Change Colors.black38 & Colors.black12 with your desired colors.

Related

how to give curve design inside container in flutter?

I trying to understand how can I design curves in a container like the below
can anyone help me to design this kind of container? much appreciate it if anyone provides code for this kind of design.
Thank you in advance.
use Stack and boxDecoration, play with margins and size to get best result.
SizedBox(
width: double.infinity,
child: Stack(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: Colors.grey,
boxShadow: const [
BoxShadow(color: Colors.grey, spreadRadius: 3),
],
),
height: 50,
),
Container(
width: 200,
child: const Center(
child: Text('\$1400'),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: const Color(0xff232fac),
boxShadow: const [
BoxShadow(color: Color(0xff232fac), spreadRadius: 3),
],
),
height: 50,
),
Container(
width: 200,
margin: const EdgeInsets.only(left: 150),
child: const Center(
child: Text('\$1400'),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: const Color(0xff483395),
boxShadow: const [
BoxShadow(color: Colors.white, spreadRadius: 3),
],
),
height: 50,
),
],
),
),
read about Stack
read about boxDecoration
You cab usethe decoration property of the container to add border radius to a container
Container(
height: 25,
width: 100,
decoration: BoxDecoration (
borderRadius : BorderRadius circular(15),
color: Colors.purple
)
)
Use Stack and gradient to implement this
Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Container(
height: 50,
width: double.infinity,
decoration: BoxDecoration (
borderRadius : BorderRadius.circular(50),
gradient: LinearGradient(colors: [
Colors.purple,
Colors.grey,
],
stops: [0.5, 0],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
border: Border.all(color: Colors.white, width: 2)
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text("\$1400"),
Text("\$700"),
],
),
),
),
Container(
height: 50,
width: 150,
decoration: BoxDecoration (
borderRadius : BorderRadius.circular(50),
color: Colors.green,
border: Border.all(color: Colors.white, width: 2)
),
child: Center(
child: Text("\$700"),
),
),
],
Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Container(
height: 50,
width: double.infinity,
decoration: BoxDecoration (
borderRadius : BorderRadius.circular(50),
gradient: LinearGradient(colors: [
Colors.purple,
Colors.grey,
],
stops: [0.5, 0],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
border: Border.all(color: Colors.white, width: 2)
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text("\$1400"),
Text("\$700"),
],
),
),
),
Container(
height: 50,
width: 150,
decoration: BoxDecoration (
borderRadius : BorderRadius.circular(50),
color: Colors.green,
border: Border.all(color: Colors.white, width: 2)
),
child: Center(
child: Text("\$700"),
),
),
],
You can try something like this:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: MyApp()));
}
class MyApp extends StatelessWidget {
final double center = 300;
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(children: [
Positioned(
left: 0,
child: Container(
width: center,
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.indigo, Colors.blue]),
borderRadius: BorderRadius.all(Radius.circular(
MediaQuery.of(context).size.height / 2.0)),
border: Border.all(color: Colors.white, width: 3)),
child: const Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'\$ 1400',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
)),
Positioned(
right: 0,
child: Container(
width: MediaQuery.of(context).size.width - center,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.all(Radius.circular(
MediaQuery.of(context).size.height / 2.0)),
border: Border.all(color: Colors.white, width: 3)),
child: const Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'\$ 900',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
),
)),
Positioned(
left: center,
child: FractionalTranslation(
translation: const Offset(-0.5, 0),
child: Container(
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.indigo, Colors.purple]),
borderRadius: BorderRadius.all(Radius.circular(
MediaQuery.of(context).size.height / 2.0)),
border: Border.all(color: Colors.white, width: 3)),
child: const Center(
child: Padding(
padding: EdgeInsets.fromLTRB(20, 8, 20, 8),
child: Text(
'\$ 700',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
),
)),
]),
);
}
}
output:
use decoration property see simple use.
Container(
height: 50,
width: 500,
decoration: BoxDecoration (
borderRadius : BorderRadius circular(40),
color: Colors.red)
child: const Center(
child: Text('\$ 1400'),),
)

Flutter: Gesture detector being used in specific area on sized box

I have something like a tinder card that Id like to be able to detect if a user taps on the left/right area of the card. Its defined by a sized box, but how can i tell a gesture detector to only use a specific part of the given area?
relevant code, you should be able to ignore everything in the stack but i included it in case there was something i was missing:
return Padding(
padding: const EdgeInsets.only(top: 10, left: 20, right: 20),
child: SizedBox(
height: MediaQuery.of(context).size.height / 1.4,
width: MediaQuery.of(context).size.width,
child: GestureDetector(
child: Stack(
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
state.stingrays[index]!.imageUrls[0])),
borderRadius: BorderRadius.circular(5.0),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 4,
blurRadius: 4,
offset: Offset(3, 3),
)
]),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
gradient: LinearGradient(
colors: [
Color.fromARGB(200, 0, 0, 0),
Color.fromARGB(0, 0, 0, 0),
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
),
Positioned(
bottom: 30,
left: 20,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${state.stingrays[index]!.name}, ${state.stingrays[index]!.age}',
style: Theme.of(context)
.textTheme
.headline2!
.copyWith(
color: Colors.white,
),
),
Text(
'${state.stingrays[index]!.jobTitle}',
style: Theme.of(context)
.textTheme
.headline3!
.copyWith(
color: Colors.white,
fontWeight: FontWeight.normal,
),
),
Row(
children: [
Container(
width: 35,
height: 35,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
child: Icon(Icons.info_outline,
size: 25,
color: Theme.of(context).primaryColor),
)
],
)
],
),
),
],
),
),
),
);
Thanks!
I am assuming that the card is placed in the center. Then you can get the coordinates with the following
Add this
onTapDown: (details) {
var position = details.globalPosition;
//Here it's checking if the click position is less than half of screen or more
if (position.dx < MediaQuery.of(context).size.width / 2){
print(" tapped left side");
} else {
print{"tapped right size");
}
},
While Kaushik had a good suggestion, for my case I ended up doing the following:
GestureDetector(
child: LayoutBuilder(builder: (ctx, constraints) {
return Align(
alignment: Alignment.centerRight,
child: Opacity(
opacity: 0.0,
child: Container(
color: Colors.black,
height: constraints.maxHeight,
width: constraints.maxWidth * 0.3,
),
),
);
}), onTap: () {
Provider.of<StingrayBloc>(context, listen: false).add(
IncrementImageUrlIndex(
imageUrlIndex: state.imageUrlIndexes[index],
stingrayIndex: index));
print(state.imageUrlIndexes[index]);
}),

how to change the border-radius while scrolling my list-view?

I'm working in a project on flutter and i have a problem with my listView.
While I scroll down on the feed, my list view radius seems like this(
the border is straight under the two photos):
So basically what I want is for the two photos to be as floating photos and the Latest posts to go under it.
But I want the list to look like this:
Does anyone have any idea about the problem?
check the code below:
Column(
children: [
Padding(
padding: const EdgeInsets.only(
left: 20.0, right: 20.0, top: 10.0, bottom: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: TeamImageContainer(
data: data[0],
onNavigate: () {
Navigator.of(context).pushNamed(
TeamStatisticViewScreen.routeName,
arguments: data[0].id);
},
),
),
SizedBox(
width: 10,
),
Expanded(
child: TeamImageContainer(
data: data[1],
onNavigate: () {
Navigator.of(context).pushNamed(
TeamStatisticViewScreen.routeName,
arguments: data[1].id);
},
),
),
],
),
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Container(
decoration: BoxDecoration(
// color: Colors.black38,
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.transparent, Colors.black38],
stops: [-0.1, 0.5],
tileMode: TileMode.clamp,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20.0)),
),
margin: const EdgeInsets.only(top: 20.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(
right: 20,
left: 20,
bottom: 20.0,
top: 20.0),
child: SizedBox(
width: double.infinity,
child: Align(
alignment: Alignment.topLeft,
child: Text(
'Latest Posts',
style: TextStyle(
color: Color(0xffe0e6f3),
fontSize: 18,
fontFamily: 'SFProText',
fontWeight: FontWeight.w600,
),
),
),
),
),
Column(
children: _postsData.loadedLatestPosts
.map(
(data) => PostsListWidget(data),
)
.toList(),
),
],
),
),
],
),
),
),
],
),
As far as I can tell, you need to focus on the class/element that is drawing the background. Also, consider declaring the border-radius in a const file so you don't have to hard code the values constantly, you just call "boxCurve" E.G. for your boxDecorationStyle.
It's tough to actually pinpoint what's wrong in your code without knowing which elements are drawn and where, if you can share the GitHub link then it might help :)
Try this :)
Container(
decoration: const BoxDecoration(
// color: Colors.black38,
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.purple, Colors.blue],
stops: [-5, 0.5],
tileMode: TileMode.clamp,
),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(5),
bottomRight: Radius.circular(5),
topLeft: Radius.circular(20),
topRight: Radius.circular(20)),
),
margin: const EdgeInsets.only(
top: 10.0,
bottom: 10,
left: 3,
right: 3,
),
child: Column(
children: const [
Padding(
padding: EdgeInsets.only(
right: 20, left: 20, bottom: 20.0, top: 20.0),
child: SizedBox(
child: Align(
alignment: Alignment.topLeft,
child: Text(
'Latest Posts',
style: TextStyle(
color: Color(0xffe0e6f3),
fontSize: 18,
fontFamily: 'SFProText',
fontWeight: FontWeight.w600,
),
),
),
),
),
MyResult
This is also what I meant earlier with your styles -
Create a new dart file called constants for example...
import 'package:flutter/material.dart';
const kLatestPosts = TextStyle(
color: Color(0xffe0e6f3),
fontSize: 18,
fontFamily: 'SFProText',
fontWeight: FontWeight.w600,
);
Copy that into it the constants file + import the file on the file you are working on, replace style: TextStyle(
with style: kLatestPosts,
Now you have tidier code + better performance too :)

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 do i fixed containers in Flutter?

I want to make first container fixed in center and second container fixed in right but i couldn't. I add mainAxisAlignment: MainAxisAlignment.spaceBetween in row line but it didn't work. Is there different properties to adjust position of widgets?
My Code:
new Container(
width: 360,
height: 502,
decoration: new BoxDecoration(
gradient: LinearGradient(
colors: [
Color(0xffffffff),
Color(0x00caebfe)
],
stops: [0,1]
)
),
child:Column(
//crossAxisAlignment: CrossAxisAlignment.start,
children:<Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
alignment: Alignment.center,
child: Text("JULY 2017",
style: TextStyle(
fontFamily: 'Baloo',
color: Color(0xff181743),
fontSize: 20,
fontWeight: FontWeight.w400,
fontStyle: FontStyle.normal,
)
)
),
Container(
alignment: Alignment.centerLeft,
//padding: EdgeInsets.only(left:90.0),
child: Column(
children: <Widget>[
new Container(
width: 14,
height: 5,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color(0xfffe1e9a),
Color(0xfffea64c)
],
stops: [0,1]
),
)
),
new Container(
//margin: EdgeInsets.only(top:3.0),
width: 23,
height: 5,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color(0xfffe1e9a),
Color(0xfffea64c)
],
stops: [0,1]
)
)
)
],)
)
]
)
]
)
),
Is there different properties to adjust position of widgets?
Following what you said you wanted to achieve, as in having the text centred and the gradients all the way to the right, I've modified your code by adding an Expanded widget around the first Container in Row and removing the width of your top Container:
Container(
height: 502,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color(0xffffffff),
Color(0x00caebfe)
],
stops: [0,1]
)
),
child: Column(
children:<Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
child: Center(
child: Text("JULY 2017",
style: TextStyle(
fontFamily: 'Baloo',
color: Color(0xff181743),
fontSize: 20,
fontWeight: FontWeight.w400,
fontStyle: FontStyle.normal,
)
),
),
),
Column(
children: <Widget>[
new Container(
width: 14,
height: 5,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color(0xfffe1e9a),
Color(0xfffea64c)
],
stops: [0,1]
),
)
),
new Container(
//margin: EdgeInsets.only(top:3.0),
width: 23,
height: 5,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color(0xfffe1e9a),
Color(0xfffea64c)
],
stops: [0,1]
)
)
)
],)
]
)
]
)
),
Are you trying to make your FirstContainer placed at the very center, and your SecondContainer at the very end?
If so, you might want to use Row, and Expanded
So it would be something like this:
Row(
mainAxisSize: MainAxisSize.max,
children: [
/// Just a placeholder container to occupy 1/3 of the Row
Expanded(child: Container()),
Expanded(child: FirstContainer()),
Expanded(child: SecondContainer()),
]
)
What this does, is add a placeholder container which occupies 1/3 of the Row, and your FirstContainer will be forced to be at the center, and SecondContainer section of the Row.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SafeArea(
child: Scaffold(
body: new Container(
width: 360,
height: 502,
decoration: new BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffffffff), Color(0x00caebfe)],
stops: [0, 1])),
child: Stack(
children: <Widget>[
Positioned(
right: 0,
child: Container(
alignment: Alignment.center,
child: Text("JULY 2017",
style: TextStyle(
fontFamily: 'Baloo',
color: Color(0xff181743),
fontSize: 20,
fontWeight: FontWeight.w400,
fontStyle: FontStyle.normal,
))),
),
Align(
alignment: Alignment.center,
child: Container(
child: Column(
children: <Widget>[
new Container(
width: 14,
height: 5,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(colors: [
Color(0xfffe1e9a),
Color(0xfffea64c)
], stops: [
0,
1
]))),
new Container(
//margin: EdgeInsets.only(top:3.0),
width: 23,
height: 5,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(colors: [
Color(0xfffe1e9a),
Color(0xfffea64c)
], stops: [
0,
1
])))
],
))),
])),
),
),
);
}
Check out this method using stack it makes it simple later can later modify thank-you.