flutter 跟随text高度变化

跟随 text 高度 ,其他组件高度变化的三种实现方法:

1.,计算是否超过 text 限制的最大行1一行。

return LayoutBuilder(builder: (context, size) {
      final span = TextSpan(text: str, style: style);
      final tp = TextPainter(text: span, maxLines: 1, textDirection: TextDirection.ltr);
      tp.layout(maxWidth: 1.sw - 152.w);
      return tp.didExceedMaxLines ? 36.h : 14.h;
    });

2,自定义 view 包裹 widget 。

/// 竖线
class VerticalLinePainter extends CustomPainter {
  ///线条颜色
  final Color color;

  ///线条宽度
  final double width;

  ///线条左边内边距
  final double paddingLeft;

  ///线条顶部内边距
  final double paddingTop;

  ///线条底部内边距
  final double paddingBottom;

  VerticalLinePainter({
    this.color: Colors.grey,
    this.width: 1,
    this.paddingLeft: 0,
    this.paddingTop: 0,
    this.paddingBottom: 0,
  });

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    paint.style = PaintingStyle.fill;
    paint.color = color;
    Path path = new Path(); //使用轨迹画线条
    path.moveTo(paddingLeft, paddingTop);//左上点
    path.lineTo(paddingLeft, size.height + paddingTop - paddingBottom);//左下点
    path.lineTo(width + paddingLeft, size.height + paddingTop - paddingBottom);//右下点
    path.lineTo(width + paddingLeft, paddingTop);//右上点
    path.close();
    canvas.drawPath(path, paint);
  }

  ///有变化刷新
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
Widget _getItemView(
    LogisticsItem item, {
    bool isFirst: false,
    bool isLast: false,
  }) {
    return Container(
      margin: EdgeInsets.only(top: setWidth(isFirst ? 45 : 0)),
      padding: EdgeInsets.symmetric(horizontal: setWidth(32)),
      constraints: BoxConstraints(minHeight: setWidth(108)),
      child: CustomPaint(
        painter: VerticalLinePainter(
            color: isLast
                ? Colors.transparent
                : XColors.dividerColorDark,//最后一个调整为透明
            width: setWidth(2),//根据UI调整即可
            paddingTop: setWidth(14 + 11 + 5),//根据UI调整即可
            paddingLeft: setWidth(11 / 2 - 2 / 2),//根据UI调整即可
            paddingBottom: setWidth(11 + 10)),//根据UI调整即可
        child: Column(
          //Item
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _getTitleView(item, isFirst),
            SizedBox(height: 200),
          ],
        ),
      ),
    );
  }

3,stack 包裹 根据 text 变化高度。

return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        //灰色右
        Expanded(
          child: Stack(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(left: 37),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
                      height: topSpace==0?4:0,
                    ),
                    firstRow,
                    SizedBox(
                      height: 12.0,
                    ),
                    contentWidget,
                    SizedBox(
                      height: 12.0,
                    ),
                  ],
                ),
              ),
              Positioned(
                left: 0,
                width: 37,
                bottom: 0,
                top: topSpace,
                child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      pointWidget,
                      Expanded(
                        child: Container(
                          width: 27,
                          child: MySeparatorVertical(
                            color: Colors.grey,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ],
    );

还有两个常用工具:

flutter 时间戳判断异常:

if (DateTime.tryParse(item.createTime ?? "") == null) item.createTime = '20200101';

flutter 图片压缩工具:

ImageCompressUtil 图片压缩

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值