Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 53 additions & 41 deletions lib/gradient_input_border.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ class GradientOutlineInputBorder extends InputBorder {
final Gradient focusedGradient;
final Gradient unfocusedGradient;

final double gapPadding;
final BorderRadius borderRadius;

const GradientOutlineInputBorder({
BorderSide borderSide = const BorderSide(),
this.borderRadius = const BorderRadius.all(Radius.circular(4.0)),
this.gapPadding = 4.0,
@required this.focusedGradient,
@required this.unfocusedGradient,
}) : assert(borderRadius != null),
assert(gapPadding != null && gapPadding >= 0.0),
required this.focusedGradient,
required this.unfocusedGradient,
}) : assert(gapPadding >= 0.0),
super(borderSide: borderSide);

static bool _cornersAreCircular(BorderRadius borderRadius) {
Expand All @@ -24,18 +26,14 @@ class GradientOutlineInputBorder extends InputBorder {
borderRadius.bottomRight.x == borderRadius.bottomRight.y;
}

final double gapPadding;

final BorderRadius borderRadius;

@override
bool get isOutline => true;

@override
GradientOutlineInputBorder copyWith({
BorderSide borderSide,
BorderRadius borderRadius,
double gapPadding,
BorderSide? borderSide,
BorderRadius? borderRadius,
double? gapPadding,
}) {
return GradientOutlineInputBorder(
borderSide: borderSide ?? this.borderSide,
Expand All @@ -61,11 +59,12 @@ class GradientOutlineInputBorder extends InputBorder {
}

@override
ShapeBorder lerpFrom(ShapeBorder a, double t) {
ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
if (a is GradientOutlineInputBorder) {
final GradientOutlineInputBorder outline = a;
return GradientOutlineInputBorder(
borderRadius: BorderRadius.lerp(outline.borderRadius, borderRadius, t),
borderRadius:
BorderRadius.lerp(outline.borderRadius, borderRadius, t)!,
borderSide: BorderSide.lerp(outline.borderSide, borderSide, t),
gapPadding: outline.gapPadding,
focusedGradient: this.focusedGradient,
Expand All @@ -75,11 +74,12 @@ class GradientOutlineInputBorder extends InputBorder {
}

@override
ShapeBorder lerpTo(ShapeBorder b, double t) {
ShapeBorder? lerpTo(ShapeBorder? b, double t) {
if (b is GradientOutlineInputBorder) {
final GradientOutlineInputBorder outline = b;
return GradientOutlineInputBorder(
borderRadius: BorderRadius.lerp(borderRadius, outline.borderRadius, t),
borderRadius:
BorderRadius.lerp(borderRadius, outline.borderRadius, t)!,
borderSide: BorderSide.lerp(borderSide, outline.borderSide, t),
gapPadding: outline.gapPadding,
focusedGradient: this.focusedGradient,
Expand All @@ -89,16 +89,21 @@ class GradientOutlineInputBorder extends InputBorder {
}

@override
Path getInnerPath(Rect rect, {TextDirection textDirection}) {
return Path()..addRRect(borderRadius.resolve(textDirection).toRRect(rect).deflate(borderSide.width));
Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
return Path()
..addRRect(borderRadius
.resolve(textDirection)
.toRRect(rect)
.deflate(borderSide.width));
}

@override
Path getOuterPath(Rect rect, {TextDirection textDirection}) {
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
return Path()..addRRect(borderRadius.resolve(textDirection).toRRect(rect));
}

Path _gapBorderPath(Canvas canvas, RRect center, double start, double extent) {
Path _gapBorderPath(
Canvas canvas, RRect center, double start, double extent) {
final Rect tlCorner = Rect.fromLTWH(
center.left,
center.top,
Expand All @@ -125,7 +130,9 @@ class GradientOutlineInputBorder extends InputBorder {
);

const double cornerArcSweep = math.pi / 2.0;
final double tlCornerArcSweep = start < center.tlRadiusX ? math.asin(start / center.tlRadiusX) : math.pi / 2.0;
final double tlCornerArcSweep = start < center.tlRadiusX
? math.asin(start / center.tlRadiusX)
: math.pi / 2.0;

final Path path = Path()
..addArc(tlCorner, math.pi, tlCornerArcSweep)
Expand Down Expand Up @@ -157,14 +164,13 @@ class GradientOutlineInputBorder extends InputBorder {

@override
void paint(
Canvas canvas,
Rect rect, {
double gapStart,
double gapExtent = 0.0,
double gapPercentage = 0.0,
TextDirection textDirection,
}) {
assert(gapExtent != null);
Canvas canvas,
Rect rect, {
double? gapStart,
double gapExtent = 0.0,
double gapPercentage = 0.0,
TextDirection? textDirection,
}) {
assert(gapPercentage >= 0.0 && gapPercentage <= 1.0);
assert(_cornersAreCircular(borderRadius));

Expand All @@ -173,26 +179,32 @@ class GradientOutlineInputBorder extends InputBorder {

final bool isFocused = borderSide.width == 2.0;

paint.shader =
isFocused ? focusedGradient.createShader(outer.outerRect) : unfocusedGradient.createShader(outer.outerRect);
paint.shader = isFocused
? focusedGradient.createShader(outer.outerRect)
: unfocusedGradient.createShader(outer.outerRect);
final RRect center = outer.deflate(borderSide.width / 2.0);
if (gapStart == null || gapExtent <= 0.0 || gapPercentage == 0.0) {
canvas.drawRRect(center, paint);
} else {
final double extent = lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage);
final double? extent =
lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage);
switch (textDirection) {
case TextDirection.rtl:
{
final Path path = _gapBorderPath(canvas, center, gapStart + gapPadding - extent, extent);
canvas.drawPath(path, paint);
break;
}
final Path path = _gapBorderPath(
canvas, center, gapStart + gapPadding - extent!, extent);
canvas.drawPath(path, paint);
break;

case TextDirection.ltr:
{
final Path path = _gapBorderPath(canvas, center, gapStart - gapPadding, extent);
canvas.drawPath(path, paint);
break;
}
final Path path =
_gapBorderPath(canvas, center, gapStart - gapPadding, extent!);
canvas.drawPath(path, paint);
break;

default:
final Path path = _gapBorderPath(
canvas, center, gapStart + gapPadding - extent!, extent);
canvas.drawPath(path, paint);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: gradient_input_border
description: Gradient outline input border
version: 1.0.0
version: 2.0.0
authors:
- Wojciech Warwas <wojciech.warwas@iteo.com>
- Mariusz Wojtach <mariusz.wojtach@iteo.com>
homepage: https://github.com/iteo/flutter-gradient-input-border

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand Down