跟随 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 图片压缩