如何修复 Flutter 中的“The method 'CustomTimerPainter' isn't Defined”错误?

问题描述 投票:0回答:2

我在 Flutter 倒数计时器实现中遇到以下错误:

 59:48: Error: The method 'CustomTimerPainter' isn't defined for the class '_CountDownTimerState'.
 - '_CountDownTimerState' is from 'package:braintrinig/pages/Countdown_timer.dart' ('lib/pages/Countdown_timer.dart'). Try correcting the name to the name of an existing method, or defining a method named 'CustomTimerPainter'.
                    painter: CustomTimerPainter(

相关代码:

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

class CountDownTimer extends StatefulWidget {
  // ...
}

class _CountDownTimerState extends State<CountDownTimer> with TickerProviderStateMixin {
  // ... 

  @override
  Widget build(BuildContext context) {
    // ... 

            CustomPaint(
              painter: CustomTimerPainter( 
                 animation: controller,
                 backgroundColor: Colors.white,
                 color: themeData.indicatorColor,
              ),
            ),

    // ... 
  }
}

我尝试过的事情:

  • 检查了我的导入(我看不到

    CustomTimerPainter
    可能在哪里定义)。

  • 验证具有

    CustomTimerPainter
    类的文件是否可访问并正确导入。

问题: 您能帮我确定为什么会出现此错误以及如何解决它吗?

flutter dart countdowntimer custom-painter
2个回答
2
投票

CustomeTimerPainter 是一个自定义小部件,有人已经在您的示例中编写了,

您可以检查这些示例,或者您可以使用下面的示例。

class CustomTimerPainter extends CustomPainter {
  CustomTimerPainter({
    this.animation,
    this.backgroundColor,
    this.color,
  }) : super(repaint: animation);

  final Animation<double> animation;
  final Color backgroundColor, color;

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = backgroundColor
      ..strokeWidth = 10.0
      ..strokeCap = StrokeCap.butt
      ..style = PaintingStyle.stroke;

    canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
    paint.color = color;
    double progress = (1.0 - animation.value) * 2 * math.pi;
    canvas.drawArc(Offset.zero & size, math.pi * 1.5, -progress, false, paint);
  }

  @override
  bool shouldRepaint(CustomTimerPainter old) {
    return animation.value != old.animation.value ||
        color != old.color ||
        backgroundColor != old.backgroundColor;
  }
}

1
投票

检查“CustomTimerPainter”来自circular_countdown_timer库。 添加此库后,您提到的问题将得到解决。

我将你的代码粘贴到我的项目中并安装“circular_countdown_timer”,我现在没有看到任何错误。

circular_countdown_timer pub.dev

  1. 在 pubspec.yaml => get => update 中添加circular_countdown_timer。
dependencies:
  circular_countdown_timer: ^0.2.2
  1. 将此库导入到您的 dart 文件中
import 'package:circular_countdown_timer/custom_timer_painter.dart';
© www.soinside.com 2019 - 2024. All rights reserved.