【问题标题】:How do I align my Neumorphic widgets in concentric circles Flutter?如何在同心圆 Flutter 中对齐我的 Neumorphic 小部件?
【发布时间】:2020-04-18 13:24:04
【问题描述】:

我正在使用flutter_neumorphics依赖https://pub.dev/packages/flutter_neumorphic 制作我的圆形 Neumorphic 容器。但是,当我尝试调整它的大小时,我遇到了一个问题。 我希望外圈更小更紧凑。 这是我尝试过的代码:

  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),
            ),

          ),
        ),

      ],
    );
  }

NeuProgressPainter 定义为:

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

这是我的输出的样子:

我正在尝试实现与此灵感类似的东西:

【问题讨论】:

    标签: user-interface flutter dart


    【解决方案1】:

    这个怎么样?

    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(),
          ),
        ),
      ),
    ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 2021-12-09
      • 2019-03-10
      相关资源
      最近更新 更多