flutter 实现文本贴图

关键代码

js 复制代码
var maxWordHeight = 0.0;

    for(var i = 0; i < line.length;i++){
      var char = line.substring(i,i+1);
      final textPainter = TextPainter(
        text: TextSpan(text: char, style: textStyle.apply(
            color: Colors.black
        )),
        textDirection: TextDirection.ltr,
      );
      textPainter.layout();
      textPainter.paint(canvas, Offset(offsetX,0));
      offsetX+= (textPainter.width + wordSpace);
      maxWordHeight = max(maxWordHeight, textPainter.height);
    }

    canvas.drawImageRect(
      labelItem.image!,
      Rect.fromLTWH(0, 0, labelItem.image!.width.toDouble(), labelItem.image!.height.toDouble()),
      Rect.fromLTWH(0, 0, labelItem.width, maxWordHeight),
      Paint()
      ..blendMode = BlendMode.plus,
    );

或者

js 复制代码
   canvas.saveLayer(Rect.fromLTWH(0, 0, labelItem.width, labelItem.height), Paint());
    for(var i = 0; i < line.length;i++){
      var char = line.substring(i,i+1);
      final textPainter = TextPainter(
        text: TextSpan(text: char, style: textStyle.apply(
            color: Colors.black
        )),
        textDirection: TextDirection.ltr,
      );
      textPainter.layout();
      textPainter.paint(canvas, Offset(offsetX,0));
      offsetX+= (textPainter.width + wordSpace);
      maxWordHeight = max(maxWordHeight, textPainter.height);
    }

    canvas.drawImageRect(
      labelItem.image!,
      Rect.fromLTWH(0, 0, labelItem.image!.width.toDouble(), labelItem.image!.height.toDouble()),
      Rect.fromLTWH(0, 0, labelItem.width, maxWordHeight),
      Paint()
      ..blendMode = BlendMode.srcIn,
    );
    canvas.restore();
相关推荐
程序员Ctrl喵11 小时前
异步编程:Event Loop 与 Isolate 的深层博弈
开发语言·flutter
前端不太难13 小时前
Flutter 如何设计可长期维护的模块边界?
flutter
小蜜蜂嗡嗡14 小时前
flutter列表中实现置顶动画
flutter
始持14 小时前
第十二讲 风格与主题统一
前端·flutter
始持14 小时前
第十一讲 界面导航与路由管理
flutter·vibecoding
始持14 小时前
第十三讲 异步操作与异步构建
前端·flutter
新镜15 小时前
【Flutter】 视频视频源横向、竖向问题
flutter
黄林晴15 小时前
Compose Multiplatform 1.10 发布:统一 Preview、Navigation 3、Hot Reload 三箭齐发
android·flutter
Swift社区16 小时前
Flutter 应该按功能拆,还是按技术层拆?
flutter
肠胃炎16 小时前
树形选择器组件封装
前端·flutter