How to stretch image to the screen borders by width? - flutter

I need to show image with a width of a screen, but when the image is smaller it does not strech to the screen edges.
CachedNetworkImage _getImage(String link, BuildContext context) {
return CachedNetworkImage(
width: MediaQuery.of(context).size.width,
imageUrl: link,
placeholder: (context, url) => Center(child: CircularProgressIndicator()),
errorWidget: (context, url, error) => Icon(Icons.error),
);
}
and width: MediaQuery.of(context).size.width, - does not stretch the image but just put it in the center of screen width
body: SingleChildScrollView(
child: Column(
// outer column
children: [
_getImage(imageLink, context),
...

try these two modifications:
CachedNetworkImage(
fit: BoxFit.fitWidth // or any other fitting option
),
and
Column(
crossAxisAlignment : CrossAxisAlignment.stretch,
// outer column
children: [
_getImage(imageLink, context),
...

Related

Hero Animation for Network Image in Flutter

I am trying to add Hero animation for a Network Image.
The problem is, when I navigate from one screen to another the image loads again on second screen and so I am not able to see the animation.
After loading complete, if I repeat this navigation (from 1st screen to 2nd) this is working fine.
So the question is, How can I achieve this animation when I navigate for first time?
Error-Output:
Click Here
Code:
FirstScreen.dart
Hero(
tag: 'tag',
child: Image.network(
'https://...',
cacheHeight: 1080,
cacheWidth: 1080,
fit: BoxFit.none,
scale: 5,
),
),
secondScreen.dart
Hero(
tag: 'tag',
child: Image.network(
'https://...',
),
)
With CachedNetworkImage:
firstScreen.dart
Hero(
tag: tag
child: CachedNetworkImage(
imageUrl: 'url',
fit: BoxFit.scaleDown,
height: 200,
width: 200,
memCacheHeight: 1080,
memCacheWidth: 1080,
errorWidget: (context, url, error) => Image.asset('assets/..'),
),
)
secondScreen.dart
Hero(
tag: tag,
child: CachedNetworkImage(
imageUrl: 'url',
errorWidget: (context, url, error) => Image.asset('assets/...'),
),
),
create loading builder in the network image widget and use a placeholder instead of image when the image is not fully loaded something like this
Image.network(
imagePath,
// width: 100,
// height: 100,
loadingBuilder: (BuildContext context,
Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Container(
width: 100,
height: 100,
child: Center(
child: // PlaceHolderWidget()
),
);
},
// fit: BoxFit.none,
)

Change opacity of image in CachedNetworkImage Flutter

Hello I have multiple images.
I want change the opacity of the image with the imageUrl.
My goal is that when user click on a image, it change his opacity.
My question is how to change the opacity while keeping the image ?
My CachedNetworkImage :
CachedNetworkImage(
imageUrl : _getImageUrl(),
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
fit: BoxFit.cover,
placeholder: (context, url) => const Center(child: CircularProgressIndicator()),
errorWidget: (context, exception, stacktrace)
{
return const Icon(Icons.warning);
},
)
You can wrap CachedNetworkImage with Opacity widget and provide opacity:x based on your need.
double _opacity =.3;
......
Opacity(
opacity: _opacity.
child:CachedNetworkImage(...)
More about Opacity.

make photo bigger when click on flutter

how to make the picture zoomable when the photo is clicked, here's the code I made to display the image
Column(
children: [
image11 != null
? Image.file(
image11,
width: 50,
height: 50,
fit: BoxFit.cover,
)
: Text(" Bagian Belakang"),
Container(
margin: const EdgeInsets.fromLTRB(14, 5, 0, 0),
child: RaisedButton(
padding: EdgeInsets.all(10),
onPressed: () {
pickImage11(ImageSource.camera);
},
child: Icon(Icons.camera),
),
),
],
),
Taking inspiration from this answer Hero animation with an AlertDialog
You can use a Hero widget combined with a PageRouteBuilder to imitate a zoom feature:
Navigator.of(context).push(
PageRouteBuilder(
opaque: false,
barrierDismissible:true,
pageBuilder: (BuildContext context, _, __) {
return Hero(
tag: "zoom",
child: Image.file(...),
);
}
)
);
EDIT: I found barrier dismissable not working and found the solution here: Flutter SizeTransition and PageRouteBuilder not working
You will have to wrap the Hero widget with a defined width and height to get the optimum zoom level.

Flutter: avoid opacity effect for FlexibleSpaceBar

I use a FlexibleSpaceBar but only for an image. The AppBar is always on top so there is no transition between these 2 elements at all, just the effect of scrolling the image up using parallax.
So I don't need to make the opacity effect for the image.
I know it's possible because I've seen it on Aliexpress's app, which I know it uses Flutter. You can see the no opacity effect here:
https://youtu.be/ESSsY2m7vTY?t=28
when mine is looking like this:
https://youtu.be/Jnm9jN4-wWY
This is my widget:
FlexibleSpaceBar(
collapseMode: CollapseMode.parallax,
background: Hero(
tag: widget.newRent == false
? 'rent-image${Provider.of<MyRents>(context).currenRentIndex}'
: 'new-rent-image',
child: _imageEdited == false
? myRent['images'].isNotEmpty
? CachedNetworkImage(
imageUrl: myRent['images'][0],
fit: BoxFit.cover,
placeholder: (context, url) =>
Center(child: CircularProgressIndicator()),
errorWidget: (context, url, error) =>
errorImage(context),
)
: Provider.of<MyRents>(context, listen: false)
.newGalleryImages
.isNotEmpty
? Image.file(
Provider.of<MyRents>(context, listen: false)
.newGalleryImages[0],
fit: BoxFit.cover)
: Container(
padding: const EdgeInsets.all(20.0),
child: Image(
image:
AssetImage('assets/images/icon.jpg'),
),
)
: Image.file(myRent['images'][0], fit: BoxFit.cover),
),
),
After removing all the checks it's a basic FlexibleSpaceBar with a Hero transition, nothing fancy
I think the problem is because it is indeed a "Bar" like the name of the widget says: FlexibleSpaceBar. You would have to propably extend this function and overwrite this behaviour or maybe you could use this:
https://pub.dev/packages/stretchy_header
Followed this approach:
https://stackoverflow.com/a/58973381/4858133
Stack(
children: <Widget>[
Positioned.fill(
child: Hero(
tag: widget.newRent == false
? 'rent-image${Provider.of<MyRents>(context).currenRentIndex}'
: 'new-rent-image',
child: _imageEdited == false
// TODO: move it to a separate function
? myRent['images'].isNotEmpty
? CachedNetworkImage(
imageUrl: myRent['images'][0],
fit: BoxFit.cover,
placeholder: (context, url) => Center(
child: CircularProgressIndicator()),
errorWidget: (context, url, error) =>
errorImage(context),
)
: Provider.of<MyRents>(context, listen: false)
.newGalleryImages
.isNotEmpty
? Image.file(
Provider.of<MyRents>(context,
listen: false)
.newGalleryImages[0],
fit: BoxFit.cover)
: Container(
padding: const EdgeInsets.all(20.0),
child: Image(
image: AssetImage(
'assets/images/icon.jpg'),
),
)
: Image.file(myRent['images'][0],
fit: BoxFit.cover),
),
),
],
),

How can I make a Card height extend based on content?

I am creating a Card which has a header and a grid view of photos. Below is the code:
_getImageWidget(Post post, AppConfig config) {
if (post.photoPaths != null && post.photoPaths.length > 0) {
var url = config.imagePathDomain + post.photoPaths[0];
try {
return Expanded(
child: GridView.count(
crossAxisCount: 3,
shrinkWrap: false,
children: post.photoPaths.map<Widget>((String path) {
return CachedNetworkImage(
imageUrl: url,
);
}).toList()));
} catch (err) {
return Container();
}
}
return Container();
}
#override
Widget build(BuildContext context) {
var config = AppConfig.of(context);
return BlocBuilder<UserInfoBloc, UserInfo>(builder: (_, userInfo) {
return Container(
width: MediaQuery.of(context).size.width,
child: Card(
child: Column(children: <Widget>[
Row(
children: <Widget>[
IconButton(
iconSize: 30,
icon: roundImage(post.userPicture, Icon(Icons.person)),
onPressed: () {},
),
Text('#${userInfo.username}')
],
),
this._getImageWidget(post, config),
])));
});
}
The header in the Card is a Row includes a IconButton and Text.
The body of the Card is a gridview which includes a few photo.
Below is the screenshot when I run the code. You can see that the photo is shown only a half. And I can scroll vertically on the grid view. The number of photos is dynamic which means there could be many rows of photos in the GridView. How can I make the Card extend based on its children?
By simply setting your CachedNetwork image to use a fit: BoxFit.cover that will resize to fill the content while preserving ratio (this means you may lose some of its details) or fit: BoxFit.contain that will try to be as big as possible while containing the entire source (image) within the bounds.
If that doesn't help as well (as I'm not seeing the entire tree so I'm not sure about the ancestors of your Card) you can also replace the return of your BlocBuilder's child to be a FittedBox instead of a Container and apply the same logic for the fit property, but for the whole card instead.
Try using a fixed size container and using the BoxFit property on the container.
Something like this :
new Container(
width: 80.0,
height: 80.0,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
image: new DecorationImage(
fit: BoxFit.fill,
image: CachedNetworkImageProvider(url),
),
),
),
Edit : Try to remove itemExtent from ListView.builder