颤动:将盒子阴影添加到透明容器中

问题描述 投票:4回答:2

我正在尝试创建一个小部件,呈现此image中显示的一个圆圈。它是一个带有阴影的透明圆圈。圆圈应显示应用于父容器的颜色或背景图像。圆圈是透明的,但我看到的是this:一个黑色的盒子阴影,而不是父母的背景颜色。有什么建议?

这是我到目前为止:

class TransParentCircle extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
        child: new Center(
          child: new Container(
            decoration: new BoxDecoration(
              border: new Border.all(width: 1.0, color: Colors.black),
              shape: BoxShape.circle,
              color: Colors.transparent,
              boxShadow: <BoxShadow>[
                BoxShadow(
                  color: Colors.black,
                  offset: Offset(1.0, 6.0),
                  blurRadius: 40.0,
                ),
              ],
            ),
            padding: EdgeInsets.all(16.0),
          ),
        ),
        width: 320.0,
        height: 240.0,
        margin: EdgeInsets.only(bottom: 16.0));
  }
}
dart flutter
2个回答
8
投票

正如你在BoxShadow类中看到的那样,他们将toPaint()方法子类化如下:

Paint toPaint() {
  final Paint result = Paint()
    ..color = color
    ..maskFilter = MaskFilter.blur(BlurStyle.normal, blurSigma);
  assert(() {
    if (debugDisableShadows)
      result.maskFilter = null;
    return true;
  }());
  return result;
}

...按照我们的要求,用BlurStyle.normal而不是BlurStyle.outer

让我们创建一个以BoxShadow为参数的自定义BlurStyle

import 'package:flutter/material.dart';

class CustomBoxShadow extends BoxShadow {
  final BlurStyle blurStyle;

  const CustomBoxShadow({
    Color color = const Color(0xFF000000),
    Offset offset = Offset.zero,
    double blurRadius = 0.0,
    this.blurStyle = BlurStyle.normal,
  }) : super(color: color, offset: offset, blurRadius: blurRadius);

  @override
  Paint toPaint() {
    final Paint result = Paint()
      ..color = color
      ..maskFilter = MaskFilter.blur(this.blurStyle, blurSigma);
    assert(() {
      if (debugDisableShadows)
        result.maskFilter = null;
      return true;
    }());
    return result;
  }
}

现在我们可以像这样使用它:

new CustomBoxShadow(
  color: Colors.black,
  offset: new Offset(5.0, 5.0),
  blurRadius: 5.0,
  blurStyle: BlurStyle.outer
)

0
投票

我可以试着向你清楚这一点。当我们说阴影它并不意味着边界阴影而不是它意味着阴影一般对于你的完整容器是圆形的,圆形的阴影也是圆形的,即你看到的深黑色圆圈是容器的阴影因为您已将容器着色为透明,如果您将容器着色为红色,则不会看到圆圈内的阴影,而是看到用ime红色绘制容器的颜色。我建议你像你一样做一个阴影并用颜色涂上容器

Theme.of(context).primaryColor

这样你就可以得到你想要的那种效果。并使用相同的颜色绘制父容器。

© www.soinside.com 2019 - 2024. All rights reserved.