简体   繁体   English

如何为 flutter 中的图标添加阴影?

[英]How can I add shadow to an icon in flutter?

I need to add shadows to some icons in my flutter project.我需要为我的 flutter 项目中的一些图标添加阴影。 I've checked the icon class constructors but nothing points to that.我已经检查了图标 class 构造函数,但没有任何迹象表明这一点。 Any idea on how to implement that?关于如何实施的任何想法?

I got what i wanted eventually using this workaround.我最终使用此解决方法得到了我想要的东西。 I hope it helps whoever might need something similar.我希望它可以帮助任何可能需要类似东西的人。

                Stack(
                  children: <Widget>[
                    Positioned(
                      left: 1.0,
                      top: 2.0,
                      child: Icon(icon, color: Colors.black54),
                    ),
                    Icon(icon, color: Colors.white),
                  ],
                ),
Container( decoration: BoxDecoration( shape: BoxShape.circle, boxShadow: [ BoxShadow( color: Colors.grey[400], blurRadius: 5.0, ), ] ), child: Icon( Icons.fiber_manual_record, color: Colors.amber, size:15, ) ),

You can use IconShadowWidget() .您可以使用IconShadowWidget() 在此处输入图片说明

How to use:如何使用:

1. add dependencies to pubspec.yaml : 1. 向pubspec.yaml添加依赖pubspec.yaml

 icon_shadow: ^1.0.1

2. Import your Dart code : 2. 导入你的 Dart 代码:

import 'package:icon_shadow/icon_shadow.dart';

3. add icons: 3.添加图标:

Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
        IconShadowWidget(
          Icon(
            Icons.add_circle,
            color: Colors.red,
            size: 100.0,
          ),
        ),
        IconShadowWidget(
          Icon(
            Icons.add_circle,
            color: Colors.red,
            size: 100.0,
          ),
          shadowColor: Colors.black,
        ),
        IconShadowWidget(
          Icon(
            Icons.add_circle,
            color: Colors.red,
            size: 100.0,
          ),
          shadowColor: Colors.black,
          showShadow: false,
        ),
              ],
            ),
          ),

You can also check my GitHub Repository您还可以查看我的GitHub 存储库

  InkWell(
      child: Container(
                padding: const EdgeInsets.all(4.0),
                decoration: BoxDecoration(
                             color: Colors.white,
                             shape: BoxShape.circle,
                             boxShadow: [
                                BoxShadow(
                                  color: Colors.grey,
                                  blurRadius: .5,
                                ),
                              ]),
                child: Icon(
                         Icons.clear,
                         color: Colors.black,
                         size: 25,
                          )),
                    ),

result will be like this pic:结果将是这样的图片:

带阴影的圆形图标按钮 :

Whenever you need elevation/shadow, remember the Card widget.每当您需要高程/阴影时,请记住Card小部件。 So, you can wrap it with Card and SizedBox :所以,你可以用CardSizedBox包装它:

在此处输入图片说明

          Card(
          elevation: 10,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(35.0),
          ),
          child: SizedBox(
            width: 35,
            height: 35,
            child: Icon(
              Icons.close,
              color: Colors.black,
              size: 19,
            ),
          ),
        )

Even better, here is an icon button with material bubble effect + shadow (in below GIF, shadow's quality looks like bad, it is because of GIF itself)更好的是,这里有一个带有材质气泡效果+阴影的图标按钮(在下面的GIF中,阴影的质量看起来很糟糕,这是因为GIF本身)

带阴影的材质图标按钮 :

          Card(
          elevation: 10,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(35.0),
          ),
          child: ClipOval(
            child: Material(
              color: Colors.transparent, // button color
              child: InkWell(
                splashColor: Colors.red, // inkwell color
                child: SizedBox(
                  width: 35,
                  height: 35,
                  child: Icon(
                    Icons.close,
                    color: Colors.black,
                    size: 19,
                  ),
                ),
                onTap: () {},
              ),
            ),
          ),
        )

Took the idea from @Dzeri answer ( https://stackoverflow.com/a/55668093/414635 ) and encapsulated it into a Widget so it became reusable.从@Dzeri 的答案( https://stackoverflow.com/a/55668093/414635 )中汲取了这个想法,并将其封装到一个 Widget 中,使其变得可重用。

Widget小工具

class ShadowIcon extends StatelessWidget {
  final IconData icon;
  final Color color;

  ShadowIcon(this.icon, {Key key, this.color: kLight}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          left: 0.5,
          top: 0.5,
          child: BackdropFilter(
            filter: ImageFilter.blur(
              sigmaX: 1.0,
              sigmaY: 1.0,
            ),
            child: FaIcon(this.icon, color: kDark.withOpacity(0.7)),
          ),
        ),
        FaIcon(this.icon, color: color),
      ],
    );
  }
}

The BackdropFilter doesn't seem to be working as expected but anyway all I needed was a subtle drop shadow. BackdropFilter似乎没有按预期工作,但无论如何我需要的是一个微妙的阴影。 I'm also using the package font_awesome_flutter but you can replace the FaIcon by the native Icon widget.我也在使用font_awesome_flutter包,但您可以用本机Icon小部件替换FaIcon

