Image Scale type center crop on flutter?
Android ScaleType and BoxFit should match like this:
- CENTER = none
- CENTER_CROP = cover
- CENTER_INSIDE = scaleDown
- FIT_CENTER = contain (alignment.center)
- FIT_END = contain (alignment.bottomright)
- FIT_START = contain (alignment.topleft)
- FIT_XY = fill
Ex :-
Image.asset('assets/images/your_image.png',fit: BoxFit.fill)
So you should use Cover to achieve the CENTER_CROP result.
EDIT:
The problem should be crossAxisAlignment
of the Column widget.
Setting this property to crossAxisAlignment: CrossAxisAlignment.stretch
should fix your issue.
How can you center crop a Widget in Flutter?
Finally solved it. There were a few missing pieces:
- I needed an
OverflowBox
with no restrictions so that my child could grow as large as needed. - I needed the child of
FittedBox
to actually enforce a size to stop the layout system from crashing. - Now that my widget will paint outside of its bounds, we also want to put a
ClipRect
in there to stop this from happening.
final Size size = controller.value.size;
return new ClipRect(
child: new OverflowBox(
maxWidth: double.infinity,
maxHeight: double.infinity,
alignment: Alignment.center,
child: new FittedBox(
fit: BoxFit.cover,
alignment: Alignment.center,
child: new Container(
width: size.width,
height: size.height,
child: new VideoPlayer(controller)
)
)
)
);
How do I automatically scale images in flutter to fit every resolution without overflow and scrolling?
Looking at your code you have at least two different problems.
- Setting the correct image fit - You can use
BoxFit.contain
inImage.asset(fit: boxFit.contain, .... )
to make sure it is resized to be contained inside it's parent. - You have a
Column
and want the first child to take all the available width. Hence you should nest it insideExpanded
widget.
ie. structurally something like:
Column(
children: [
Expanded(
// your image goes here which will take as much height as possible.
child: Image.asset('asset', fit: BoxFit.contain),
),
Container(
// your button bar which takes up the rest of the height
child: MaterialButton( ... ),
),
],
);
I left out quite a bit, but i hope you get the gist.
How do I crop an image in Flutter?
I would probably use a BoxDecoration
with a DecorationImage
. You can use the alignment
and fit
properties to determine how your image is cropped. You can use an AspectRatio
widget if you don't want to hard code a height on the Container
.
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new MyHomePage(),
));
}
class MyHomePage extends StatelessWidget {
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Image Crop Example"),
),
body: new Center(
child: new AspectRatio(
aspectRatio: 487 / 451,
child: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
fit: BoxFit.fitWidth,
alignment: FractionalOffset.topCenter,
image: new NetworkImage('https://i.stack.imgur.com/lkd0a.png'),
)
),
),
),
),
);
}
}
Related Topics
How to Use Threetenabp in Android Project
How to Save an Activity State Using the Save Instance State
Is There a Unique Android Device Id
How to Close/Hide the Android Soft Keyboard Programmatically
How to Turn Off Suggestions in Edittext
How to Load New Values of Json After Every 10 Seconds
Android Shared Preferences For Creating One Time Activity (Example)
How to Handle Notification When App in Background in Firebase
Send Data from Activity to Fragment in Android
Onactivityresult Is Not Being Called in Fragment
Change App Language Programmatically in Android
Activity Restart on Rotation Android
You Need to Use a Theme.Appcompat Theme (Or Descendant) With This Activity
How to Display an Image in Full Screen on Click in the Imageview
Httpmessagenotreadableexception: Could Not Read Json: Unrecognized Field Using Spring for Android
Android Sdk Is Mssing ,Out of Date or Corrupted
How to Install the App Twice Without Interference in Android