如何在Flutter中绘制具有圆角矩形轨道的滑块?

3

如何在Flutter中绘制一个带有圆角矩形轨道的滑块?

我尝试使用SliderTheme,但是当我将RoundedRectSliderTrackShape()作为轨道形状时,它不允许我指定边框半径。

是否只能通过使用自定义绘图来实现这一点?

1个回答

2
你可以使用这段代码。

import 'package:flutter/material.dart';

class RoundedRectangleSeekbarShape extends SliderComponentShape {
  //The radius of the thumb
  final double thumbRadius;

  //the thickness of the border
  final double thickness;

  //the roundness of the corners
  final double roundness;

  RoundedRectangleSeekbarShape(
      {this.thumbRadius = 6.0, this.thickness = 2, this.roundness = 6.0});

  @override
  Size getPreferredSize(bool isEnabled, bool isDiscrete) {
    return Size.fromRadius(thumbRadius);
  }

  @override
  void paint(PaintingContext context, Offset center,
      {required Animation<double> activationAnimation,
      required Animation<double> enableAnimation,
      bool? isDiscrete,
      required TextPainter labelPainter,
      required RenderBox parentBox,
      required SliderThemeData sliderTheme,
      double? value,
      double? textScaleFactor,
      Size? sizeWithOverflow,
      TextDirection? textDirection,
      Thumb? thumb,
      bool? isPressed}) {
    final Canvas canvas = context.canvas;

    final rect = Rect.fromCircle(center: center, radius: thumbRadius);

    final roundedRectangle = RRect.fromRectAndRadius(
      Rect.fromPoints(
        Offset(rect.left - 1, rect.top),
        Offset(rect.right + 1, rect.bottom),
      ),
      Radius.circular(roundness),
    );

    final fillPaint = Paint()
      ..color = Colors.white
      ..style = PaintingStyle.fill;

    final borderPaint = Paint()
      ..color = sliderTheme.thumbColor!
      ..strokeWidth = thickness
      ..style = PaintingStyle.stroke;

    canvas.drawRRect(roundedRectangle, fillPaint);
    canvas.drawRRect(roundedRectangle, borderPaint);
  }
}

//**call from SliderTheme widget.
SliderTheme(
                                  data: SliderTheme.of(context).copyWith(
                                    trackHeight: 1,
                                    activeTrackColor: CustomColors.orange,
                                    inactiveTrackColor: Colors.white,
                                    thumbColor: CustomColors.orange,
                                    overlayShape: RoundSliderOverlayShape(
                                        overlayRadius: 1),
                                    trackShape: RectangularSliderTrackShape(),
                                    rangeThumbShape: RoundedRectangleThumbShape(
                                      thumbRadius: 12,
                                      roundness: 6,
                                      thickness: 1,
                                    ),
                                    thumbShape: RoundedRectangleSeekbarShape(
                                      thumbRadius: 12,
                                      roundness: 6,
                                      thickness: 1,
                                    ),
                                  ),
                                  child: Slider(
                                    value: 0,
                                    onChanged: (rangeValue) {
                                     
                                    },
                                 
                                    min: 0,
                                    max: 100,
                                    divisions: 100,
                                  ),
                                )


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接