Usage用法

Your can simply replace the native Icon by the ShadowIcon widget call:您可以通过ShadowIcon小部件调用简单地替换本机Icon

IconButton(
  icon: ShadowIcon(FontAwesomeIcons.chevronLeft, color: kLight),
  onPressed: () => Get.back(),
),

Try this, use the icon font.试试这个,使用图标字体。

GestureDetector(
                  child: Container(
                    padding: EdgeInsets.only(right: 10, top: 10),
                    child: Text('\u{e5d3}',
                        style: TextStyle(
                            fontSize: 22,
                            color: Colors.white,
                            fontFamily: 'MaterialIcons',
                            shadows: [
                              BoxShadow(color: Colors.black, blurRadius: 2)
                            ])),
                  ),
                  onTap: () {}
)

Icon data from icons.dart
/// <i class="material-icons md-36">more_horiz</i> &#x2014; material icon named "more horiz".
static const IconData more_horiz = IconData(0xe5d3, fontFamily: 'MaterialIcons');

You can use decorated icon plugin to do shadow on icon您可以使用装饰图标插件在图标上做阴影

在此处输入图片说明

Code here :代码在这里:

Scaffold(
  body: Center(
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        DecoratedIcon(
          Icons.android,
          color: Colors.purple,
          size: 60.0,
          shadows: [
            BoxShadow(
              blurRadius: 42.0,
              color: Colors.purpleAccent,
            ),
            BoxShadow(
              blurRadius: 12.0,
              color: Colors.white,
            ),
          ],
        ),
        DecoratedIcon(
          Icons.favorite,
          color: Colors.lightBlue.shade50,
          size: 60.0,
          shadows: [
            BoxShadow(
              blurRadius: 12.0,
              color: Colors.blue,
            ),
            BoxShadow(
              blurRadius: 12.0,
              color: Colors.green,
              offset: Offset(0, 6.0),
            ),
          ],
        ),
        DecoratedIcon(
          Icons.fingerprint,
          color: Colors.orange,
          size: 60.0,
          shadows: [
            BoxShadow(
              color: Colors.black,
              offset: Offset(3.0, 3.0),
            ),
          ],
        ),
      ],
    ),
  ),
);

I know this is pretty late, but for anyone looking to add a shadow in circular form should wrap the icon with a CircleAvatar widget and set the backgroundColor proprety of CircleAvatar to Colors.grey.withOpacity (0.5) or to any other color for the shadow.我知道这已经很晚了,但是对于任何想要以圆形形式添加阴影的人来说,应该用 CircleAvatar 小部件包裹图标,并将 CircleAvatar 的 backgroundColor 属性设置为 Colors.grey.withOpacity (0.5) 或任何其他阴影颜色. Here's the code snippet这是代码片段

CircleAvatar (
bacgroundColor: Colors.grey.withOpacity (0.5),
child: Icon (
Icons.yourIcon
)

Right now, it's not possible to directly add shadows to an Icon widget.目前,无法直接向 Icon 小部件添加阴影。 You can however use the additional information from your IconData icon to display the icon as a styled text.但是,您可以使用 IconData 图标中的附加信息将图标显示为样式文本。

Text(
  String.fromCharCode(Icons.add.codePoint), 
  style: TextStyle(
    fontFamily: Icons.add.fontFamily,
    color: Colors.white, 
    fontSize: 20.0,
    shadows: [
      BoxShadow(
        color: ColorTheme.blackLight,
        spreadRadius: 2,
        blurRadius: 2,
      )
    ],
    height: 1 //if this isn't set, the shadow will be cut off on the top and bottom
  )
);
Container(
    width: 30,
    height: 30,
    decoration: BoxDecoration(
    color: Colors.white,
    borderRadius: BorderRadius.circular(100),
    boxShadow: const [
    BoxShadow(color: Colors.grey, blurRadius: 1),
    ]),
    child: const Icon(
    FontAwesomeIcons.checkCircle,
    size: 30,
    color: Colors.green,
    ),
    ),

if the container size is the same as the icon size, then 3 pixels will be always downward, This is because I don't know.如果容器大小与图标大小相同,那么3个像素将始终向下,这是因为我不知道。 but this solution will clear it.但这个解决方案会清除它。 make sure that the container size will increase by 3 pixels with icons size.确保容器大小将随着图标大小增加 3 个像素。

Container(
width: 33,
height: 33,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(100),
boxShadow: const [
BoxShadow(color: Colors.grey, blurRadius: 1),
]),
child: const Icon(
FontAwesomeIcons.checkCircle,
size: 30,
color: Colors.green,
),
),
Material(
  color: Colors.transparent,
  elevation: 10,
  child: Icon(
    icons.add,

  ),
),

The Icon widget has a shadows property with which you can give shadows to an icon. Icon 小部件具有 shadows 属性,您可以使用该属性为图标提供阴影。

 const Icon( icon, shadows: <Shadow>[Shadow(color: Colors.black, blurRadius: 15.0)], size: 60, color: Colors.white, )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM