Image gets shrinked when put inside Stack widget Flutter - flutter

My expected ui is to have the image occupy full width and have an text on top of it. So I an stack widget but it shrinks down and does not take complete width.
body: ListView(
children: [
Stack(
children: [
Container(
child: Image(
height:120.0,
image: AssetImage('images/business.jpg'),
),
),
Text("Business",style: TextStyle(fontSize: 30.0),)
],
)
])

First of all, I think there is no need for the ListView widget as far I know from looking at your code. Remove it and to achieve what you're looking for, add the below properties to the Image widget
fit: BoxFit.cover,
height: double.infinity,
width: double.infinity
so the final version should look like:
Image(
fit: BoxFit.cover,
height: double.infinity,
width: double.infinity
image: AssetImage('images/business.jpg'),
),

Related

Problem I have space in listview between items (Container) with flutter

Problem I have space in listview between items (Container) with flutter
Problem I have space in listview between items (Container) with flutter
this code :
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView(
children: [
Container(
width: width,
height: 220,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/exclusion.png'),
// fit: BoxFit.contain,
),
),
),
Container(
width: width,
height: 220,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/exclusion.png'),
// fit: BoxFit.contain,
),
),
),
],
),
),
],
),
);
The height of the container you provided does not reflect the image height, so the image gets centered try to set fit: BoxFit.fill in the DecorationImage so that you see the actual height you need

Flutter make container size fit stack size

I want to make something like in the picture below: A picture with two texts over it.
My first approach was creating a Container with a Stack inside and both Texts inside the Stack; and another Stack with the picture and Texts, but I encountered the problem of having to give a size to the Container that wraps the Stack with the texts. Althought it works that way, it is not a optimal solution for me due to responsiveness reasons.
Is there any way of wrapping a Stack with a Container and let the Container size from the Stack's content? Or is there a better way of achieving what I'm seeking? Thanks in advance. (Sorry for my English).
In your Stack:
Stack(
children: <Widget> [
Image(...), // Your Image here
Positioned.fill( // Expands to the size of your image
child: ..., // Put your Text widgets here in a Stack/Any other way you deem fit
),
],
),
You can use Shadow property to achieve this layout.
Following code will help you more.
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: double.infinity,
height: 300,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://miro.medium.com/max/1400/1*U-R58ahr5dtAvtSLGK2wXg.png'),
fit: BoxFit.cover),
),
child: Text(
" Hello world",
style: TextStyle(
fontSize: 50,
color: Colors.white,
shadows: [
Shadow(
color: Colors.amber,
offset: Offset(-30.0, -30.0),
blurRadius: 1.0,
),
],
),
textAlign: TextAlign.center,
),
),
),
);
}
You can also use the double.maxFinite property for the setting the height and width to match parent , like below
Stack(
children: <Widget>[
Container(
height: double.maxFinite,
width: double.maxFinite,
child:
Image.asset('YOOUR_ASSEST_IMAGE_HERE', fit: BoxFit.fill),
),
]
)
And check the another solution of mine using thee Stack, please check it once https://stackoverflow.com/a/58029364/3946958

How can I improve image display quality?

Original image
Screenshot
When I try to load an image in my application, it feels like it is somehow not stretching properly.
I tried all the options for the "fit" parameter
And nothing happened
It's my code
SingleChildScrollView(
child: Container(
width: width,
child: Image.network(
'https://img4.manga-chan.me/manga/-9new/t/1568958338_the-gamer-tom-4-glava-290/001.png',
filterQuality: FilterQuality.high,
fit: BoxFit.cover,
),
),
)
What should I do to make the image of original quality?
Did it and it helped me
Wrapped scroll in container
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
Container(
width: width,
height: height,
child: SingleChildScrollView(
child: Image.network(
'https://img4.manga-chan.me/manga/-9new/t/1571807955_the-gamer-tom-4-glava-296/002.png',
filterQuality: FilterQuality.high,
fit: BoxFit.fitWidth,
),
),
)

Flutter: Stack ignores Image's alignment property

I have a grid of images and I want an delete icon on the top-right position of each image.
I used this code to achieve it:
return Stack(
alignment: Alignment.topRight,
fit: StackFit.expand,
children: <Widget>[
Image(
image: image,
fit: BoxFit.cover,
),
Icon(Icons.delete, color: Colors.white,),
],
);
What I got:
The problem is, as you can see, the delete icon is on the center of the image not the up-right even though I set Stack's alignment to Alignment.topRight. I know this happens because I set Stack's fit to StackFit.expand but if I remove it then Image's fit property will be ignored and I'll get this:
So what should I do if I want to keep my Images square and be able to move the Icon to borders?
You should set the alignment only for the icon, so wrap the icon in an Align widget and set its alignment.
return Stack(
fit: StackFit.expand,
children: <Widget>[
Image(
image: image,
fit: BoxFit.cover,
),
Align(
alignment: Alignment.topRight,
child: Icon(
Icons.delete,
color: Colors.white,
),
),
],
);

Flutter - Image taking the whole screen width

In a Detail page, I'd like an image to take the whole width available, on the very top. This is my code:
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Container(
child: Column(
children: <Widget>[
Container(
width: double.infinity,
child: Image.asset(
this.imageLink,
),
)
],
),
),
),
);
The problème is : with the emulator, the result is exactly what I want :
But when I launch the app on a real phone (Galaxy S8 Plus) there are some paddings (top, left and right) and the image doesn't fit the screen width.
Which means that something is not correct in the way I tried to do it with the code.
Where could be the problem ?
I think the reason being original width of image is equal or more than the width of emulator and which is less than your Samsung S8+ width, you need to use
Image.asset(
this.linkImage,
width: double.infinity,
fit: BoxFit.cover,
),
You will have use expanded widget for that or you can use
height: double.infinity,
width: double.infinity,
or
new Image.asset('assets/images/umbrella.png',
width: size.width,
height: size.height,
fit: BoxFit.fill,
)
you can use
fit: BoxFit.cover,