Container color in flutter - flutter

I want a colour for container but getting error while using colour.
My code:
Container(
color:Colors.red,
width: 50,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50)
)
),

You can't use "color" and "decoration" at the same time. You need to pass the "color" parameter to the "BoxDecoration" widget like this:
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color:Colors.red,
borderRadius: BorderRadius.circular(50)
)
),

You cannot add color to the container if you add decorations. The solution is to enter color into the decoration box as follows
Container(
width: 50,
height: 50,
decoration: BoxDecoration(color: Color.red)
)

Please use color in BoxDecoration like this
Container( width: 50, height: 50, decoration: BoxDecoration( borderRadius: BorderRadius.circular(50),color:Colors.red,)),

You can't give both colors and decoration to Container(). If you are looking to give color and decoration to Container, give color inside Boxdecoration
Container(
width: 50,
height: 50,
decoration: BoxDecoration(color: Color.red)
)

Use color inside container directly to set the background for the children. If you use any decoration then it will conflict with outside color. In those case you need to give color inside decoration.
FullCode : here
Column(
children: [
Container(
padding: EdgeInsets.all(10),
color: Colors.green,
child: Text('test'),
),
SizedBox(height: 10),
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(10),
),
child: Text('test'),
)
],
)
Output for the above code is

The container does not accept the color parameter when there is a decoration parameter. This happens because the BoxDecoration has a color parameter. Just put the color inside the BoxDecoration and remove the color parameter from the Container and your problem will be solved.
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Colors.red
)
),

Related

How to create this curved effect using flutter with clippath

HOW TO CREATE THIS?
Its like a ListTile or something with this curve effect in start of it, as of now it won't animate just the next option will be selected onTap but I'm unable to create this effect. I tried it with radius but that's not resulting out to be exact match of this. The code is below.
MY TRY SO FAR
Code
Container(
color: AppTheme.c.primary,
width: 200,
height: 60,
child: Row(
children: [
Container(
width: 50,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.horizontal(
right: Radius.elliptical(
MediaQuery.of(context).size.width,
MediaQuery.of(context).size.height,
),
),
),
),
],
),
),

Can't resize svg picture in flutter

I need to make an svg image size like on the photo below
But when i'm trying to give this size through SvgPicture.asset constructor or with a SizedBox it doesn't make any effect and i'm getting something like this
here is my code of that widget
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: yellowGradient,
),
child: SizedBox(
height: 29,
width: 45,
child: SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
),
),
),
I think that my Svg picture stretches to container size but i don't know what causes such a behaviour because there is SizedBox
Container doesn't know how to align the child if it is smaller than the container itself, so it expands it to fill the containers size.
The SizedBox can also be removed as SvgPicture.asset(...) now accepts size parameters.
Based on the above information, a possible solution for you will be the following, the important bit being the addition of alignment: Alignment.center:
Container(
width: 80,
height: 80,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: yellowGradient,
),
child: SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
height: 29,
width: 45,
),
),
For further information, refer to "Layout behaviour" in the Container widget documentation here.
you can use padding or margin
Container(
width: 40,
height: 40,
padding: const EdgeInsets.all(15),
child: SvgPicture.asset(
"assets/icons/outline/edit2.svg",
),
);
Instead of wrapping it with a SizedBox, use the fields height and width of the SvgPicture.
SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
height: 29,
width: 45,
),

RectangleBox In Flutter

