Horizontal divider with text in the middle in Flutter?

前端 未结 6 1576
不思量自难忘°
不思量自难忘° 2021-02-01 03:13

Is there a built in widget in Flutter to create a divider with text in the middle? Any guide on how to do it? Like this: (the \"OR\" text in the middle of horizontal line)

6条回答
  •  执笔经年
    2021-02-01 04:15

    Best solution is to make a CustomPainter and draw a line

    Follow the steps:

    CustomPainter:

    class Drawhorizontalline extends CustomPainter {
          Paint _paint;
          bool reverse;
    
       Drawhorizontalline(this.reverse) {
         _paint = Paint()
              ..color = PPColors.tertiaryColor
              ..strokeWidth = 1
              ..strokeCap = StrokeCap.round;
       }
    
      @override
      void paint(Canvas canvas, Size size) {
          if (reverse) {
             canvas.drawLine(Offset(-250.0, 0.0), Offset(-10.0, 0.0), _paint);
          } else {
             canvas.drawLine(Offset(10.0, 0.0), Offset(250.0, 0.0), _paint);
          }
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) {
         return false;
      }
    }
    

    Use this CustomPainter

    Widget getSeparateDivider() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        CustomPaint(painter: Drawhorizontalline(true)),
        Text(
          "OR",
          style: TextStyle(
              color: PPColors.primaryColor,
              fontWeight: FontWeight.bold,
              fontSize: PPUIHelper.FontSizeLarge),
        ),
        CustomPaint(painter: Drawhorizontalline(false))
      ],
     );
    }
    

提交回复
热议问题