在Flutter中创建带有图像的按钮? [英] Create a button with an image in Flutter?

查看:884
本文介绍了在Flutter中创建带有图像的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Flutter创建带有图像的按钮?这似乎是最简单的操作,但是图像并未完全填充父窗口小部件.这就是我所拥有的:

How do you create a button with an image using Flutter? It seems like the simplest thing to do, but the image does not fill the parent widget completely. This is what I have:

Container(child: ConstrainedBox(
    constraints: BoxConstraints.expand(),
    child: FlatButton(onPressed: null,
        child: Image.asset('path/the_image.png'))))

我按照这篇文章进行了指导.我的图片看起来像这样:

I followed this post as guidance. My image looks like this:

请注意PNG图像周围的填充-它不在代码中.它从何而来? PNG本身没有画布填充,因此这不一定是正确的技术.

Notice the padding around the PNG image - it's not in the code. Where does it come from? The PNG itself does not have canvas padding, so this must not be the correct technique.

我需要的是一个带有填充整个FlatButton的图像的按钮,或者是一个我可以向其中添加操作而不会扭曲图像的小部件.

All I need is a button with an image that fills the entire FlatButton, or another widget I can add actions to, without distorting the image.

推荐答案

FlatButton中包含图像可能不符合您的要求,因为它可以自行处理某些样式(如填充).

Having an image inside a FlatButton might not fit your requirements, as it takes care of some styling ( like that padding ) on its own.

要完全控制按钮方面,您可能需要构建自定义小部件(甚至是具有自定义BoxDecoration的简单Container来显示图像),并用手势识别器将其包装起来以处理用户交互(轻按一下即可).最简单的实现方法是使用GestureDetector,但是还有其他小部件,例如InkWell,可在水龙头上的可轻敲小部件表面上呈现出材料设计的波纹.

To have full control on your button aspect you might want to build a custom widget ( even a simple Container with a custom BoxDecoration to display the image ) and wrap it with a gesture recognizer to handle user interactions ( a simple tap, in your case ). The simplest implementation would use a GestureDetector, but there are also other widgets, like InkWell that renders a material design ripple over the tappable widget surface on tap.

这篇关于在Flutter中创建带有图像的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