Flutter 如何扩展BorderSide以生成渐变边框
我试图通过扩展“border Side”类向border添加渐变,如下所示:Flutter 如何扩展BorderSide以生成渐变边框,flutter,dart,Flutter,Dart,我试图通过扩展“border Side”类向border添加渐变,如下所示: class GradientBorderSide extends BorderSide { const GradientBorderSide( {this.color = const Color(0x00000000), this.width = 1.0, this.style = BorderStyle.solid, this.gradient =
class GradientBorderSide extends BorderSide {
const GradientBorderSide(
{this.color = const Color(0x00000000),
this.width = 1.0,
this.style = BorderStyle.solid,
this.gradient =
const LinearGradient(colors: [Colors.black, Colors.white])})
: super(color: color, width: width, style: style);
final Gradient gradient;
final Color color;
final double width;
final BorderStyle style;
@override
Paint toPaint() {
switch (style) {
case BorderStyle.solid:
return Paint()
..strokeWidth = width
..style = PaintingStyle.stroke
..shader = gradient.createShader(Rect.fromCenter(
center: Offset.zero, width: width, height: width));
case BorderStyle.none:
return Paint()
..color = const Color(0x00000000)
..strokeWidth = 0.0
..style = PaintingStyle.stroke;
}
return null;
}
}
然后将其应用于按钮:
SizedBox(
width: 75,
height: 75,
child: RaisedButton(
onPressed: () {},
shape: CircleBorder(
//borderRadius: BorderRadius.circular(5),
side: GradientBorderSide(
width: 10,
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment(0.5, .05),
colors: [Colors.red, Colors.orange],
tileMode: TileMode.repeated,
)),
),
),
)
渐变看起来不错,但当按下按钮时,渐变消失,边框颜色显示是否有颜色,然后渐变重新出现。如果您想给边框添加渐变,则可以实现相同的效果,然后只需将两个容器堆叠在一起,一个容器应该比第二个容器大一点,然后将渐变应用到底部的容器底部的容器将作为边界。好主意,非常感谢。但我需要它更通用,通过扩展border端,它可以应用于任何接受border的小部件。