如何在Flutter中裁剪图像?(How do I crop an image in Flutter?)
假设我有一个长方形的肖像图像:
我想裁剪它,使其呈现如下:
我如何在Flutter中做到这一点?
(我不需要调整图像大小。)
(图片来自https://flic.kr/p/nwXTDb )
Let's say I have a rectangular, portrait image:
I'd like to crop it, such that it's rendered like this:
How can I do this in Flutter?
(I don't need to resize the image.)
(Image from https://flic.kr/p/nwXTDb)
最满意答案
我可能会使用带有DecorationImage的BoxDecoration 。 您可以使用alignment和fit属性来确定图像如何裁剪。 如果您不想在Container上硬编码高度,则可以使用AspectRatio小部件。
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'), ) ), ), ), ), ); } }
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'), ) ), ), ), ), ); } }更多推荐
发布评论