首页 > 解决方案 > 如何在 Flutter 中覆盖 SliderComponentShape 类中的绘制方法?

问题描述

我在颤振中使用 Slider 类。我想改变滑块的拇指形状,为此我正在创建一个下面的类。一切都很好,但重写“paint()”方法是在类下面抛出错误。请参阅下面的代码并尽可能提供任何解决方案。

        class CustomSliderThumbRect extends SliderComponentShape {
      final double thumbRadius;
      final thumbHeight;
      final int min;
      final int max;
    
      const CustomSliderThumbRect({
        this.thumbRadius,
        this.thumbHeight,
        this.min,
        this.max,
      });
    
      @override
      Size getPreferredSize(bool isEnabled, bool isDiscrete) {
        return Size.fromRadius(thumbRadius);
      }
    
      @override
      void paint (
          PaintingContext context,
          Offset center, {
            Animation<double> activationAnimation,
            Animation<double> enableAnimation,
            bool isDiscrete,
            TextPainter labelPainter,
            RenderBox parentBox,
            SliderThemeData sliderTheme,
            TextDirection textDirection,
            double value,
            double textScaleFactor,
            Size sizeWithOverflow,
          })
      {
        final Canvas canvas = context.canvas;
    
        final rRect = RRect.fromRectAndRadius(
          Rect.fromCenter(
              center: center, width: thumbHeight * 1.2, height: thumbHeight * .6),
          Radius.circular(thumbRadius * .4),
        );
    
        final paint = Paint()
          ..color = sliderTheme.activeTrackColor //Thumb Background Color
          ..style = PaintingStyle.fill;
    
        TextSpan span = new TextSpan(
            style: new TextStyle(
                fontSize: thumbHeight * .3,
                fontWeight: FontWeight.w700,
                color: sliderTheme.thumbColor,
                height: 1),
            text: '${getValue(value)}');
        TextPainter tp = new TextPainter(
          text: span,
          textAlign: TextAlign.left,
          // textDirection: TextDirection.ltr
          // textDirection: ui.TextDirection.ltr,
        );
        tp.layout();
        Offset textCenter =
        Offset(center.dx - (tp.width / 2), center.dy - (tp.height / 2));
    
        canvas.drawRRect(rRect, paint);
        tp.paint(canvas, textCenter);
      }
    
      String getValue(double value) {
        return (min+(max-min)*value).round().toString();
      }
    }

错误

方法“CustomSliderThumbRect.paint”的参数“textDirection”的类型为“TextDirection/ 1 /”,与覆盖方法“SliderComponentShape.paint”中的相应类型“TextDirection/ 2 /”不匹配。

标签: flutterflutter-slider

解决方案


推荐阅读