I had designed a picture with a LinearGradient on it like this
Container(
width: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(controller
.specialEpisodes[pagePosition].appImage),
fit: BoxFit.cover)),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
const Color(0xff21242C),
const Color(0xff21242C).withOpacity(.2),
const Color(0xff21242C).withOpacity(.2),
const Color(0xff21242C).withOpacity(.9),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: const [0, 0.2, 0.7, 3],
)),
),
);
But after knowing the importance of CachedNetworkImage have implemented it as follows
CachedNetworkImage(
width: double.infinity,
height: 130,
fit: BoxFit.contain,
imageUrl:
controller.specialEpisodes[pagePosition].appImage,
progressIndicatorBuilder:
(context, url, downloadProgress) =>
Shimmer.fromColors(
baseColor: const Color(0xff21242C),
highlightColor: Colors.white,
child: Skeleton(
width: double.infinity,
height: SizeConfig.screenHeight * 0.7,
)),
errorWidget: (context, url, error) =>
const Icon(
Icons.error,
size: 100,
color: Colors.red,
),
);
How do I add the LinearGradient to it
thank you
CachedNetworkImage also provide imageBuilder, you can use it.
CachedNetworkImage(
imageUrl: "",
imageBuilder: (context, imageProvider) {
return Container(
width: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
const Color(0xff21242C),
const Color(0xff21242C).withOpacity(.2),
const Color(0xff21242C).withOpacity(.2),
const Color(0xff21242C).withOpacity(.9),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: const [0, 0.2, 0.7, 3],
)),
),
);
},
progressIndicatorBuilder: (context, url, progress) =>
CircularProgressIndicator(),
),
Related
Here, by action i mean, Loading other widgets aswell
I am using CachedNetworkImage package. And it takes some time for the image to load from the network. I want to exactly know when the image is totally loaded so that i can show my other widgets.
Problem: Image takes time to load but the text already gets displayed which looks unprofessional.
So is there any way to know when the image is completely loaded from network. i.e change from placeholder to image.
child: Stack(
children: [
CachedNetworkImage(
fadeInDuration: const Duration(milliseconds: 300),
imageUrl: image,
height: double.infinity,
fit: BoxFit.cover,
imageBuilder: (context, imageProvider) => Stack(children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider, fit: BoxFit.cover))),
Container(
color: Colors.black.withOpacity(0.4),
)
]),
),
Padding(
padding: const EdgeInsets.only(left: 2.0, bottom: 2),
child: Align(
alignment: Alignment.bottomLeft,
child: Text(title), <-- This gets displayed before image is loaded
),
Visibility(
visible: discount != 0,
child: Align(
alignment: Alignment.topRight,
child: Container(
margin: const EdgeInsets.only(top: 2, right: 2),
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: const LinearGradient(
begin: Alignment(-1.238, -1.313),
end: Alignment(3.464, 3.196),
colors: <Color>[
Color(0xfff9d423),
Color(0xfffc8e3a),
Color(0xffff4e50)
],
stops: <double>[0.038, 0.5, 0.928],
),
),
child: Text("$discount%"), <-- This gets displayed before image is loaded
),
),
)
],
),
You can put the Stack inside the imageBuilder of a CachedNetworkImage, so everything will be displayed only after the image is loaded.
SizedBox(
height: 100,
width: 100,
child: CachedNetworkImage(
fadeInDuration: const Duration(milliseconds: 300),
imageUrl: image,
height: double.infinity,
fit: BoxFit.cover,
imageBuilder: (context, imageProvider) => Stack(
children: [
Stack(children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(image: imageProvider, fit: BoxFit.cover))),
Container(
color: Colors.black.withOpacity(0.4),
)
]),
Padding(
padding: const EdgeInsets.only(left: 2.0, bottom: 2),
child: Align(
alignment: Alignment.bottomLeft,
child: Text(title),
),
),
Visibility(
visible: discount != 0,
child: Align(
alignment: Alignment.topRight,
child: Container(
margin: const EdgeInsets.only(top: 2, right: 2),
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: const LinearGradient(
begin: Alignment(-1.238, -1.313),
end: Alignment(3.464, 3.196),
colors: <Color>[Color(0xfff9d423), Color(0xfffc8e3a), Color(0xffff4e50)],
stops: <double>[0.038, 0.5, 0.928],
),
),
child: Text("$discount%"),
),
),
)
],
),
),
),
I am trying to fit image to the whole screen. I am using media query to fill the image for the whole screen, but the problem is image is scrolling when using media query full height. Is it possible to show image without scrolling to fill the whole screen.
return Stack(
children: [
Container(
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage('assets/welcome_screen.png'),fit:BoxFit.fill),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 50,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
end: const Alignment(0.0, -1),
begin: const Alignment(0.0, 0.4),
colors: <Color>[const Color(0x8A000000), Colors.black12.withOpacity(0.0)],
),
),
),
),
You can try this:
return Stack(
children: [
Column(
children: [
Expanded(
child: Container(
//height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage('assets/welcome_screen.png'),fit:BoxFit.fill),
),
),
),
]
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 50,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
end: const Alignment(0.0, -1),
begin: const Alignment(0.0, 0.4),
colors: <Color>[const Color(0x8A000000), Colors.black12.withOpacity(0.0)],
),
),
),
),
final PlatformFile image;
final double size;
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(image.path!),
),
you need to used FileImage instead of AssetImage
decoration: new BoxDecoration(
color: Colors.transparent,
image: new DecorationImage(
image: new FileImage(File(image.path)),
fit: BoxFit.cover,
),
),
Use FileImage instead of AssetImage if you are trying to take from local path.
You can also do one thing check the _image value if it null shows image through your assets if not then you call it through FileImage(_image!)
//////code
File ?_image;
_image==null?Stack(
children: [
Container(
height: 150,
width:_width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 120,
width: 120,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(60),
border: Border.all(color: AppColors.white, width: 2.0),
image: DecorationImage(
image: AssetImage("assets/managerPicture.jpeg"),fit: BoxFit.cover,
)
/*gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Color(0xff00519E),
AppColors.blue,
Colors.blue.shade900,
],
)*/
),
/* child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Image.asset("assets/manager.jpeg",height: 100,width: 100,)
//Icon(Icons.person, size: 100),
// child: Image.asset("assets/logo/profile.png", fit: BoxFit.fill),
),*/
),
],
),
),
Positioned(
top: 60,
right: 0,
left: 100,
bottom: 0,
child: GestureDetector(
onTap: (){
_showChoiceDialog(context);
},
child: Container(
height: 10,
width: _width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 35,
width: 35,
decoration: BoxDecoration(
color: AppColors.profiePicBackground,
borderRadius: BorderRadius.circular(60),
//border: Border.all(color: Colors.blue.shade900, width: 2.0),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Icon(MdiIcons.squareEditOutline,color: AppColors.popUpBlueColor,size: 20,),
),
),
],
),
),
),)
],
):Stack(
children: [
Container(
height: 150,
width:_width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 120,
width: 120,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: FileImage(_image!),
),
border: Border.all(color: AppColors.white, width: 2.0),
),
),
],
),
),
Positioned(
top: 60,
right: 0,
left: 100,
bottom: 0,
child: GestureDetector(
onTap: (){
_showChoiceDialog(context);
},
child: Container(
height: 10,
width: _width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 35,
width: 35,
decoration: BoxDecoration(
color: AppColors.profiePicBackground,
borderRadius: BorderRadius.circular(60),
//border: Border.all(color: Colors.blue.shade900, width: 2.0),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Icon(MdiIcons.squareEditOutline,color: AppColors.popUpBlueColor,size: 20,),
),
),
],
),
),
),)
],
)
So i want to achieve this transition color with flutter
There is a container that contain Text tile with blue color () that changing to transparent. Here is what i do so far. I'm trying to use stack and LinearGradient
return Stack(
children: [
InkWell(
onTap: () {},
child: AspectRatio(
aspectRatio: 16 / 9,
child: pageData.thumbnail == ""
? ThumnailError()
: CachedNetworkImage(
imageUrl: "https://via.placeholder.com/200x100",
placeholder: (context, url) => MyLoader(),
errorWidget: (context, url, error) => ThumnailError(),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
),
),
),
),
Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.all(10.h),
decoration: BoxDecoration(
color: Colors.white,
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
colors: [
AppColors.primary,
Colors.transparent,
],
stops: [
0.2,
0.33
]),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
color: AppColors.primary,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextLarge(
label: "Title",
color: Colors.white,
fontWeight: FontWeight.bold,
),
Row(
children: [
TextMedium(
label: "Category",
color: Colors.white,
),
SizedBox(
width: 10.w,
),
TextMedium(
label: pageData.duration ?? "",
color: Colors.white,
)
],
),
],
),
),
),
],
);
and here is the result
So how can i achieve the first image that i put above ? thanks in advance and sorry for my english
You can try ShaderMask to achieve the desired result.
Stack(
children: [
ClipRRect(
child: ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.transparent,
Colors.black
]
).createShader(bounds);
},
blendMode: BlendMode.darken,
child: Container(
width: 300,
height: 250,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/someimage.jpg'),
fit: BoxFit.cover,
),
)
))
)
]
);
ShaderMask Widget
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),
),
),
),
)
),