Flutter 带梯度的颤振滑块
我想知道是否有一种方法可以用渐变色在Flutter中创建一个滑块小部件。是指向slider类的链接,但没有“SliderTheme”之类的内容。我现在基于youtube视频创建了一个自定义轨迹形状类,并在构造函数中添加了渐变属性 如何使用它: 梯度:Flutter 带梯度的颤振滑块,flutter,slider,gradient,Flutter,Slider,Gradient,我想知道是否有一种方法可以用渐变色在Flutter中创建一个滑块小部件。是指向slider类的链接,但没有“SliderTheme”之类的内容。我现在基于youtube视频创建了一个自定义轨迹形状类,并在构造函数中添加了渐变属性 如何使用它: 梯度: LinearGradient gradient = LinearGradient( colors: <Color> [ Colors.red, Colors.orange, Colors.yellow, Colors.
LinearGradient gradient = LinearGradient(
colors: <Color> [
Colors.red,
Colors.orange,
Colors.yellow,
Colors.green,
Colors.blue,
Colors.blue[900],
Colors.purple
]
);
import 'package:flutter/material.dart';
class GradientRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape {
/// Based on https://www.youtube.com/watch?v=Wl4F5V6BoJw
/// Create a slider track that draws two rectangles with rounded outer edges.
final LinearGradient gradient;
final bool darkenInactive;
const GradientRectSliderTrackShape({ this.gradient: const LinearGradient(colors: [Colors.lightBlue, Colors.blue]), this.darkenInactive: true});
@override
void paint(
PaintingContext context,
Offset offset, {
@required RenderBox parentBox,
@required SliderThemeData sliderTheme,
@required Animation<double> enableAnimation,
@required TextDirection textDirection,
@required Offset thumbCenter,
bool isDiscrete = false,
bool isEnabled = false,
double additionalActiveTrackHeight = 2,
}) {
assert(context != null);
assert(offset != null);
assert(parentBox != null);
assert(sliderTheme != null);
assert(sliderTheme.disabledActiveTrackColor != null);
assert(sliderTheme.disabledInactiveTrackColor != null);
assert(sliderTheme.activeTrackColor != null);
assert(sliderTheme.inactiveTrackColor != null);
assert(sliderTheme.thumbShape != null);
assert(enableAnimation != null);
assert(textDirection != null);
assert(thumbCenter != null);
// If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
// then it makes no difference whether the track is painted or not,
// therefore the painting can be a no-op.
if (sliderTheme.trackHeight <= 0) {
return;
}
final Rect trackRect = getPreferredRect(
parentBox: parentBox,
offset: offset,
sliderTheme: sliderTheme,
isEnabled: isEnabled,
isDiscrete: isDiscrete,
);
// Assign the track segment paints, which are leading: active and
// trailing: inactive.
final ColorTween activeTrackColorTween = ColorTween(begin: sliderTheme.disabledActiveTrackColor, end: sliderTheme.activeTrackColor);
final ColorTween inactiveTrackColorTween = darkenInactive
? ColorTween(begin: sliderTheme.disabledInactiveTrackColor, end: sliderTheme.inactiveTrackColor)
: activeTrackColorTween;
final Paint activePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = activeTrackColorTween.evaluate(enableAnimation);
final Paint inactivePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = inactiveTrackColorTween.evaluate(enableAnimation);
Paint leftTrackPaint;
Paint rightTrackPaint;
switch (textDirection) {
case TextDirection.ltr:
leftTrackPaint = activePaint;
rightTrackPaint = inactivePaint;
break;
case TextDirection.rtl:
leftTrackPaint = inactivePaint;
rightTrackPaint = activePaint;
break;
}
final Radius trackRadius = Radius.circular(trackRect.height / 2);
final Radius activeTrackRadius = Radius.circular(trackRect.height / 2 + 1);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
trackRect.left,
(textDirection == TextDirection.ltr) ? trackRect.top - (additionalActiveTrackHeight / 2): trackRect.top,
thumbCenter.dx,
(textDirection == TextDirection.ltr) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius : trackRadius,
bottomLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius: trackRadius,
),
leftTrackPaint,
);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
thumbCenter.dx,
(textDirection == TextDirection.rtl) ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
trackRect.right,
(textDirection == TextDirection.rtl) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
bottomRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
),
rightTrackPaint,
);
}
}
形状类别:
LinearGradient gradient = LinearGradient(
colors: <Color> [
Colors.red,
Colors.orange,
Colors.yellow,
Colors.green,
Colors.blue,
Colors.blue[900],
Colors.purple
]
);
import 'package:flutter/material.dart';
class GradientRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape {
/// Based on https://www.youtube.com/watch?v=Wl4F5V6BoJw
/// Create a slider track that draws two rectangles with rounded outer edges.
final LinearGradient gradient;
final bool darkenInactive;
const GradientRectSliderTrackShape({ this.gradient: const LinearGradient(colors: [Colors.lightBlue, Colors.blue]), this.darkenInactive: true});
@override
void paint(
PaintingContext context,
Offset offset, {
@required RenderBox parentBox,
@required SliderThemeData sliderTheme,
@required Animation<double> enableAnimation,
@required TextDirection textDirection,
@required Offset thumbCenter,
bool isDiscrete = false,
bool isEnabled = false,
double additionalActiveTrackHeight = 2,
}) {
assert(context != null);
assert(offset != null);
assert(parentBox != null);
assert(sliderTheme != null);
assert(sliderTheme.disabledActiveTrackColor != null);
assert(sliderTheme.disabledInactiveTrackColor != null);
assert(sliderTheme.activeTrackColor != null);
assert(sliderTheme.inactiveTrackColor != null);
assert(sliderTheme.thumbShape != null);
assert(enableAnimation != null);
assert(textDirection != null);
assert(thumbCenter != null);
// If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
// then it makes no difference whether the track is painted or not,
// therefore the painting can be a no-op.
if (sliderTheme.trackHeight <= 0) {
return;
}
final Rect trackRect = getPreferredRect(
parentBox: parentBox,
offset: offset,
sliderTheme: sliderTheme,
isEnabled: isEnabled,
isDiscrete: isDiscrete,
);
// Assign the track segment paints, which are leading: active and
// trailing: inactive.
final ColorTween activeTrackColorTween = ColorTween(begin: sliderTheme.disabledActiveTrackColor, end: sliderTheme.activeTrackColor);
final ColorTween inactiveTrackColorTween = darkenInactive
? ColorTween(begin: sliderTheme.disabledInactiveTrackColor, end: sliderTheme.inactiveTrackColor)
: activeTrackColorTween;
final Paint activePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = activeTrackColorTween.evaluate(enableAnimation);
final Paint inactivePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = inactiveTrackColorTween.evaluate(enableAnimation);
Paint leftTrackPaint;
Paint rightTrackPaint;
switch (textDirection) {
case TextDirection.ltr:
leftTrackPaint = activePaint;
rightTrackPaint = inactivePaint;
break;
case TextDirection.rtl:
leftTrackPaint = inactivePaint;
rightTrackPaint = activePaint;
break;
}
final Radius trackRadius = Radius.circular(trackRect.height / 2);
final Radius activeTrackRadius = Radius.circular(trackRect.height / 2 + 1);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
trackRect.left,
(textDirection == TextDirection.ltr) ? trackRect.top - (additionalActiveTrackHeight / 2): trackRect.top,
thumbCenter.dx,
(textDirection == TextDirection.ltr) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius : trackRadius,
bottomLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius: trackRadius,
),
leftTrackPaint,
);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
thumbCenter.dx,
(textDirection == TextDirection.rtl) ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
trackRect.right,
(textDirection == TextDirection.rtl) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
bottomRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
),
rightTrackPaint,
);
}
}
导入“包装:颤振/材料.省道”;
类GradientRectSliderTrackShape使用BaseSliderTrackShape扩展了SliderTrackShape{
///基于https://www.youtube.com/watch?v=Wl4F5V6BoJw
///创建一个滑块轨迹,用于绘制两个外边缘为圆形的矩形。
最终线性梯度;
最终bool变暗激活;
const gradientrectlidertrackshape({this.gradient:const LinearGradient(colors:[colors.lightBlue,colors.blue]),this.darkininactive:true});
@凌驾
空漆(
绘画语境,
抵消{
@必需的RenderBox父框,
@所需幻灯片主题数据幻灯片主题,
@所需的动画启用,
@所需文本方向文本方向,
@所需偏移拇指中心,
bool isDiscrete=false,
bool isEnabled=false,
双重附加ActiveTrackHeight=2,
}) {
断言(上下文!=null);
断言(偏移量!=null);
断言(parentBox!=null);
断言(sliderTheme!=null);
断言(sliderTheme.disabledActiveTrackColor!=null);
断言(sliderTheme.disabledInactiveTrackColor!=null);
断言(sliderTheme.activeTrackColor!=null);
断言(sliderTheme.inactiveTrackColor!=null);
断言(sliderTheme.thumbShape!=null);
断言(enableAnimation!=null);
断言(textDirection!=null);
断言(thumbCenter!=null);
//如果滑块[SliderThemeData.trackHeight]小于或等于0,
//那么轨道是否上漆也没什么区别,
//因此,这幅画可以是禁止使用的。
if(sliderTheme.trackHeight我现在基于youtube视频创建了一个自定义轨迹形状类,并在构造函数中添加了渐变属性
如何使用它:
梯度:
LinearGradient gradient = LinearGradient(
colors: <Color> [
Colors.red,
Colors.orange,
Colors.yellow,
Colors.green,
Colors.blue,
Colors.blue[900],
Colors.purple
]
);
import 'package:flutter/material.dart';
class GradientRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape {
/// Based on https://www.youtube.com/watch?v=Wl4F5V6BoJw
/// Create a slider track that draws two rectangles with rounded outer edges.
final LinearGradient gradient;
final bool darkenInactive;
const GradientRectSliderTrackShape({ this.gradient: const LinearGradient(colors: [Colors.lightBlue, Colors.blue]), this.darkenInactive: true});
@override
void paint(
PaintingContext context,
Offset offset, {
@required RenderBox parentBox,
@required SliderThemeData sliderTheme,
@required Animation<double> enableAnimation,
@required TextDirection textDirection,
@required Offset thumbCenter,
bool isDiscrete = false,
bool isEnabled = false,
double additionalActiveTrackHeight = 2,
}) {
assert(context != null);
assert(offset != null);
assert(parentBox != null);
assert(sliderTheme != null);
assert(sliderTheme.disabledActiveTrackColor != null);
assert(sliderTheme.disabledInactiveTrackColor != null);
assert(sliderTheme.activeTrackColor != null);
assert(sliderTheme.inactiveTrackColor != null);
assert(sliderTheme.thumbShape != null);
assert(enableAnimation != null);
assert(textDirection != null);
assert(thumbCenter != null);
// If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
// then it makes no difference whether the track is painted or not,
// therefore the painting can be a no-op.
if (sliderTheme.trackHeight <= 0) {
return;
}
final Rect trackRect = getPreferredRect(
parentBox: parentBox,
offset: offset,
sliderTheme: sliderTheme,
isEnabled: isEnabled,
isDiscrete: isDiscrete,
);
// Assign the track segment paints, which are leading: active and
// trailing: inactive.
final ColorTween activeTrackColorTween = ColorTween(begin: sliderTheme.disabledActiveTrackColor, end: sliderTheme.activeTrackColor);
final ColorTween inactiveTrackColorTween = darkenInactive
? ColorTween(begin: sliderTheme.disabledInactiveTrackColor, end: sliderTheme.inactiveTrackColor)
: activeTrackColorTween;
final Paint activePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = activeTrackColorTween.evaluate(enableAnimation);
final Paint inactivePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = inactiveTrackColorTween.evaluate(enableAnimation);
Paint leftTrackPaint;
Paint rightTrackPaint;
switch (textDirection) {
case TextDirection.ltr:
leftTrackPaint = activePaint;
rightTrackPaint = inactivePaint;
break;
case TextDirection.rtl:
leftTrackPaint = inactivePaint;
rightTrackPaint = activePaint;
break;
}
final Radius trackRadius = Radius.circular(trackRect.height / 2);
final Radius activeTrackRadius = Radius.circular(trackRect.height / 2 + 1);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
trackRect.left,
(textDirection == TextDirection.ltr) ? trackRect.top - (additionalActiveTrackHeight / 2): trackRect.top,
thumbCenter.dx,
(textDirection == TextDirection.ltr) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius : trackRadius,
bottomLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius: trackRadius,
),
leftTrackPaint,
);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
thumbCenter.dx,
(textDirection == TextDirection.rtl) ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
trackRect.right,
(textDirection == TextDirection.rtl) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
bottomRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
),
rightTrackPaint,
);
}
}
形状类别:
LinearGradient gradient = LinearGradient(
colors: <Color> [
Colors.red,
Colors.orange,
Colors.yellow,
Colors.green,
Colors.blue,
Colors.blue[900],
Colors.purple
]
);
import 'package:flutter/material.dart';
class GradientRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape {
/// Based on https://www.youtube.com/watch?v=Wl4F5V6BoJw
/// Create a slider track that draws two rectangles with rounded outer edges.
final LinearGradient gradient;
final bool darkenInactive;
const GradientRectSliderTrackShape({ this.gradient: const LinearGradient(colors: [Colors.lightBlue, Colors.blue]), this.darkenInactive: true});
@override
void paint(
PaintingContext context,
Offset offset, {
@required RenderBox parentBox,
@required SliderThemeData sliderTheme,
@required Animation<double> enableAnimation,
@required TextDirection textDirection,
@required Offset thumbCenter,
bool isDiscrete = false,
bool isEnabled = false,
double additionalActiveTrackHeight = 2,
}) {
assert(context != null);
assert(offset != null);
assert(parentBox != null);
assert(sliderTheme != null);
assert(sliderTheme.disabledActiveTrackColor != null);
assert(sliderTheme.disabledInactiveTrackColor != null);
assert(sliderTheme.activeTrackColor != null);
assert(sliderTheme.inactiveTrackColor != null);
assert(sliderTheme.thumbShape != null);
assert(enableAnimation != null);
assert(textDirection != null);
assert(thumbCenter != null);
// If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
// then it makes no difference whether the track is painted or not,
// therefore the painting can be a no-op.
if (sliderTheme.trackHeight <= 0) {
return;
}
final Rect trackRect = getPreferredRect(
parentBox: parentBox,
offset: offset,
sliderTheme: sliderTheme,
isEnabled: isEnabled,
isDiscrete: isDiscrete,
);
// Assign the track segment paints, which are leading: active and
// trailing: inactive.
final ColorTween activeTrackColorTween = ColorTween(begin: sliderTheme.disabledActiveTrackColor, end: sliderTheme.activeTrackColor);
final ColorTween inactiveTrackColorTween = darkenInactive
? ColorTween(begin: sliderTheme.disabledInactiveTrackColor, end: sliderTheme.inactiveTrackColor)
: activeTrackColorTween;
final Paint activePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = activeTrackColorTween.evaluate(enableAnimation);
final Paint inactivePaint = Paint()
..shader = gradient.createShader(trackRect)
..color = inactiveTrackColorTween.evaluate(enableAnimation);
Paint leftTrackPaint;
Paint rightTrackPaint;
switch (textDirection) {
case TextDirection.ltr:
leftTrackPaint = activePaint;
rightTrackPaint = inactivePaint;
break;
case TextDirection.rtl:
leftTrackPaint = inactivePaint;
rightTrackPaint = activePaint;
break;
}
final Radius trackRadius = Radius.circular(trackRect.height / 2);
final Radius activeTrackRadius = Radius.circular(trackRect.height / 2 + 1);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
trackRect.left,
(textDirection == TextDirection.ltr) ? trackRect.top - (additionalActiveTrackHeight / 2): trackRect.top,
thumbCenter.dx,
(textDirection == TextDirection.ltr) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius : trackRadius,
bottomLeft: (textDirection == TextDirection.ltr) ? activeTrackRadius: trackRadius,
),
leftTrackPaint,
);
context.canvas.drawRRect(
RRect.fromLTRBAndCorners(
thumbCenter.dx,
(textDirection == TextDirection.rtl) ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
trackRect.right,
(textDirection == TextDirection.rtl) ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
topRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
bottomRight: (textDirection == TextDirection.rtl) ? activeTrackRadius : trackRadius,
),
rightTrackPaint,
);
}
}
导入“包装:颤振/材料.省道”;
类GradientRectSliderTrackShape使用BaseSliderTrackShape扩展了SliderTrackShape{
///基于https://www.youtube.com/watch?v=Wl4F5V6BoJw
///创建一个滑块轨迹,用于绘制两个外边缘为圆形的矩形。
最终线性梯度;
最终bool变暗激活;
const gradientrectlidertrackshape({this.gradient:const LinearGradient(colors:[colors.lightBlue,colors.blue]),this.darkininactive:true});
@凌驾
空漆(
绘画语境,
抵消{
@必需的RenderBox父框,
@所需幻灯片主题数据幻灯片主题,
@所需的动画启用,
@所需文本方向文本方向,
@所需偏移拇指中心,
bool isDiscrete=false,
bool isEnabled=false,
双重附加ActiveTrackHeight=2,
}) {
断言(上下文!=null);
断言(偏移量!=null);
断言(parentBox!=null);
断言(sliderTheme!=null);
断言(sliderTheme.disabledActiveTrackColor!=null);
断言(sliderTheme.disabledInactiveTrackColor!=null);
断言(sliderTheme.activeTrackColor!=null);
断言(sliderTheme.inactiveTrackColor!=null);
断言(sliderTheme.thumbShape!=null);
断言(enableAnimation!=null);
断言(textDirection!=null);
断言(thumbCenter!=null);
//如果滑块[SliderThemeData.trackHeight]小于或等于0,
//那么轨道是否上漆也没什么区别,
//因此,这幅画可以是禁止使用的。
如果(sliderTheme.trackHeight使用这个库,它很容易实现
使用这个库。它很容易实现
您所说的”是什么意思,但没有“SliderTheme”
?例如,在文本小部件中,您可以添加样式(TextTheme)更改大小、颜色和字体。-如果您没有安装本地API文档,您可以随时检查-它们在右上角有漂亮的Search API docs
Search字段,但是如何添加渐变?只有颜色属性,没有绘画(文本小部件)或者渐变属性。检查*Shape
属性你所说的'是什么意思,但是没有“SliderTheme”
?例如,在文本小部件中,你可以添加样式(TextTheme)更改大小、颜色和字体。-如果您没有安装本地API文档,您可以随时检查-它们在右上角有漂亮的搜索API文档搜索字段,但是如何添加渐变?只有颜色属性,没有绘画(文本小部件)或渐变属性。检查*形状属性