You will need to use a CustomPainter
with a TextPainter
and a blending mode for that.
(您将需要使用带有TextPainter
的CustomPainter
以及混合模式。)
In your widget tree:
(在小部件树中:)
return Container(
color: Colors.black,
height: 40.0,
child: Center(
child: CustomPaint(
painter: MyTextPainter(text: 'Hello'),
),
),
);
Your custom text painter:
(您的自定义文字画家:)
class MyTextPainter extends CustomPainter {
MyTextPainter({this.text});
final String text;
@override
void paint(Canvas canvas, Size size) {
TextPainter textPainter = TextPainter(
text: TextSpan(
text: text,
style: TextStyle(
fontSize: 30.0,
fontWeight: FontWeight.w600,
),
),
textDirection: TextDirection.ltr,
);
textPainter.layout();
// Draw text
final textOffset = size.center(Offset.zero) - textPainter.size.center(Offset.zero);
final textRect = textOffset & textPainter.size;
// Your text box (orange in your example)
final boxRect = RRect.fromRectAndCorners(textRect.inflate(5.0));
final boxPaint = Paint()
..color = Colors.orange
..blendMode = BlendMode.srcOut;
canvas.saveLayer(boxRect.outerRect, Paint());
textPainter.paint(canvas, textOffset);
canvas.drawRRect(boxRect, boxPaint);
canvas.restore();
}
@override
bool shouldRepaint(MyTextPainter oldDelegate) => true;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…