![](/img/trans.png)
[英]How to give Rounded rectangular shape for FadeInImage.assetNetwork image in flutter?
[英]How to give rectangular shape to the thumb icon in slider in flutter?
我正在學習 flutter。我使用 Slider class 創建 slider(在 android 中稱為 Seekbar),在這里我想將拇指圖標更改為矩形,是否可能,請讓我知道對此有任何解決方案嗎?
SliderTheme.of(context).copyWith(
trackShape: CustomTrackShape(),
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: 15,
disabledThumbRadius: 5,
),
),
child: Slider(
inactiveColor: Colors.black12,
value: _sliderValue,
min: 5000,
max: 500000,
divisions: 1000,
label: _sliderValue.round().toString(),
onChanged: (value) {
setState(() {
_sliderValue = value;
print("slider value" + _sliderValue.toString());
// _amountController.text = value.floor().toString();
_amountController.text =
getChangedAmount(value.floor().toString());
// print("amount controller value" + _amountController.text);
});
},
),
),
** 我想要下面的類型 **
您可以使用SliderComponentShape創建自定義thumbShape
。 在我的實現中,我只是修改了默認RoundSliderThumbShape中的繪畫。
class RectSliderThumbShape extends SliderComponentShape {
/// Create a slider thumb that draws a Rect.
const RectSliderThumbShape({
this.enabledThumbRadius = 10.0,
this.disabledThumbRadius,
this.elevation = 1.0,
this.pressedElevation = 6.0,
});
/// The preferred radius of the round thumb shape when the slider is enabled.
///
/// If it is not provided, then the Material Design default of 10 is used.
final double enabledThumbRadius;
/// The preferred radius of the round thumb shape when the slider is disabled.
///
/// If no disabledRadius is provided, then it is equal to the
/// [enabledThumbRadius]
final double? disabledThumbRadius;
double get _disabledThumbRadius => disabledThumbRadius ?? enabledThumbRadius;
/// The resting elevation adds shadow to the unpressed thumb.
///
/// The default is 1.
///
/// Use 0 for no shadow. The higher the value, the larger the shadow. For
/// example, a value of 12 will create a very large shadow.
///
final double elevation;
/// The pressed elevation adds shadow to the pressed thumb.
///
/// The default is 6.
///
/// Use 0 for no shadow. The higher the value, the larger the shadow. For
/// example, a value of 12 will create a very large shadow.
final double pressedElevation;
@override
Size getPreferredSize(bool isEnabled, bool isDiscrete) {
return Size.fromRadius(isEnabled == true ? enabledThumbRadius : _disabledThumbRadius);
}
@override
void paint(
PaintingContext context,
Offset center, {
required Animation<double> activationAnimation,
required Animation<double> enableAnimation,
required bool isDiscrete,
required TextPainter labelPainter,
required RenderBox parentBox,
required SliderThemeData sliderTheme,
required TextDirection textDirection,
required double value,
required double textScaleFactor,
required Size sizeWithOverflow,
}) {
assert(context != null);
assert(center != null);
assert(enableAnimation != null);
assert(sliderTheme != null);
assert(sliderTheme.disabledThumbColor != null);
assert(sliderTheme.thumbColor != null);
final Canvas canvas = context.canvas;
final Tween<double> radiusTween = Tween<double>(
begin: _disabledThumbRadius,
end: enabledThumbRadius,
);
final ColorTween colorTween = ColorTween(
begin: sliderTheme.disabledThumbColor,
end: sliderTheme.thumbColor,
);
final Color color = colorTween.evaluate(enableAnimation)!;
final double radius = radiusTween.evaluate(enableAnimation);
final Tween<double> elevationTween = Tween<double>(
begin: elevation,
end: pressedElevation,
);
final double evaluatedElevation = elevationTween.evaluate(activationAnimation);
final Path path = Path()
..addArc(Rect.fromCenter(center: center, width: 2 * radius, height: 2 * radius), 0, math.pi * 2);
bool paintShadows = true;
assert(() {
if (debugDisableShadows) {
paintShadows = false;
}
return true;
}());
if (paintShadows) {
canvas.drawShadow(path, Colors.black, evaluatedElevation, true);
}
// Use drawRect instead of drawCircle
canvas.drawRect(Rect.fromCircle(center: center, radius: radius),
Paint()..color = color,
);
}
}
然后在SliderTheme.thumbShape
上使用 RectSliderThumbShape
SliderTheme(
data: const SliderThemeData(
thumbColor: Colors.green,
thumbShape: RectSliderThumbShape()
),
child: Slider(
value: _currentSliderValue,
max: 100,
divisions: 5,
label: _currentSliderValue.round().toString(),
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
});
},
),
),
這是它的樣子
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.