In my code, there is this CircleAvtar which I want to replace it with a big rectangular box. I am new to flutter I am finding it difficult to achieve this.
child: Card(
elevation: 3,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
child: Container(
margin: EdgeInsets.all(5),
padding: EdgeInsets.all(5),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
10,
),
// border: Border.all(width: 0.5),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5)),
color: whiteColor,
),
child: expertiseSearchModel.userImageURL.isEmpty
? CircleAvatar(
radius: 35,
child: SvgPicture.asset(
'assets/images/default_user_image.svg',
// height: screenUtil.setSp(80),
// width: screenUtil.setSp(80),
fit: BoxFit.contain,
),
)
: CircleAvatar(
radius: 35,
backgroundImage:
NetworkImage(expertiseSearchModel.userImageURL),
),
),
I want it to look like this :
If you can show us what your current code is giving the output maybe I can help you more. But As far as i understood this, you can simple use Image.Network() to show the image and remove the circular avatar
Try this, if it works if not lemme know I will edit it accordingly
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5)),
color: whiteColor,
),
child: expertiseSearchModel.userImageURL.isEmpty
? CircleAvatar(
radius: 35,
child: SvgPicture.asset(
'assets/images/default_user_image.svg',
// height: screenUtil.setSp(80),
// width: screenUtil.setSp(80),
fit: BoxFit.contain,
),
)
: Image.network(expertiseSearchModel.userImageURL)
),
Inside your main container use the column as a child and put another container as a child and use the Image decoration property of the container to display pictures. You can change the border-radius of the child container to have those circular edges.

Set Container height equal to width in Flutter

I am learning Flutter development. I want to display some rounded square shaped containers with short text in a row. However, the possible shapes are either a circle or rectangle. So I decided to set the width and height of the rectangle so that they would be equal.
The following is the code I used to create the Container
Container(
width: double.maxFinite,
height:
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
child: Text(keyText),
),
When I set the shape in Box decoration alone, the Container resized to the size of the text. When I set the width property of the Container, I was able to divide the available width of the screen among the containers, which ensures flexibility of size if the screen was smaller or bigger, instead of hard coding it. Now I want to make the height the exact same size that the width turns out to be, thus obtaining a square container. Any idea how I could do that?
EDIT: I tried the following code and this is how the containers are looking like in both cases:
Container(
alignment: Alignment.center,
width: double.maxFinite,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
After using MediaQuery.of(context).size.width for height property or both
Here is a solution without using MediaQuery:
AspectRatio(
aspectRatio: 1,
child: Container(
width: double.infinity,
child: Text(),
),
)
Use MediaQuery. MediaQuery.of(context).size.width gives you the Screen width size value.
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
child: Text(keyText),
)
check this quick example that I made just for you!
// This function is just an example
List<Widget> buildContainers(BuildContext context, int containerCount) {
// Here you get the width of all your containers
final containerWidth = MediaQuery.of(context).size.width / containerCount;
// Make height = width for squares!
final containerHeight = containerWidth;
// We will gather all containers in this list
List containerList = <Container>[];
for (var i = 0; i < containerCount; i++) {
containerList.add(
Container(
// Use the predefined width and height here!
width: containerWidth,
height: containerHeight,
// Random color to differentiate each container
// You could replace this with your child widget
color: Color(
Random().nextInt(0xFFFFFFFF),
),
),
);
}
return containerList;
}
#override
Widget build(BuildContext context) {
return Container(
child: Row(
children: [
// If you are wondering what are these dots (...)
// they are called "spread operators" and you can find more about them here:
// https://dart.dev/guides/language/language-tour#spread-operator
...buildContainers(context, 7),
],
),
);
}
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
child: Text(keyText),
),

How to add gradient color to Divider in Flutter

I have a Divider widget with a solid color but I want to set it to a gradient color. Is there a way to do this?
Divider(
height: 20,
thickness: 2.5,
indent: 0,
endIndent: 100,
)
Just use a Container() with [BoxDecoration][1] to create a gradient.
SizedBox(
width: 200,
height: 4,
child: Container(
decoration: BoxDecoration(
gradient: //...
),
),
),
The pre-defined divider is good but not powerful when it comes to customization.
[1]: https://api.flutter.dev/flutter/painting/BoxDecoration-class.html
If you wish to use the exact same parameters as the official Divider and yet have the possibility to gradient AND to round these nasty square sides, you can use this ds is my DividerStyle containing the parameters:
return SizedBox(
height: ds.heigth,
child: Center(
child: Container(
height: ds.thickness,
margin: EdgeInsetsDirectional.only(start: ds.indent, end: ds.endIndent),
decoration: BoxDecoration(
color: ds.color.getColor(),
gradient: ds.color.getGradient(),
borderRadius: ds.roundEdge
? BorderRadius.all(Radius.circular(ds.thickness))
: null,
border: Border.all(color: Colors.transparent, width: 1),
),
),
),
);