Flutter: Add box shadow to a transparent Container

前端 未结 3 2075
南笙
南笙 2020-12-25 14:48

I\'m trying to make a widget that renders one of the circles shown in this image. It is a transparent circle with a box-shadow. The circle should show whichever color or bac

相关标签:
3条回答
  • 2020-12-25 15:12

    This is working for everyone

      Container(
        height: 210.0,
        decoration: BoxDecoration(
            boxShadow: [
              new BoxShadow(
                color: Colors.grey,
                blurRadius: 10.0,
              ),],
    
        ),
      child: ClipPath(
        clipper: ShapeBorderClipper(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(10))
            )
        ),
        child: Container(
          height: 200.0,
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                    left: BorderSide(
                        color: Theme.of(context).primaryColor,
                        width: 7.0
                    )
                )
            ),
          child: Text("kokoko"),
        ),
      ),
    );
    
    0 讨论(0)
  • 2020-12-25 15:14
    Container(
    height: 200.0,
    decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
            color: Colors.black,
            blurRadius: 10.0, // soften the shadow
            spreadRadius: 7.0, //extend the shadow
            offset: Offset(
              5.0, // Move to right 10  horizontally
              5.0, // Move to bottom 5 Vertically
            ),
          )
        ],
    ),
    child: Text("                                                                     
    0 讨论(0)
  • 2020-12-25 15:35

    As you can see in the BoxShadow class, they subclass the toPaint() method like this :

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

    ... with BlurStyle.normal instead of BlurStyle.outer as we wanted.

    Let's just create a custom BoxShadow that takes the BlurStyle as parameter.

    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;
      }
    }
    

    Now we can use it like this :

    new CustomBoxShadow(
      color: Colors.black,
      offset: new Offset(5.0, 5.0),
      blurRadius: 5.0,
      blurStyle: BlurStyle.outer
    )
    
    0 讨论(0)
提交回复
热议问题