How to add text on an Image in flutter? - flutter

Here's the code for an image with border,
Container(
child: Image.network(
"https://drive.google.com/uc?export=view&id=139Wu8bbLz5ubRECzdEmZof8crfGhx0oA", height: 140,fit: BoxFit.cover),
decoration: BoxDecoration(
border: Border.all(color: Colors.black12, width: 1),
),
),
How do I add text to it in such a way that I get an output like in the given image ?

you can use Stack, here is a sample:
Stack(
children: [
Container(
child: Image.network(
"https://drive.google.com/ucexport=view&id=1GgUaQNmNaY2h2oRqgBECQZzJ4I6MnV8G",
height: 140,
fit: BoxFit.cover,
),
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(7)),
border: Border.all(color: Colors.black12, width: 1),
),
),
Positioned(
child: Text(
'Vegetables',
style: TextStyle(
fontSize: 42,
shadows: <Shadow>[
Shadow(
offset: Offset(1.0, 5.0),
blurRadius: 2.0,
color: Color.fromARGB(255, 0, 0, 0),
),
],
),
),
top: 45,
left: 20,
)
],
)

Instead of having image as a child of Container, you can use image:ImageDecoration and have the Text as the child:
Container(
child: Text('Vegetables'),
decoration: BoxDecoration(
image: const DecorationImage(
image: NetworkImage(
'https://drive.google.com/ucexport=view&id=1GgUaQNmNaY2h2oRqgBECQZzJ4I6MnV8G'),
fit: BoxFit.cover,
),
border: Border.all(color: Colors.black12, width: 1),
),
),

Try below code hope its help to you
Container(
height: 100,
width: 200,
child: Text(
'data',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
decoration: BoxDecoration(
image: const DecorationImage(
image: NetworkImage(
'https://drive.google.com/uc?export=view&id=1GgUaQNmNaY2h2oRqgBECQZzJ4I6MnV8G'),
fit: BoxFit.cover,
),
border: Border.all(color: Colors.black12, width: 1),
),
),
Your result screen

You can use Stack Widget to make beautiful designed widget

Related

How do I create a selection avatar? in Flutter

I'm creating an app where the user can choose up to avatars, my question is how do I make the avatars have the following state: when selected, the selected avatar should display in a container.
below is an image of the avatar when selected.enter image description here
I'm creating an app where the user can choose up to avatars, my question is how do I make the avatars have the following state: when selected, the selected avatar should display in a container.
below is an image of the avatar when selected.enter image description here
Code:
//avatar selection
Center(
child: Container(
height: 130,
width: 130,
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius:
BorderRadius.circular(30),
boxShadow: const [
BoxShadow(
blurRadius: 5.0,
color: Colors.grey),
],
),
child: Image.asset(
'assets/images/purple_male.png',
fit: BoxFit.scaleDown,
)),
),
const SizedBox(height: 50),
Padding(
padding: const EdgeInsets.all(1),
child: Row(
children: [
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(
50),
boxShadow: const [
BoxShadow(
blurRadius: 5.0,
color: Color.fromARGB(
255,
209,
209,
209)),
],
),
height: 90,
width: 90,
child: Image.asset(
'assets/images/purple_male2.png',
fit: BoxFit.scaleDown,
),
),
const SizedBox(width: 20),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(
50),
boxShadow: const [
BoxShadow(
blurRadius: 5.0,
color: Color.fromARGB(
255,
209,
209,
209)),
],
),
height: 90,
width: 90,
child: Image.asset(
'assets/images/purple_male.png',
fit: BoxFit.scaleDown,
),
),
],
),
),
),
),
);

Container in flutter

Hope you all are doing well. I am making a flutter application. In this interface I am having a issue. I am making a container and circle avatar which are in stack. In stack, in first widget container there is also another container. When I set image as a child to inner container it also applied to outer container. Why is this happening and what is the solution of this problem.
Stack(
children: [
// Container(child:
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.indigo,
)),
width: 340,
height: 200,
child: Container(
child: Image.asset(
'assets/pic1.jpg',
fit: BoxFit.cover,
),
width: 340,
height: 150,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
)),
),
),
Positioned(
bottom: 10,
right: 10,
child: CircleAvatar(
backgroundColor: Colors.grey,
radius: 40,
))
],
)
I think You just need to Alignment Property to your Parent widget otherwise child widget will not care about height and width
Stack(
children: [
// Container(child:
Container(
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border.all(
color: Colors.indigo,
)),
width: 340,
height: 200,
child: Container(
child: Image.asset(
'assets/pic1.jpg',
fit: BoxFit.cover,
),
width: 340,
height: 150,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
)),
),
),
Positioned(
bottom: 10,
right: 10,
child: CircleAvatar(
backgroundColor: Colors.grey,
radius: 40,
))
],
)
You can also follow the below article to get more information.
https://www.kindacode.com/article/flutter-sizing-a-container-inside-another-container/#:~:text=To%20size%20a%20Container%20that,expand%20to%20its%20parent's%20constraints.

