如何在 Flutter 中裁剪图像? [英] How do I crop an image in Flutter?
本文介绍了如何在 Flutter 中裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个矩形的纵向图像:
我想裁剪它,使其呈现如下:
我如何在 Flutter 中做到这一点?
(我不需要调整图像大小.)
(图片来自
import 'package:flutter/material.dart';无效主(){运行应用程序(新材料应用程序(主页:新的我的主页(),));}class MyHomePage 扩展了 StatelessWidget {小部件构建(BuildContext 上下文){返回新的脚手架(应用栏:新的应用栏(标题:新文本(图像裁剪示例"),),身体:新中心(孩子:新的纵横比(纵横比:487/451,孩子:新容器(装饰:新的 BoxDecoration(图像:新的装饰图像(适合:BoxFit.fitWidth,对齐:FractionalOffset.topCenter,图片:new NetworkImage('https://i.stack.imgur.com/lkd0a.png'),)),),),),);}}
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)
解决方案
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'),
)
),
),
),
),
);
}
}
这篇关于如何在 Flutter 中裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文