I have like this code to build youtube player thumbnail with icon:
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://img.youtube.com/vi/_uOgXpEHNbc/0.jpg"),
fit: BoxFit.fitHeight),
),
),
Container(
color: Colors.black.withOpacity(0.7),
),
Container(
alignment: Alignment.center,
child: Center(
child: Icon(
Icons.smart_display,
color: Colors.red,
size: 100.0,
),
),
),
],
),
This is result of code:
How to make sure that the transparent background of the picture does not affect the central icon? I just need the icon to stay light.
If you want to keep white color of the Play icon in the center then you can try like this
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://img.youtube.com/vi/_uOgXpEHNbc/0.jpg"),
fit: BoxFit.fill),
),
),
Container(
color: Colors.black.withOpacity(0.7),
),
Container(
alignment: Alignment.center,
child: Center(
child: Container(
height: 50,
width: 60,
color: Colors.white,
),
),
),
Container(
alignment: Alignment.center,
child: Center(
child: Icon(
Icons.smart_display,
color: Colors.red,
size: 100.0,
),
),
),
],
)
You can use Color(0xFF000000).withOpacity(0.7) inside your parent container so it will affect only your parent. You can try here
return Center(
child: Stack(
children: [
Center(
child: Container(
height: 200,
width: 200,
decoration: new BoxDecoration(
image: new DecorationImage(
image: new NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRVuX218dvGRwSyGhdr1KeTrN6wocBci6xWWQ&usqp=CAU'),
fit: BoxFit.cover,
),
),
child: Container(
color: Color(0xFF000000).withOpacity(0.7),
),
),
),
Center(
child: Container(
child: IconButton(
onPressed: () {},
icon: Image(
image: NetworkImage(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTRtN8lufTjm-VwUnxm5uAwsW5dCrZqzFFWxg&usqp=CAU"),
),
iconSize: 80,
splashColor: Colors.red,
),
),
),
],
),
);
I use the worst possible images to showcase the example. Don't mind
Output
Related
I'm trying to create an overlay above an image, which should match the size of the image no matter which size I give the Container.
This is my code I tried so far.
Container(
width: double.infinity,
height: double.infinity,
color: Colors.red,
child: Stack(
children: [
Image.memory(
snapshot.data!,
fit: BoxFit.contain,
),
Positioned.fill(
child: Container(
color: Colors.grey.withOpacity(0.5),
),
),
],
),
),
This results to this image (https://i.stack.imgur.com/eL3Ig.png)
I expect that the grey overlay matches the size of the image.
Move the Stack into a FittedBox:
Container(
width: double.infinity,
height: double.infinity,
color: Colors.red,
child: FittedBox(
child: Stack(
children: [
Image.asset(
'images/default.png',
),
Positioned.fill(
child: Container(
color: Colors.grey.withOpacity(0.5),
child: const Center(
child: Text('Overlay'),
),
),
),
],
),
),
),
Give your Stack a fit: Stackfit.expand, so it expands to the Container size.
If this doesn't work you can try it with a BoxDecoration - found here https://medium.com/ariel-mejia-dev/make-an-image-with-opacity-layer-in-flutter-fca77e453731
decoration:
BoxDecoration(
color: const Color(0xff7c94b6),
image: new DecorationImage(
fit: BoxFit.cover,
colorFilter:
ColorFilter.mode(Colors.black.withOpacity(0.2),
BlendMode.dstATop),
image: new NetworkImage(
'http://www.server.com/image.jpg',
),
),
),
How should I do to add text inside a widget, so that in this way I can notify the user of my app that they can change their profile picture by pressing that camera button I want to insert the text "Change your profile picture here" into the widget _Foto?
my goal is that under the profile image there is a text so that the user knows that he can change his profile image by clicking on the button
Widget _Foto() {
return new GestureDetector(
onTap: () => imagePicker.showDialog(context),
child: new Center(
child: _image == null
? FutureBuilder<String>(
future: globals.SharedPreferencesHelper
.getDataAgentUserProfileImage(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return new Stack(children: [
Container(
margin: EdgeInsets.only(top: 48),
height: 0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.0),
),
),
Align(
alignment: Alignment.topCenter,
child: SizedBox(
child: CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 25.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: new NetworkImage(
systemProfilesUsersImagesRepository +
snapshot.data!),
),
),
)),
]);
} else if (snapshot.hasError) {
return Stack(
children: <Widget>[
new Center(
child: new CircleAvatar(
radius: 50.0,
backgroundColor: Colors.white,
),
),
new Center(
child: Container(
width: 100.0,
height: 100.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: new AssetImage(
"images/userpic.png")))),
),
],
);
} else {
return Container(
width: 100.0,
height: 100.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image:
new AssetImage("images/userpic.png"))));
}
})
: new Container(
height: 100.0,
width: 100.0,
child: new CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 23.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: FileImage(_image!),
),
),
)),
);
You can wrap the Center widget Column and include another Text child.
Widget _Foto() {
return GestureDetector(
onTap: () => imagePicker.showDialog(context),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Center(
child: _image == null
? FutureBuilder<String>(
future: globals.SharedPreferencesHelper
.getDataAgentUserProfileImage(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Stack(children: [
Container(
margin: EdgeInsets.only(top: 48),
height: 0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.0),
),
),
Align(
alignment: Alignment.topCenter,
child: SizedBox(
child: CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 25.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: NetworkImage(
systemProfilesUsersImagesRepository +
snapshot.data!),
),
),
)),
]);
} else if (snapshot.hasError) {
return Stack(
children: <Widget>[
Center(
child: CircleAvatar(
radius: 50.0,
backgroundColor: Colors.white,
),
),
Center(
child: Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage(
"images/userpic.png")))),
),
],
);
} else {
return Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image:
AssetImage("images/userpic.png"))));
}
})
: Container(
height: 100.0,
width: 100.0,
child: CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 23.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: FileImage(_image!),
),
),
)),
Text("Change your profile picture here")
],
),
);
}
You can add a column and display it
CircleAvatar(
child: Column(
mainAxisSize: MainAxisSize.min,
children : [
Icon(Icons.camera),
Flexible(
child: Text("click here to change the profile pic")
),
]
)
)
I want to make a design like following
I build the base layout with background image. Following is the code to achieve that. Now i want to put "Grocery store" Text on top of this image and other widgets. How can i do that ?
Widget build(BuildContext context) {
return Container(
height: 190.0,
width: size.width,
margin: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
width: size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(11.0),
image: DecorationImage(
image: NetworkImage(https://www.exampledomain.com/images/background.jpg),
fit: BoxFit.cover,
),
),
// child: ????
),
),
],
),
),
],
),
);}
child: Container(
width: size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(11.0),
image: DecorationImage(
image: NetworkImage(https://www.exampledomain.com/images/background.jpg),
fit: BoxFit.cover,
),
),
child: Text('Grocery store'),
//padding: <-- Using to shift text position a little bit for your requirement
),
Stack/Align is your friend.
Take a look here https://api.flutter.dev/flutter/widgets/Stack-class.html
Here is a basic example (based in your code):
Widget build(BuildContext context) {
return Stack(
children: [
Align(
alignment: Alignment.center,
child: Container(
alignment: Alignment.center,
color: Colors.red,
height: 190.0,
width: size.width,
margin: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
width: size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(11.0),
image: DecorationImage(
image: NetworkImage(
"http://www.exampledomain.com/images/background.jpg"),
//fit: BoxFit.cover,
),
),
),
),
],
),
),
],
),
),
),
Align(
alignment: Alignment.center,
child: Text("Grocery store"))
]
);
}
I have this flutter page
I want this flowers image background to go all over the app bar as well with text MyProfile with other app bar buttons such as drawer and search button overlaying it.
Here is code as of now
Container(
child: Stack(
alignment: Alignment.bottomCenter,
overflow: Overflow.visible,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
height: 200.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://timelinecovers.pro/facebook-cover/download/stunning-little-flowers-facebook-cover.jpg'))),
),
)
],
),
Positioned(
top: 100.0,
child: Container(
height: 190.0,
width: 190.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://static.displate.com/280x392/displate/2020-06-20/97526a531e9ff32c26f7752ebc926941_07a032cb55575a397e6ba8c98804ad43.jpgD'),
),
border:
Border.all(color: Colors.white, width: 6.0)),
),
),
],
),
),
some change on your code and result:
Widget build(BuildContext context) {
var width = MediaQuery.of(context).size.width;
var height = MediaQuery.of(context).size.height;
return SafeArea(child: Scaffold(
body: Container(
child: Stack(
alignment: Alignment.bottomCenter,
overflow: Overflow.visible,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
height: 240.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://timelinecovers.pro/facebook-cover/download/stunning-little-flowers-facebook-cover.jpg'))),
),
)
],
),
Positioned(
top: 0.0,
child: Container(
width: width,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
IconButton(icon: Icon(Icons.menu,color: Colors.white,), onPressed: (){}),
Text('My Profile',style: TextStyle(color: Colors.white,fontSize: 22),),
],
),
IconButton(icon: Icon(Icons.notifications,color: Colors.white,), onPressed: (){}),
],
),
height: 52,
),
),
Positioned(
top: 120.0,
child: Container(
height: 190.0,
width: 190.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://static.displate.com/280x392/displate/2020-06-20/97526a531e9ff32c26f7752ebc926941_07a032cb55575a397e6ba8c98804ad43.jpgD'),
),
border:
Border.all(color: Colors.white, width: 6.0)),
),
),
],
),
),
));
}```
You can easily achieve this by setting:
extendBodyBehindAppBar: true property in Scaffold widget.
backgroundColor: Colors.transparent and set elevation: 0.0 in AppBar widget to make it fully transparent.
If true, and an appBar is specified, then the height of the body is
extended to include the height of the app bar and the top of the body
is aligned with the top of the app bar.
This is useful if the app bar's AppBar.backgroundColor is not
completely opaque.
Source: extendBodyBehindAppBar property from Flutter Doc
After this, adjust the height for:
background image
profile image
I copied your code and adjust it with the above solution, see the example below:
#override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
title: Text(widget.title),
leading: Icon(Icons.list),
backgroundColor: Colors.transparent,
elevation: 0.0,
),
body: Container(
child: Stack(
alignment: Alignment.bottomCenter,
overflow: Overflow.visible,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
height: 300.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://timelinecovers.pro/facebook-cover/download/stunning-little-flowers-facebook-cover.jpg'))),
),
)
],
),
Positioned(
top: 180.0,
child: Container(
height: 190.0,
width: 190.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://static.displate.com/280x392/displate/2020-06-20/97526a531e9ff32c26f7752ebc926941_07a032cb55575a397e6ba8c98804ad43.jpgD'),
),
border: Border.all(color: Colors.white, width: 6.0)),
),
),
],
),
),
);
}
Please refer to this property of the Scaffold widget in Flutter. It will do what you want. You might have to resize things a bit. And maybe, make the AppBar color as transparent.
I have an image like this
and i need to add widgets upon this image. It should look like the contents are just on the mobile screen. I tried searching for a way but i dint get any. I tried using stack like this:
Container(
child: Stack(
children: <Widget>[
Positioned(
child: Container(
padding: EdgeInsets.fromLTRB(60, 110, 60, 90),
//body
),
),
Container(width: 1000,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/transparentMobile.png'),
fit: BoxFit.fill)),
),
],
),
),
It worked correctly on my phone:
but on other phone it looks very different. How do i write a single code for multiple screens?
Finally i got a way to do it. Instead of working hard on the code, i just cropped the image into 3 parts :
top :
body:
bottom:
and added them to columns like this:
AspectRatio(
aspectRatio: 1/2,
child: Container(
padding: EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/Mobile/mobileTop.png'),
fit: BoxFit.fill
)
),
),
),
Expanded(
flex: 7,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/Mobile/mobileBody.png',),
fit: BoxFit.fill
)
),
height: 100,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
automaticallyImplyLeading: false,
title: Text('My Application'),
),
body: Center(
child: Text(
'Hello World!',
style: TextStyle(color: Colors.black),
),
),
backgroundColor: Colors.white,
),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/Mobile/mobileBottom.png'),
fit: BoxFit.fill
)
),
),
),
],
),
),
),
and the image now looks much similar on most of the devices now:
Thanks for your help!