How to make an Icon overlay over an image

I'm new to flutter and not that experienced when it comes to programming in general. So I'm trying to do something like this
But I just can't seem to do it, I tried align properties, padding to make it move left and right, I also tried stacks but I don't have any idea how to do it.
This is my code for the image
Widget buildImage() {
return Padding(
padding: EdgeInsets.only(right: 10, left: 10, top: 20),
child: Align(
alignment: Alignment.bottomLeft,
child: Container(
height: 100,
width: 130,
decoration: BoxDecoration(
image: new DecorationImage(
image: new AssetImage(
"assets/images/profile-image.jpeg",
),
fit: BoxFit.cover,
),
shape: BoxShape.circle,
border: Border.all(
color: Colors.white,
width: 5.0,
),
),
),
));
}
This is my current progress
I would appreciate any help.
Here is the working code
Stack(
alignment: Alignment.bottomRight,
children: <Widget>[
Container(
height: 200,
width: 200,
child: InkWell(
onTap: () => {},
child: ClipRRect(
borderRadius: BorderRadius.circular(100),
child: Container(
height: 200,
width: 200,
color: Colors.grey[200],
child: Icon(Icons.person),
),
),
),
),
Container(
height: 60,
width: 60,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
padding: EdgeInsets.all(5),
child: Container(
decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.grey[200]),
child: IconButton(
icon: Icon(Icons.edit),
onPressed: () {},
),
),
),
],
);
This can be achieved with the Stack widget check the docs it has a video explaining it really well.
It lets you positions its children relative to the edges of its box.
You can use Stack to place widget over widget. Here is the working example
Stack(
alignment: Alignment.bottomRight,
children: <Widget>[
Container(
decoration: BoxDecoration(
image: new DecorationImage(
image: new AssetImage(
"assets/images/profile-image.jpeg",
),
fit: BoxFit.cover,
),
shape: BoxShape.circle,
border: Border.all(
color: Colors.white,
width: 5.0,
)),
height: 100,
width: 130,
),
Container(
height: 50,
width: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white,
),
padding: EdgeInsets.all(5),
child: Container(
decoration:
BoxDecoration(shape: BoxShape.circle, color: Colors.blue),
child: Center(
child: IconButton(
icon: Icon(Icons.edit),
onPressed: () {},
),
),
),
),
],
)

How to add an icon with opacity layer over circular image in Flutter

I need to add camera icon with opacity layer over circular image.
I tried below code:
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(userInfo.imageUrl),
fit: BoxFit.cover,
),
),
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 5),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3),
),
child: Icon(
Icons.photo_camera,
color: Colors.white.withOpacity(0.5),
),
),
),
)
But I did not get the expected result:
Anyone have an idea how to make it works?
Thanks
Wrap it in the ClipRRect widget like this
Container(
height: 100,
width: 100,
child: ClipRRect(
borderRadius: BorderRadius.circular(50.0),
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage("assets/imgs/avatar-default.png"),
fit: BoxFit.cover,
),
),
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 5),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3),
),
child: Icon(
Icons.photo_camera,
color: Colors.white.withOpacity(0.5),
),
),
),
),
),
),
You could also use ClipOval
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(userInfo.imageUrl),
fit: BoxFit.cover,
),
),
child: ClipOval(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 5),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3),
),
child: Icon(
Icons.photo_camera,
color: Colors.white.withOpacity(0.5),
),
),
),
)
),

Flutter image does not fit in

Container(
width:
MediaQuery.of(context).size.width / 4.4,
height:
MediaQuery.of(context).size.height / 10,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black12,
width: 1.5,
style: BorderStyle.solid),
borderRadius:
BorderRadius.all(Radius.circular(20)),
),
child: (TmpBytesImage == null)
? Icon(Icons.camera_alt,
color: Colors.black26)
: Image.memory(TmpBytesImage,
fit: BoxFit.fill),
),
I expected was to fill Container with images.
but image looks like ignore BoxDecoration
How to get image to look like this Container?
Simply use Card, give it shape and put your Container inside it. Simple example:
Card(
elevation: 12,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20)),
child: Container(
padding: EdgeInsets.all(20),
width: MediaQuery.of(context).size.width / 2,
height: MediaQuery.of(context).size.height / 4,
child: Image.asset(
chocolateImage,
fit: BoxFit.fill,
),
),
),
),
try to add the image inside the container decoration
like this
decoration: BoxDecoration(
image: DecorationImage(
image:
AssetImage('assets/image.jpg'),
fit: BoxFit.cover,
),