Skip to content

Instantly share code, notes, and snippets.

@AnkitChowdhury
Last active August 11, 2020 12:28
Show Gist options
  • Save AnkitChowdhury/08119c657b3e52ec2b93461c38dc93ba to your computer and use it in GitHub Desktop.
Save AnkitChowdhury/08119c657b3e52ec2b93461c38dc93ba to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class CustomSliderThumbCircle extends SliderComponentShape {
final double thumbRadius;
final int min;
final int max;
const CustomSliderThumbCircle({
@required this.thumbRadius,
this.min = 0,
this.max = 10,
});
@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 paint = Paint()
..color = Colors.white //Thumb Background Color
..style = PaintingStyle.fill;
TextSpan span = new TextSpan(
style: new TextStyle(
fontSize: thumbRadius * .8,
fontWeight: FontWeight.w700,
color: sliderTheme.thumbColor, //Text Color of Value on Thumb
),
text: getValue(value),
);
TextPainter tp = new TextPainter(
text: span,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr);
tp.layout();
Offset textCenter =
Offset(center.dx - (tp.width / 2), center.dy - (tp.height / 2));
canvas.drawCircle(center, thumbRadius * .9, paint);
tp.paint(canvas, textCenter);
}
String getValue(double value) {
return (min+(max-min)*value).round().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment