-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Expand file tree
/
Copy pathinput_border.dart
More file actions
809 lines (722 loc) · 27.9 KB
/
input_border.dart
File metadata and controls
809 lines (722 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'input_decorator.dart';
library;
import 'dart:math' as math;
import 'dart:ui' show lerpDouble;
import 'package:flutter/foundation.dart' show clampDouble;
import 'package:flutter/widgets.dart';
/// Defines the appearance of an [InputDecorator]'s border.
///
/// An input decorator's border is specified by [InputDecoration.border].
///
/// The border is drawn relative to the input decorator's "container" which
/// is the optionally filled area above the decorator's helper, error,
/// and counter.
///
/// Input border's are decorated with a line whose weight and color are defined
/// by [borderSide]. The input decorator's renderer animates the input border's
/// appearance in response to state changes, like gaining or losing the focus,
/// by creating new copies of its input border with [copyWith].
///
/// See also:
///
/// * [UnderlineInputBorder], the default [InputDecorator] border which
/// draws a horizontal line at the bottom of the input decorator's container.
/// * [OutlineInputBorder], an [InputDecorator] border which draws a
/// rounded rectangle around the input decorator's container.
/// * [InputDecoration], which is used to configure an [InputDecorator].
abstract class InputBorder extends ShapeBorder {
/// Creates a border for an [InputDecorator].
///
/// Applications typically do not specify a [borderSide] parameter because the
/// [InputDecorator] substitutes its own, using [copyWith], based on the
/// current theme and [InputDecorator.isFocused].
const InputBorder({this.borderSide = BorderSide.none});
/// No input border.
///
/// Use this value with [InputDecoration.border] to specify that no border
/// should be drawn. The [InputDecoration.collapsed] constructor sets
/// its border to this value.
static const InputBorder none = _NoInputBorder();
/// Defines the border line's color and weight.
///
/// The [InputDecorator] creates copies of its input border, using [copyWith],
/// based on the current theme and [InputDecorator.isFocused].
final BorderSide borderSide;
/// Creates a copy of this input border with the specified `borderSide`.
InputBorder copyWith({BorderSide? borderSide});
/// True if this border will enclose the [InputDecorator]'s container.
///
/// This property affects the alignment of container's contents. For example
/// when an input decorator is configured with an [OutlineInputBorder] its
/// label is centered with its container.
bool get isOutline;
/// Paint this input border on [canvas].
///
/// The [rect] parameter bounds the [InputDecorator]'s container.
///
/// The additional `gap` parameters reflect the state of the [InputDecorator]'s
/// floating label. When an input decorator gains the focus, its label
/// animates upwards, to make room for the input child. The [gapStart] and
/// [gapExtent] parameters define a floating label width interval, and
/// [gapPercentage] defines the animation's progress (0.0 to 1.0).
@override
void paint(
Canvas canvas,
Rect rect, {
double? gapStart,
double gapExtent = 0.0,
double gapPercentage = 0.0,
TextDirection? textDirection,
});
}
// Used to create the InputBorder.none singleton.
class _NoInputBorder extends InputBorder {
const _NoInputBorder() : super(borderSide: BorderSide.none);
@override
_NoInputBorder copyWith({BorderSide? borderSide}) => const _NoInputBorder();
@override
bool get isOutline => false;
@override
EdgeInsetsGeometry get dimensions => EdgeInsets.zero;
@override
_NoInputBorder scale(double t) => const _NoInputBorder();
@override
Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
return Path()..addRect(rect);
}
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
return Path()..addRect(rect);
}
@override
void paintInterior(Canvas canvas, Rect rect, Paint paint, {TextDirection? textDirection}) {
canvas.drawRect(rect, paint);
}
@override
bool get preferPaintInterior => true;
@override
void paint(
Canvas canvas,
Rect rect, {
double? gapStart,
double gapExtent = 0.0,
double gapPercentage = 0.0,
TextDirection? textDirection,
}) {
// Do not paint.
}
}
/// Draws a horizontal line at the bottom of an [InputDecorator]'s container and
/// defines the container's shape.
///
/// The input decorator's "container" is the optionally filled area above the
/// decorator's helper, error, and counter.
///
/// See also:
///
/// * [OutlineInputBorder], an [InputDecorator] border which draws a
/// rounded rectangle around the input decorator's container.
/// * [InputDecoration], which is used to configure an [InputDecorator].
class UnderlineInputBorder extends InputBorder {
/// Creates an underline border for an [InputDecorator].
///
/// The [borderSide] parameter defaults to [BorderSide.none] (it must not be
/// null). Applications typically do not specify a [borderSide] parameter
/// because the input decorator substitutes its own, using [copyWith], based
/// on the current theme and [InputDecorator.isFocused].
///
/// The [borderRadius] parameter defaults to a value where the top left
/// and right corners have a circular radius of 4.0.
const UnderlineInputBorder({
super.borderSide = const BorderSide(),
this.borderRadius = const BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
});
/// The radii of the border's rounded rectangle corners.
///
/// When this border is used with a filled input decorator, see
/// [InputDecoration.filled], the border radius defines the shape
/// of the background fill as well as the bottom left and right
/// edges of the underline itself.
///
/// By default the top right and top left corners have a circular radius
/// of 4.0.
final BorderRadius borderRadius;
@override
bool get isOutline => false;
@override
UnderlineInputBorder copyWith({BorderSide? borderSide, BorderRadius? borderRadius}) {
return UnderlineInputBorder(
borderSide: borderSide ?? this.borderSide,
borderRadius: borderRadius ?? this.borderRadius,
);
}
@override
EdgeInsetsGeometry get dimensions {
return EdgeInsets.only(bottom: borderSide.width);
}
@override
UnderlineInputBorder scale(double t) {
return UnderlineInputBorder(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
void paintInterior(Canvas canvas, Rect rect, Paint paint, {TextDirection? textDirection}) {
canvas.drawRRect(borderRadius.resolve(textDirection).toRRect(rect), paint);
}
@override
bool get preferPaintInterior => true;
@override
ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
if (a is UnderlineInputBorder) {
return UnderlineInputBorder(
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 UnderlineInputBorder) {
return UnderlineInputBorder(
borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
borderRadius: BorderRadius.lerp(borderRadius, b.borderRadius, t)!,
);
}
return super.lerpTo(b, t);
}
/// Draw a horizontal line at the bottom of [rect].
///
/// The [borderSide] defines the line's color and weight. The `textDirection`
/// `gap` and `textDirection` parameters are ignored.
@override
void paint(
Canvas canvas,
Rect rect, {
double? gapStart,
double gapExtent = 0.0,
double gapPercentage = 0.0,
TextDirection? textDirection,
}) {
if (borderSide.style == BorderStyle.none) {
return;
}
if (borderRadius.bottomLeft != Radius.zero || borderRadius.bottomRight != Radius.zero) {
// This prevents the border from leaking the color due to anti-aliasing rounding errors.
final updatedBorderRadius = BorderRadius.only(
bottomLeft: borderRadius.bottomLeft.clamp(maximum: Radius.circular(rect.height / 2)),
bottomRight: borderRadius.bottomRight.clamp(maximum: Radius.circular(rect.height / 2)),
);
BoxBorder.paintNonUniformBorder(
canvas,
rect,
textDirection: textDirection,
borderRadius: updatedBorderRadius,
bottom: borderSide.copyWith(strokeAlign: BorderSide.strokeAlignInside),
color: borderSide.color,
);
} else {
final alignInsideOffset = Offset(0, borderSide.width / 2);
canvas.drawLine(
rect.bottomLeft - alignInsideOffset,
rect.bottomRight - alignInsideOffset,
borderSide.toPaint(),
);
}
}
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is UnderlineInputBorder &&
other.borderSide == borderSide &&
other.borderRadius == borderRadius;
}
@override
int get hashCode => Object.hash(borderSide, borderRadius);
}
/// Draws a rounded rectangle around an [InputDecorator]'s container.
///
/// When the input decorator's label is floating, for example because its
/// input child has the focus, the label appears in a gap in the border outline.
///
/// The input decorator's "container" is the optionally filled area above the
/// decorator's helper, error, and counter.
///
/// See also:
///
/// * [UnderlineInputBorder], the default [InputDecorator] border which
/// draws a horizontal line at the bottom of the input decorator's container.
/// * [ShapedInputBorder], an [InputDecorator] border which draws a custom
/// [ShapeBorder] around the input decorator's container.
/// * [InputDecoration], which is used to configure an [InputDecorator].
class OutlineInputBorder extends InputBorder {
/// Creates a rounded rectangle outline border for an [InputDecorator].
///
/// If the [borderSide] parameter is [BorderSide.none], it will not draw a
/// border. However, it will still define a shape (which you can see if
/// [InputDecoration.filled] is true).
///
/// If an application does not specify a [borderSide] parameter of
/// value [BorderSide.none], the input decorator substitutes its own, using
/// [copyWith], based on the current theme and [InputDecorator.isFocused].
///
/// The [borderRadius] parameter defaults to a value where all four corners
/// have a circular radius of 4.0. The corner radii must be circular, i.e.
/// their [Radius.x] and [Radius.y] values must be the same.
///
/// See also:
///
/// * [InputDecoration.floatingLabelBehavior], which should be set to
/// [FloatingLabelBehavior.never] when the [borderSide] is
/// [BorderSide.none]. If left as [FloatingLabelBehavior.auto], the label
/// will extend beyond the container as if the border were still being
/// drawn.
const OutlineInputBorder({
super.borderSide = const BorderSide(),
this.borderRadius = const BorderRadius.all(Radius.circular(4.0)),
this.gapPadding = 4.0,
}) : assert(gapPadding >= 0.0);
// The label text's gap can extend into the corners (even both the top left
// and the top right corner). To avoid the more complicated problem of finding
// how far the gap penetrates into an elliptical corner, just require them
// to be circular.
//
// This can't be checked by the constructor because const constructor.
static bool _cornersAreCircular(BorderRadius borderRadius) {
return borderRadius.topLeft.x == borderRadius.topLeft.y &&
borderRadius.bottomLeft.x == borderRadius.bottomLeft.y &&
borderRadius.topRight.x == borderRadius.topRight.y &&
borderRadius.bottomRight.x == borderRadius.bottomRight.y;
}
/// Horizontal padding on either side of the border's
/// [InputDecoration.labelText] width gap.
///
/// This value is used by the [paint] method to compute the actual gap width.
final double gapPadding;
/// The radii of the border's rounded rectangle corners.
///
/// The corner radii must be circular, i.e. their [Radius.x] and [Radius.y]
/// values must be the same.
final BorderRadius borderRadius;
@override
bool get isOutline => true;
@override
OutlineInputBorder copyWith({
BorderSide? borderSide,
BorderRadius? borderRadius,
double? gapPadding,
}) {
return OutlineInputBorder(
borderSide: borderSide ?? this.borderSide,
borderRadius: borderRadius ?? this.borderRadius,
gapPadding: gapPadding ?? this.gapPadding,
);
}
@override
EdgeInsetsGeometry get dimensions {
return EdgeInsets.all(borderSide.strokeInset);
}
@override
OutlineInputBorder scale(double t) {
return OutlineInputBorder(
borderSide: borderSide.scale(t),
borderRadius: borderRadius * t,
gapPadding: gapPadding * t,
);
}
@override
ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
if (a is OutlineInputBorder) {
final OutlineInputBorder outline = a;
return OutlineInputBorder(
borderRadius: BorderRadius.lerp(outline.borderRadius, borderRadius, t)!,
borderSide: BorderSide.lerp(outline.borderSide, borderSide, t),
gapPadding: outline.gapPadding,
);
}
return super.lerpFrom(a, t);
}
@override
ShapeBorder? lerpTo(ShapeBorder? b, double t) {
if (b is OutlineInputBorder) {
final OutlineInputBorder outline = b;
return OutlineInputBorder(
borderRadius: BorderRadius.lerp(borderRadius, outline.borderRadius, t)!,
borderSide: BorderSide.lerp(borderSide, outline.borderSide, t),
gapPadding: outline.gapPadding,
);
}
return super.lerpTo(b, t);
}
@override
Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
return Path()
..addRRect(borderRadius.resolve(textDirection).toRRect(rect).deflate(borderSide.strokeInset));
}
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
return Path()..addRRect(borderRadius.resolve(textDirection).toRRect(rect));
}
@override
void paintInterior(Canvas canvas, Rect rect, Paint paint, {TextDirection? textDirection}) {
canvas.drawRRect(borderRadius.resolve(textDirection).toRRect(rect), paint);
}
@override
bool get preferPaintInterior => true;
Path _gapBorderPath(Canvas canvas, RRect center, double outerWidth, double start, double extent) {
// When the corner radii on any side add up to be greater than the
// given height, each radius has to be scaled to not exceed the
// size of the width/height of the RRect.
final RRect scaledRRect = center.scaleRadii();
final tlCorner = Rect.fromLTWH(
scaledRRect.left,
scaledRRect.top,
scaledRRect.tlRadiusX * 2.0,
scaledRRect.tlRadiusY * 2.0,
);
final trCorner = Rect.fromLTWH(
scaledRRect.right - scaledRRect.trRadiusX * 2.0,
scaledRRect.top,
scaledRRect.trRadiusX * 2.0,
scaledRRect.trRadiusY * 2.0,
);
final brCorner = Rect.fromLTWH(
scaledRRect.right - scaledRRect.brRadiusX * 2.0,
scaledRRect.bottom - scaledRRect.brRadiusY * 2.0,
scaledRRect.brRadiusX * 2.0,
scaledRRect.brRadiusY * 2.0,
);
final blCorner = Rect.fromLTWH(
scaledRRect.left,
scaledRRect.bottom - scaledRRect.blRadiusY * 2.0,
scaledRRect.blRadiusX * 2.0,
scaledRRect.blRadiusY * 2.0,
);
// This assumes that the radius is circular (x and y radius are equal).
// Currently, BorderRadius only supports circular radii.
const double cornerArcSweep = math.pi / 2.0;
final path = Path();
// Top left corner
if (scaledRRect.tlRadius != Radius.zero) {
final double tlCornerArcSweep = math.acos(
clampDouble(1 - start / scaledRRect.tlRadiusX, 0.0, 1.0),
);
path.addArc(tlCorner, math.pi, tlCornerArcSweep);
} else {
// Because the path is painted with Paint.strokeCap = StrokeCap.butt, horizontal coordinate is moved
// based on strokeOffset to respect strokeAlign.
path.moveTo(scaledRRect.left + borderSide.strokeOffset / 2, scaledRRect.top);
}
// Draw top border from top left corner to gap start.
if (start > scaledRRect.tlRadiusX) {
path.lineTo(start, scaledRRect.top);
}
// Draw top border from gap end to top right corner and draw top right corner.
const double trCornerArcStart = (3 * math.pi) / 2.0;
const trCornerArcSweep = cornerArcSweep;
if (start + extent < outerWidth - scaledRRect.trRadiusX) {
path.moveTo(start + extent, scaledRRect.top);
path.lineTo(scaledRRect.right - scaledRRect.trRadiusX, scaledRRect.top);
if (scaledRRect.trRadius != Radius.zero) {
path.addArc(trCorner, trCornerArcStart, trCornerArcSweep);
}
} else if (start + extent < outerWidth) {
final double dx = outerWidth - (start + extent);
final double sweep = math.asin(clampDouble(1 - dx / scaledRRect.trRadiusX, 0.0, 1.0));
path.addArc(trCorner, trCornerArcStart + sweep, trCornerArcSweep - sweep);
}
// Draw right border and bottom right corner.
if (scaledRRect.brRadius != Radius.zero) {
path.moveTo(scaledRRect.right, scaledRRect.top + scaledRRect.trRadiusY);
}
path.lineTo(scaledRRect.right, scaledRRect.bottom - scaledRRect.brRadiusY);
if (scaledRRect.brRadius != Radius.zero) {
path.addArc(brCorner, 0.0, cornerArcSweep);
}
// Draw bottom border and bottom left corner.
path.lineTo(scaledRRect.left + scaledRRect.blRadiusX, scaledRRect.bottom);
if (scaledRRect.blRadius != Radius.zero) {
path.addArc(blCorner, math.pi / 2.0, cornerArcSweep);
}
// Draw left border
path.lineTo(scaledRRect.left, scaledRRect.top + scaledRRect.tlRadiusY);
return path;
}
/// Draw a rounded rectangle around [rect] using [borderRadius].
///
/// The [borderSide] defines the line's color and weight.
///
/// The top side of the rounded rectangle may be interrupted by a single gap
/// if [gapExtent] is non-null. In that case the gap begins at
/// `gapStart - gapPadding` (assuming that the [textDirection] is [TextDirection.ltr]).
/// The gap's width is `(gapPadding + gapExtent + gapPadding) * gapPercentage`.
@override
void paint(
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));
final Paint paint = borderSide.toPaint();
final RRect outer = borderRadius.toRRect(rect);
final RRect center = outer.inflate(borderSide.strokeOffset / 2);
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 start = switch (textDirection!) {
TextDirection.rtl => gapStart + gapPadding - extent,
TextDirection.ltr => gapStart - gapPadding,
};
final Path path = _gapBorderPath(canvas, center, outer.width, math.max(0.0, start), extent);
canvas.drawPath(path, paint);
}
}
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is OutlineInputBorder &&
other.borderSide == borderSide &&
other.borderRadius == borderRadius &&
other.gapPadding == gapPadding;
}
@override
int get hashCode => Object.hash(borderSide, borderRadius, gapPadding);
}
/// Draws a custom shape around an [InputDecorator]'s container.
///
/// This border allows any [ShapeBorder] to be used as an input decorator border.
/// This provides maximum flexibility for custom border shapes while maintaining
/// the gap functionality for floating labels.
///
/// When the input decorator's label is floating, for example because its
/// input child has the focus, the label appears in a gap in the border outline.
///
/// The input decorator's "container" is the optionally filled area above the
/// decorator's helper, error, and counter.
///
/// {@tool dartpad}
/// This sample shows how to use [ShapedInputBorder] with different
/// [ShapeBorder] implementations.
///
/// ** See code in examples/api/lib/material/shaped_input_border/shaped_input_border.0.dart **
/// {@end-tool}
///
/// See also:
///
/// * [OutlineInputBorder], a traditional rounded rectangle border.
/// * [UnderlineInputBorder], the default [InputDecorator] border which
/// draws a horizontal line at the bottom of the input decorator's container.
/// * [RoundedSuperellipseBorder], which can be used with this border for iOS-style shapes.
/// * [InputDecoration], which is used to configure an [InputDecorator].
class ShapedInputBorder extends InputBorder {
/// Creates a shaped outline border for an [InputDecorator].
///
/// The [shape] parameter defines the custom border shape. It can be any
/// [ShapeBorder] such as [RoundedSuperellipseBorder], [StadiumBorder],
/// [BeveledRectangleBorder], or a custom shape.
///
/// If the [borderSide] parameter is [BorderSide.none], it will not draw a
/// border. However, it will still define a shape (which you can see if
/// [InputDecoration.filled] is true).
///
/// If an application does not specify a [borderSide] parameter of
/// value [BorderSide.none], the input decorator substitutes its own, using
/// [copyWith], based on the current theme and [InputDecorator.isFocused].
///
/// See also:
///
/// * [InputDecoration.floatingLabelBehavior], which should be set to
/// [FloatingLabelBehavior.never] when the [borderSide] is
/// [BorderSide.none]. If left as [FloatingLabelBehavior.auto], the label
/// will extend beyond the container as if the border were still being
/// drawn.
const ShapedInputBorder({
super.borderSide = const BorderSide(),
required this.shape,
this.gapPadding = 4.0,
}) : assert(gapPadding >= 0.0);
/// Horizontal padding on either side of the border's
/// [InputDecoration.labelText] width gap.
///
/// This value is used by the [paint] method to compute the actual gap width.
final double gapPadding;
/// The shape of the border.
final ShapeBorder shape;
@override
bool get isOutline => true;
@override
ShapedInputBorder copyWith({BorderSide? borderSide, ShapeBorder? shape, double? gapPadding}) {
return ShapedInputBorder(
borderSide: borderSide ?? this.borderSide,
shape: shape ?? this.shape,
gapPadding: gapPadding ?? this.gapPadding,
);
}
@override
EdgeInsetsGeometry get dimensions {
return EdgeInsets.all(borderSide.width);
}
@override
ShapedInputBorder scale(double t) {
return ShapedInputBorder(
borderSide: borderSide.scale(t),
shape: shape.scale(t),
gapPadding: gapPadding * t,
);
}
@override
ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
if (a is ShapedInputBorder) {
return ShapedInputBorder(
borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
shape: ShapeBorder.lerp(a.shape, shape, t)!,
gapPadding: a.gapPadding,
);
}
return super.lerpFrom(a, t);
}
@override
ShapeBorder? lerpTo(ShapeBorder? b, double t) {
if (b is ShapedInputBorder) {
return ShapedInputBorder(
borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
shape: ShapeBorder.lerp(shape, b.shape, t)!,
gapPadding: b.gapPadding,
);
}
return super.lerpTo(b, t);
}
@override
Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
return shape.getInnerPath(rect.deflate(borderSide.width), textDirection: textDirection);
}
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
return shape.getOuterPath(rect, textDirection: textDirection);
}
@override
void paintInterior(Canvas canvas, Rect rect, Paint paint, {TextDirection? textDirection}) {
if (shape.preferPaintInterior) {
shape.paintInterior(canvas, rect, paint, textDirection: textDirection);
} else {
// Fallback for shapes that don't support paintInterior.
canvas.drawPath(shape.getOuterPath(rect, textDirection: textDirection), paint);
}
}
@override
bool get preferPaintInterior => shape.preferPaintInterior;
Path _gapBorderPath(Rect rect, double start, double extent, {TextDirection? textDirection}) {
// Create a continuous path for the border with a gap in the top edge.
final Path outerPath = shape.getOuterPath(rect, textDirection: textDirection);
// If there's no meaningful gap, return the full outline.
if (start <= 0 && extent <= 0) {
return outerPath;
}
// Create a rectangle that represents the gap area.
// The gap is on the top edge, so we create a rect that covers the gap region.
final gapLeft = start;
final double gapRight = start + extent;
// Create a path that excludes the gap area by combining with a difference operation.
// We'll subtract a small rectangle at the top where the gap should be.
final gapRect = Path()
..addRect(
Rect.fromLTRB(
clampDouble(gapLeft, rect.left, rect.right),
rect.top - 1.0, // Extend slightly beyond to ensure clean cut.
clampDouble(gapRight, rect.left, rect.right),
rect.top + 1.0, // Small height to only affect top edge.
),
);
return Path.combine(PathOperation.difference, outerPath, gapRect);
}
/// Draw the custom shape around [rect].
///
/// The [borderSide] defines the line's color and weight.
///
/// The top side of the border may be interrupted by a single gap
/// if [gapExtent] is non-null. In that case the gap begins at
/// `gapStart - gapPadding` (assuming that the [textDirection] is [TextDirection.ltr]).
/// The gap's width is `(gapPadding + gapExtent + gapPadding) * gapPercentage`.
@override
void paint(
Canvas canvas,
Rect rect, {
double? gapStart,
double gapExtent = 0.0,
double gapPercentage = 0.0,
TextDirection? textDirection,
}) {
assert(gapPercentage >= 0.0 && gapPercentage <= 1.0);
final Paint paint = borderSide.toPaint();
final Rect deflatedRect = rect.deflate(borderSide.width / 2.0);
if (gapStart == null || gapExtent <= 0.0 || gapPercentage == 0.0) {
// Draw the shape without a gap.
if (shape is OutlinedBorder) {
final outlinedShape = shape as OutlinedBorder;
// Create a copy with our border side.
final OutlinedBorder shapedBorder = outlinedShape.copyWith(side: borderSide);
shapedBorder.paint(canvas, deflatedRect, textDirection: textDirection);
} else {
canvas.drawPath(shape.getOuterPath(deflatedRect, textDirection: textDirection), paint);
}
} else {
final double extent = lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage)!;
final double start = switch (textDirection!) {
TextDirection.rtl => gapStart + gapPadding - extent,
TextDirection.ltr => gapStart - gapPadding,
};
final Path path = _gapBorderPath(
deflatedRect,
math.max(0.0, start),
extent,
textDirection: textDirection,
);
canvas.drawPath(path, paint);
}
}
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is ShapedInputBorder &&
other.borderSide == borderSide &&
other.shape == shape &&
other.gapPadding == gapPadding;
}
@override
int get hashCode => Object.hash(borderSide, shape, gapPadding);
}