Flutter:使用ScrollController绑定CustomPainter

时间:2020-08-30 10:17:47

标签: flutter animation scrollcontroller

我有一个像下面的CustomPainter,我希望它与包含ListView.builder的父窗口小部件绑定吗?
预期的行为:当我向上或向下滚动时,画家应该绘画。

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

class PlayerPainter extends CustomPainter {
  final Animation<double> animation;
  final Paint backgroundPaint;
  final Color bgColor;
  final Color thumbColor;
  final Paint sliderPaint;
  final Paint thumbPaint;

  PlayerPainter({this.animation, this.bgColor, this.thumbColor})
      : backgroundPaint = Paint()..color = bgColor,
        sliderPaint = Paint()
          ..style = PaintingStyle.stroke
          ..strokeWidth = 3.0,
        thumbPaint = Paint()..color = thumbColor,
        super(repaint: animation);

  @override
  void paint(Canvas canvas, Size size) {
    final value = animation.value;
    final radius = size.width * 0.5;
    final thumbRadius = 4.0;
    final rect = Rect.fromCircle(center: Offset(radius, size.height), radius: radius);
    final thumbPosX = radius + (radius * -cos(value * pi));
    final thumbPosY = (size.height - thumbRadius) + ((size.height - thumbRadius) * -sin(value * pi));
    sliderPaint.shader = LinearGradient(colors: [
      const Color(0xFFCC2B5E),
      const Color(0xFF753A88),
      const Color(0xFFCC2B5E),
      const Color(0xFF753A88),
    ]).createShader(rect);

    canvas.drawArc(rect, pi, pi, false, backgroundPaint);
    canvas.drawArc(rect, pi, value * pi, false, sliderPaint);
    canvas.drawCircle(Offset(thumbPosX, thumbPosY), thumbRadius, thumbPaint);
  }

  @override
  bool shouldRepaint(PlayerPainter oldDelegate) => true;
}

parent.dart

@override
  void initState() {
    _controller = AnimationController(vsync: this);
    _scrollController.addListener(() {
      setState(() {
        _controller = AnimationController(
          value: _scrollController.offset,
          duration: Duration(seconds: 0),
          vsync: this,
        );
      });
    });
}
PlayerView(
...
  controller: _controller,
  ),

════════基础库捕获到异常Exception ══════════════════ _MusicScreenState是SingleTickerProviderStateMixin,但是创建了多个代码。

0 个答案:

没有答案