gpt4 book ai didi

dart - Flutter中的弧形渐变?

转载 作者:IT老高 更新时间:2023-10-28 12:43:37 26 4
gpt4 key购买 nike

我有一个 Paint 对象,我正在尝试使用它来使用 canvas.drawArc 绘制弧渐变,但这样做的唯一方法(至少根据我的研究)是使用 Shader,但要从 Gradient 对象获取 Shader,您必须使用 Gradient .createShader(Rect rect),它采用一个矩形。我的问题是,有没有办法为 Arc 而不是 Rectangle 创建着色器?以下是我目前的引用资料:

Paint paint = new Paint()
..color = bgColor
..strokeCap = StrokeCap.round
..strokeWidth = 3.0
..style = PaintingStyle.stroke
..shader = new Gradient.radial(size.width / 2.0, size.height / 2.0, size.height / 3.0, Colors.transparent, timerColor, TileMode.mirror).createShader(/* I don't have a rect object */);


canvas.drawArc(..., paint);

最佳答案

您需要的 Rectangle 实际上是一个正方形,您正在绘制的圆适合该正方形。弧线只是那圈扫过这么多弧度的馅饼。使用 Rect.fromCircle 创建这个正方形,使用中心和半径。然后在创建渐变和绘制弧线时使用这个正方形。

这是一个例子

import 'dart:math';

import 'package:flutter/material.dart';

class X1Painter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
// create a bounding square, based on the centre and radius of the arc
Rect rect = new Rect.fromCircle(
center: new Offset(165.0, 55.0),
radius: 180.0,
);

// a fancy rainbow gradient
final Gradient gradient = new RadialGradient(
colors: <Color>[
Colors.green.withOpacity(1.0),
Colors.green.withOpacity(0.3),
Colors.yellow.withOpacity(0.2),
Colors.red.withOpacity(0.1),
Colors.red.withOpacity(0.0),
],
stops: [
0.0,
0.5,
0.7,
0.9,
1.0,
],
);

// create the Shader from the gradient and the bounding square
final Paint paint = new Paint()..shader = gradient.createShader(rect);

// and draw an arc
canvas.drawArc(rect, pi / 4, pi * 3 / 4, true, paint);
}

@override
bool shouldRepaint(X1Painter oldDelegate) {
return true;
}
}

class X1Demo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: const Text('Arcs etc')),
body: new CustomPaint(
painter: new X1Painter(),
),
);
}
}

void main() {
runApp(
new MaterialApp(
theme: new ThemeData.dark(),
home: new X1Demo(),
),
);
}

关于dart - Flutter中的弧形渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50225628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com