I'm trying to modify the behavior of the UnderlineInputBorder in Flutter by extending it. But the Flutter always called the draw() method of the superclass (UnderlineInputBorder) instead of my _PremiseInputBorder as bellow.
Code for the TextField widget:
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.zero,
border: _PremiseInputBorder()),
)
Code for my custom border class:
class _PremiseInputBorder extends UnderlineInputBorder {
const _PremiseInputBorder() : super();
@override
void paint(Canvas canvas, Rect rect, {
double gapStart,
double gapExtent = 0.0,
double gapPercentage = 0.0,
TextDirection textDirection,}) {
if (borderRadius.bottomLeft != Radius.zero ||
borderRadius.bottomRight != Radius.zero)
canvas.clipPath(getOuterPath(rect, textDirection: textDirection));
Offset leftRect = Offset(rect.left, rect.bottom - 5.0);
Offset rightRect = Offset(rect.right, rect.bottom - 5.0);
canvas.drawLine(leftRect, rightRect, borderSide.toPaint());
}
}
UnderlineInputBorder has
@override
UnderlineInputBorder copyWith({ BorderSide borderSide, BorderRadius borderRadius }) {
return UnderlineInputBorder(
borderSide: borderSide ?? this.borderSide,
borderRadius: borderRadius ?? this.borderRadius,
);
}
which returns an UnderlineInputBorder even when you extend the class.
If you add to _PremiseInputBorder
@override
UnderlineInputBorder copyWith(
{BorderSide borderSide, BorderRadius borderRadius}) {
return _PremiseInputBorder();
}
it will call your paint() method.
There are other methods that do similar things like scale(), lerpFrom(), lerpTo() but they were not called in your simple example.
You need to override these as well to make it all scenarios.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With