neumorphism method

Widget neumorphism({
  1. Key? key,
  2. required double elevation,
  3. BorderRadius borderRadius = BorderRadius.zero,
  4. Color backgroundColor = const Color(0xffEDF1F5),
  5. double curve = 0.0,
  6. bool animate = false,
})

Implementation

Widget neumorphism({
  Key? key,
  required double elevation,
  BorderRadius borderRadius = BorderRadius.zero,
  Color backgroundColor = const Color(0xffEDF1F5),
  double curve = 0.0,
  bool animate = false,
}) {
  double offset = elevation / 2;
  int colorOffset = (40 * curve).toInt();
  final int Function(int, int) adjustColor = (int color, int colorOffset) {
    int colorVal = color + colorOffset;
    if (colorVal > 255)
      return 255;
    else if (colorVal < 0) return 0;
    return colorVal;
  };

  BoxDecoration decoration = BoxDecoration(
    gradient: LinearGradient(
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
      colors: [
        Color.fromRGBO(
          adjustColor(backgroundColor.red, colorOffset),
          adjustColor(backgroundColor.green, colorOffset),
          adjustColor(backgroundColor.blue, colorOffset),
          1.0,
        ),
        Color.fromRGBO(
          adjustColor(backgroundColor.red, -colorOffset),
          adjustColor(backgroundColor.green, -colorOffset),
          adjustColor(backgroundColor.blue, -colorOffset),
          1.0,
        ),
      ],
      // stops: [0.90, 0.95],
    ),
    borderRadius: borderRadius,
    boxShadow: [
      BoxShadow(
        color: Colors.white,
        blurRadius: elevation.abs(),
        offset: Offset(-offset, -offset),
      ),
      BoxShadow(
        color: Color(0xAAA3B1C6),
        blurRadius: elevation.abs(),
        offset: Offset(offset, offset),
      ),
    ],
  );

  return animate
      ? _StyledAnimatedBuilder(
          key: key,
          builder: (animation) {
            return _AnimatedDecorationBox(
              child: this,
              decoration: decoration,
              duration: animation.duration,
              curve: animation.curve,
            );
          },
        )
      : DecoratedBox(
          key: key,
          child: this,
          decoration: decoration,
        );
}