I am trying to overlay a title on images in Card widget. I am trying to achieve this using Stack widget, it seems to be messing with my constraints. My original widget:
When I add Stack:
As you can see, the text appears but it destroys the layouts. Any idea to why is this happening? to my knowledge Stack is not supposed to affect any constraints(?), it should be floating on top of the existing widget.
My code:
return Card(
clipBehavior: Clip.antiAlias,
elevation: 3,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kDefaultPadding / 2)),
child: Stack(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(kDefaultPadding / 2),
child: FittedBox(
fit: BoxFit.cover,
child: Image.network(widget.data.imageUrl),
),
),
Text("Test")
],
),
);
Add fit: StackFit.expand to the Stack:
Card(
clipBehavior: Clip.antiAlias,
elevation: 3,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(kDefaultPadding / 2)),),
child: Stack(
fit: StackFit.expand, // this is new
children: [
ClipRRect(
...
Note: Instead of Stack you can use a Container with the image set as its decoration image and the text set as its child:
Card(
clipBehavior: Clip.antiAlias,
elevation: 3,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(kDefaultPadding / 2)),),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(widget.data.imageUrl),
),
),
child: Center(child: Text("Test"))),
),
...
Related
The images I have been adding are not fitting properly in the circular shape.
This is the image for reference
And this is the code for reference
Container(
child:Material(
shape: CircleBorder(),
clipBehavior: Clip.antiAliasWithSaveLayer,
child: InkWell(
splashColor: Colors.black26,
onTap: (){},
child: Ink.image(
image: AssetImage('assets/'+ Name),
height: 60 ,
width: 60,
),
),
)
),Text(String),
Use fit property of Ink.child
1st way : Use fit: BoxFit.cover, for center cropped image
Or else
2nd way : Use fit: BoxFit.fill, to stretch the image
Container(
child:Material(
shape: CircleBorder(),
clipBehavior: Clip.antiAliasWithSaveLayer,
child: InkWell(
splashColor: Colors.black26,
onTap: (){},
child: Ink.image(
image: AssetImage('assets/'+ Name),
fit: BoxFit.cover, //Add this line for center crop or use 2nd way
height: 60 ,
width: 60,
),
),
)
),Text(String),
I guess you can use
CircleAvatar
here is the demo code
CircleAvatar(
radius: 20.0,
child: ClipOval(
child: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS0sCAvrW1yFi0UYMgTZb113I0SwtW0dpby8Q&usqp=CAU')),
),
though I can't open your example image, I guess if you use Circle Avatar it will work
I use stack to place images, my back image is drawn on top of the other one, how can i fix the posterpath to be on top, also top image doesn't round the edges:
return Stack(
children: [
Positioned(
child: AspectRatio(
aspectRatio: 390 / 220,
child: ColorFiltered(
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.3),
BlendMode.dstATop,
),
child: backdropPath != null
? Image.network(ImageDownloader.imageUrl(backdropPath))
: Image.asset(AppImages.noImageBig),
),
),
),
Positioned(
child: Center(
child: Padding(
padding: const EdgeInsets.only(top: 110.0),
child: Container(
clipBehavior: Clip.antiAlias,
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(12)),
),
height: 212.0,
width: 174.0,
child: posterPath != null
? Image.network(ImageDownloader.imageUrl(posterPath))
: Image.asset(AppImages.noImageBig),
),
),
),
),
The ordering of children in a Stack is determined by their order in the source code - later items in the list are drawn on top. If you want to switch the z-order of the two children, swap them in the source code.
For your second question, you can achieve "rounded corner" with ClipRRect widget.
Is my network image still overflowing, despite the defined width, and why?
Here is a problematic part of the code:
Container(
height: 200,
width: MediaQuery.of(context).size.width, // This may be helpful, but not in my case...
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fitWidth,
image: NetworkImage("${module['thumbnail']}"),
),
),
),
How to fix overflow?
A RenderFlex overflowed by 28 pixels on the right.
Help, I am a newbie in Flutter. Thanks!
Ps. Updated code from top to problematic :
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SingleModule()),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
clipBehavior: Clip.antiAlias,
child: Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 200,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage("${module['thumbnail']}"),
),
),
),
Try putting padding: EdgeInsets.only(right: 28), to your Container that wrap around your Image Widget.
I am wondering if it is possible to resize an image that is inside of a BoxDecoration container and called by the AssetImage().
My code is:
Flexible(
child: Container(
height: size.height * .75,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(25.0),
image: DecorationImage(
image: AssetImage('assets/img/washing.png'),
fit: BoxFit.scaleDown,
)),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
and follows...
The image is too big. I would like it small and in the top right corner.
Thank you.
Its size depends on your container Size. If you reduce of your container size then The image size automatically reduced. But you must use BoxFit.cover as fit
Here is the code sample -
return Scaffold(
appBar: AppBar(
title: Text('Help'),
),
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * .5,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(25.0),
image: DecorationImage(
image: AssetImage(
'asset/images/download.jpg',
),
fit: BoxFit.cover,
),
),
),
),
);
If you want to developed it without Decorated image I can help you. Please share your sample UI, I will created for you
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.