简体   繁体   中英

How do I align my Neumorphic widgets in concentric circles Flutter?

Im using the flutter_neumorphics dependency https://pub.dev/packages/flutter_neumorphic to make my circular Neumorphic containers. However I'm facing an issue when Im trying to resize it. I want the outer circle to be smaller and more compact. Here is the code I've tried:

  Widget build(BuildContext context) {
    final percentage = 50.0;
    return Stack(
      children: <Widget>[
        Align(
          child: Neumorphic(
            boxShape: NeumorphicBoxShape.circle(),
            padding: EdgeInsets.all(80),
            style: NeumorphicStyle(
              depth: NeumorphicTheme.embossDepth(context),
            ),
          child: CustomPaint(
            painter: NeuProgressPainter(
              circleWidth: 20,
              completedPercentage: percentage,
              defaultCircleColor: Colors.transparent,
            ),
            child: Center(),
          ),
        ),
        ),
        Align(
          child: Neumorphic(
            boxShape: NeumorphicBoxShape.circle(),
            padding: EdgeInsets.all(80),
            style: NeumorphicStyle(
              color: Colors.white,
              depth: NeumorphicTheme.depth(context),
            ),

          ),
        ),

      ],
    );
  }

Where the NeuProgressPainter is defined as:

class NeuProgressPainter extends CustomPainter {
  //
  Color defaultCircleColor;
  Color percentageCompletedCircleColor;
  double completedPercentage;
  double circleWidth;

  NeuProgressPainter(
      {this.defaultCircleColor,
      this.percentageCompletedCircleColor,
      this.completedPercentage,
      this.circleWidth});

  getPaint(Color color) {
    return Paint()
      ..color = color
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke
      ..strokeWidth = circleWidth;
  }

  @override
  void paint(Canvas canvas, Size size) {
    Paint defaultCirclePaint = getPaint(defaultCircleColor);

    Offset center = Offset(size.width / 2, size.height / 2);
    double radius = min(size.width / 2, size.height / 2);
    Rect boundingSquare = Rect.fromCircle(center: center, radius: radius);

    paint(
      List<Color> colors,
    ) {
      final Gradient gradient = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomRight,
        colors: colors,
      );

      return Paint()
        ..strokeCap = StrokeCap.round
        ..style = PaintingStyle.stroke
        ..strokeWidth = circleWidth
        ..shader = gradient.createShader(boundingSquare);
    }

    canvas.drawCircle(center, radius, defaultCirclePaint);

    double arcAngle = 2 * pi * (completedPercentage / 100);
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      -pi / 2,
      arcAngle,
      false,
      paint(
        [
        kcolor,
          kDarkOrange,
          kOrange,
        ],
      ),
    );
  }

  @override
  bool shouldRepaint(CustomPainter painter) {
    return true;
  }
}

This is what my output looks like: 在此处输入图像描述

Im trying to achieve something similar to this inspiration: 在此处输入图像描述

How about this?

Padding(
  padding: const EdgeInsets.all(80.0),
  child: Align(
    child: Neumorphic(
      boxShape: NeumorphicBoxShape.circle(),
      padding: EdgeInsets.all(20),
      style: NeumorphicStyle(
        depth: NeumorphicTheme.embossDepth(context),
      ),
      child: CustomPaint(
        painter: NeuProgressPainter(
          circleWidth: 20,
          completedPercentage: percentage,
          defaultCircleColor: Colors.transparent,
        ),
        child: Center(),
      ),
    ),
  ),
),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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