Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extends UnderlineInputBorder class in Google Flutter

Tags:

flutter

dart

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());
    }
  }
like image 705
Linh Avatar asked Dec 06 '25 22:12

Linh


1 Answers

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.

like image 91
Günter Zöchbauer Avatar answered Dec 08 '25 14:12

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!