Skip to content

Instantly share code, notes, and snippets.

@AnkitChowdhury
Last active August 11, 2020 12:42
Show Gist options
  • Save AnkitChowdhury/ac79cbf8e16a919fa174e20ab183f576 to your computer and use it in GitHub Desktop.
Save AnkitChowdhury/ac79cbf8e16a919fa174e20ab183f576 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
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);
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();
}
}
@D4-Developer
Copy link

still can't understand how to change radius of range slider....
I mean how to use this code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment