Last active
January 16, 2020 05:58
-
-
Save supernovel/1f91feff983bc8720f27d6ffa94498a0 to your computer and use it in GitHub Desktop.
Horizontal line input border
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math' as math; | |
import 'package:flutter/material.dart'; | |
class HorizontalLineBorder extends InputBorder { | |
const HorizontalLineBorder({ | |
BorderSide borderSide = const BorderSide(), | |
this.borderRadius = const BorderRadius.only(), | |
}) : assert(borderRadius != null), | |
super(borderSide: borderSide); | |
final BorderRadius borderRadius; | |
@override | |
bool get isOutline => true; | |
@override | |
HorizontalLineBorder copyWith( | |
{BorderSide borderSide, BorderRadius borderRadius}) { | |
return HorizontalLineBorder( | |
borderSide: borderSide ?? this.borderSide, | |
borderRadius: borderRadius ?? this.borderRadius, | |
); | |
} | |
@override | |
EdgeInsetsGeometry get dimensions { | |
return EdgeInsets.only(bottom: borderSide.width); | |
} | |
@override | |
HorizontalLineBorder scale(double t) { | |
return HorizontalLineBorder(borderSide: borderSide.scale(t)); | |
} | |
@override | |
Path getInnerPath(Rect rect, {TextDirection textDirection}) { | |
return Path() | |
..addRect(Rect.fromLTWH(rect.left, rect.top, rect.width, | |
math.max(0.0, rect.height - borderSide.width))); | |
} | |
@override | |
Path getOuterPath(Rect rect, {TextDirection textDirection}) { | |
return Path()..addRRect(borderRadius.resolve(textDirection).toRRect(rect)); | |
} | |
@override | |
ShapeBorder lerpFrom(ShapeBorder a, double t) { | |
if (a is HorizontalLineBorder) { | |
return HorizontalLineBorder( | |
borderSide: BorderSide.lerp(a.borderSide, borderSide, t), | |
borderRadius: BorderRadius.lerp(a.borderRadius, borderRadius, t), | |
); | |
} | |
return super.lerpFrom(a, t); | |
} | |
@override | |
ShapeBorder lerpTo(ShapeBorder b, double t) { | |
if (b is HorizontalLineBorder) { | |
return HorizontalLineBorder( | |
borderSide: BorderSide.lerp(borderSide, b.borderSide, t), | |
borderRadius: BorderRadius.lerp(borderRadius, b.borderRadius, t), | |
); | |
} | |
return super.lerpTo(b, t); | |
} | |
@override | |
void paint( | |
Canvas canvas, | |
Rect rect, { | |
double gapStart, | |
double gapExtent = 0.0, | |
double gapPercentage = 0.0, | |
TextDirection textDirection, | |
}) { | |
Paint paint = borderSide.toPaint(); | |
Offset topLeft = rect.topLeft; | |
Offset topRight = rect.topRight; | |
Offset bottomLeft = rect.bottomLeft; | |
Offset bottomRight = rect.bottomRight; | |
if (borderRadius.topLeft != Radius.zero) { | |
_drawArc( | |
canvas: canvas, | |
paint: paint, | |
offset: topLeft, | |
radius: borderRadius.topLeft, | |
startAngle: 180, | |
sweepAngle: 90); | |
topLeft = topLeft.translate(borderRadius.topLeft.x, 0.0); | |
} | |
if (borderRadius.topRight != Radius.zero) { | |
_drawArc( | |
canvas: canvas, | |
paint: paint, | |
offset: topRight.translate(-borderRadius.topRight.x * 2, 0.0), | |
radius: borderRadius.topRight, | |
startAngle: 270, | |
sweepAngle: 90); | |
topRight = topRight.translate(-borderRadius.topRight.x, 0.0); | |
} | |
if (borderRadius.bottomLeft != Radius.zero) { | |
_drawArc( | |
canvas: canvas, | |
paint: paint, | |
offset: bottomLeft.translate(0.0, -borderRadius.bottomLeft.y * 2), | |
radius: borderRadius.bottomLeft, | |
startAngle: 90, | |
sweepAngle: 90); | |
bottomLeft = bottomLeft.translate(borderRadius.bottomLeft.x, 0.0); | |
} | |
if (borderRadius.bottomRight != Radius.zero) { | |
_drawArc( | |
canvas: canvas, | |
paint: paint, | |
offset: bottomRight.translate( | |
-borderRadius.bottomRight.x * 2, -borderRadius.bottomRight.y * 2), | |
radius: borderRadius.bottomRight, | |
startAngle: 0, | |
sweepAngle: 90); | |
bottomRight = bottomRight.translate(-borderRadius.bottomRight.x, 0.0); | |
} | |
canvas.drawLine(topLeft, topRight, paint); | |
canvas.drawLine(bottomLeft, bottomRight, paint); | |
} | |
_drawArc( | |
{Offset offset, | |
Radius radius, | |
double startAngle, | |
double sweepAngle, | |
Canvas canvas, | |
Paint paint}) { | |
canvas.drawArc( | |
Rect.fromCenter( | |
center: Offset( | |
offset.dx + radius.x, | |
offset.dy + radius.y, | |
), | |
width: radius.x * 2, | |
height: radius.y * 2), | |
2 * math.pi * (startAngle / 360), | |
2 * math.pi * (sweepAngle / 360), | |
false, | |
paint); | |
} | |
@override | |
bool operator ==(dynamic other) { | |
if (identical(this, other)) return true; | |
if (runtimeType != other.runtimeType) return false; | |
final InputBorder typedOther = other; | |
return typedOther.borderSide == borderSide; | |
} | |
@override | |
int get hashCode => borderSide.hashCode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment