gpt4 book ai didi

flutter - dart/flutter:使用ui.Gradient.linear时的语法错误

转载 作者:行者123 更新时间:2023-12-03 04:54:23 43 4
gpt4 key购买 nike

我在flutter项目中陷入了Android Studio中的语法错误,无法使用ui.Gradient构造函数实例化ui.Gradient.linear



这在CustomPainter子类中。
我在下面的评论中发布了编译器错误。

  import 'dart:ui';

...

@override
void paint(Canvas canvas, Size size) {
final tracePaint = Paint()
..strokeJoin = StrokeJoin.round
..strokeWidth = 2.0
..color = traceColor
..style = PaintingStyle.stroke;

...

// HERE: I've tried several ways as below and I simply cannot use this constructor.

tracePaint.shader = Gradient.linear(
from: Offset(size.width-currentX.toDouble(), size.height),
to: Offset(size.width-currentX.toDouble(), 0),
colors: colors,
colorStops: colorStops,).createShader(...); // The method 'linear' isn't defined for the class Gradient.
tracePaint.shader = ui.Gradient.linear(...).createShader(...); // Undefined name 'ui'.
tracePaint.shader = dart.ui.Gradient.linear(...).createShader(...); // Undefined name 'dart'.



所做的努力

我检查了文档:
  • https://api.flutter.dev/flutter/dart-ui/Gradient-class.html
  • https://api.flutter.dev/flutter/dart-ui/Gradient/Gradient.linear.html

  • ,但找不到我错了。

    提示将不胜感激。

    最佳答案

    您可以在下面复制粘贴运行完整代码
    第1步:import 'dart:ui' as ui;第2步:请删除from: to: colors: colorStops:
    工作演示

    enter image description here

    完整的代码

    import 'dart:math';
    import 'dart:ui' as ui;

    import 'package:flutter/material.dart';

    class X1Painter extends CustomPainter {
    @override
    void paint(Canvas canvas, Size size) {
    final tracePaint = Paint()
    ..strokeJoin = StrokeJoin.round
    ..strokeWidth = 2.0
    ..color = Colors.blue
    ..style = PaintingStyle.stroke;

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

    tracePaint.shader = ui.Gradient.linear(
    Offset(0, 0),
    Offset(200, 200),
    <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),
    ],
    [
    0.0,
    0.5,
    0.7,
    0.9,
    1.0,
    ],
    );

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

    @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(),
    ),
    );
    }

    关于flutter - dart/flutter:使用ui.Gradient.linear时的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60861288/

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