Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在Flight中使用CustomPainter绘制戒指?_Flutter_Dart_Paint - Fatal编程技术网

Flutter 如何在Flight中使用CustomPainter绘制戒指?

Flutter 如何在Flight中使用CustomPainter绘制戒指?,flutter,dart,paint,Flutter,Dart,Paint,我正在尝试使用CustomPainter类绘制半环,您可以在下图中看到(浅紫色和浅橙色),但无法绘制。 我只能画圆、正方形和直线 代码: 这只能画一个圆圈 必须为绘制对象使用绘制样式.stroke,还必须使用路径类绘制弧 我为您创建了一个示例,请测试它: import 'dart:math' as math; import 'package:flutter/material.dart'; main() { runApp(MaterialApp( home: Home(), )

我正在尝试使用CustomPainter类绘制半环,您可以在下图中看到(浅紫色和浅橙色),但无法绘制。 我只能画圆、正方形和直线

代码:

这只能画一个圆圈


必须为绘制对象使用绘制样式.stroke,还必须使用路径类绘制

我为您创建了一个示例,请测试它:

import 'dart:math' as math;

import 'package:flutter/material.dart';

main() {
  runApp(MaterialApp(
    home: Home(),
  ));
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 300,
          height: 300,
          child: CustomPaint(
            painter: MakeCircle(strokeWidth: 50,strokeCap: StrokeCap.round),
          ),
        ),
      ),
    );
  }
}




class MakeCircle extends CustomPainter {
  final double strokeWidth;
  final StrokeCap strokeCap;

  MakeCircle({this.strokeCap = StrokeCap.square, this.strokeWidth = 10.0});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.blue
      ..strokeCap = StrokeCap.round
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke; //important set stroke style

    final path = Path()
      ..moveTo(strokeWidth, strokeWidth)
      ..arcToPoint(Offset(size.width - strokeWidth, size.height - strokeWidth),
          radius: Radius.circular(math.max(size.width, size.height)));

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}




检查Canvas.drawArc()method我也试过,但不知道怎么做。你能给我提供准确的代码吗?欢迎你加入我们的Flitter群,加入linkedin=>
import 'dart:math' as math;

import 'package:flutter/material.dart';

main() {
  runApp(MaterialApp(
    home: Home(),
  ));
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 300,
          height: 300,
          child: CustomPaint(
            painter: MakeCircle(strokeWidth: 50,strokeCap: StrokeCap.round),
          ),
        ),
      ),
    );
  }
}




class MakeCircle extends CustomPainter {
  final double strokeWidth;
  final StrokeCap strokeCap;

  MakeCircle({this.strokeCap = StrokeCap.square, this.strokeWidth = 10.0});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.blue
      ..strokeCap = StrokeCap.round
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke; //important set stroke style

    final path = Path()
      ..moveTo(strokeWidth, strokeWidth)
      ..arcToPoint(Offset(size.width - strokeWidth, size.height - strokeWidth),
          radius: Radius.circular(math.max(size.width, size.height)));

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}