Image Scale Type Center Crop on Flutter

Image Scale type center crop on flutter?

Android ScaleType and BoxFit should match like this:

  1. CENTER = none
  2. CENTER_CROP = cover
  3. CENTER_INSIDE = scaleDown
  4. FIT_CENTER = contain (alignment.center)
  5. FIT_END = contain (alignment.bottomright)
  6. FIT_START = contain (alignment.topleft)
  7. 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:

  1. I needed an OverflowBox with no restrictions so that my child could grow as large as needed.
  2. I needed the child of FittedBox to actually enforce a size to stop the layout system from crashing.
  3. 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.

  1. Setting the correct image fit - You can use BoxFit.contain in Image.asset(fit: boxFit.contain, .... ) to make sure it is resized to be contained inside it's parent.
  2. You have a Column and want the first child to take all the available width. Hence you should nest it inside Expanded 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.

screenshot

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



Leave a reply



Submit