Flutter Text 实现下划线、波浪线、删除线、背景色、渐变文字、阴影、描边、空心字

本文详细介绍了如何使用Flutter的TextStyle属性来实现文字的下划线、删除线、背景色、渐变文字、阴影、描边和空心字效果。通过调整TextDecoration、TextDecorationStyle、shadows等属性,可以创建出具有波浪线、删除线、立体效果、描边和空心文字的艺术文字。同时,文章展示了添加背景色和渐变色文字的方法,帮助开发者丰富UI设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

在这里插入图片描述


1 下划线、 删除线

TextStyle 中有4个属性

  • decoration :TextDecoration可在文字上 中 下方添加直线
  • decorationStyle :extDecorationStyle 可指定线条样式,如直线、虚线、波浪线、双实线、点线
  • decorationThickness :设置线条的宽度
  • decorationColor :设置线条的颜色

下方波浪线

   Text(
    '下划波浪线',
    style: TextStyle(
      fontSize: 24,
      decoration: TextDecoration.underline,
      decorationStyle: TextDecorationStyle.wavy,
      decorationThickness: 1,
    ),
  ),

中间红色删除线

   Text(
    '中间删除线',
    style: TextStyle(
      fontSize: 24,
      decoration: TextDecoration.lineThrough,
      decorationStyle: TextDecorationStyle.solid,
      decorationColor: Colors.red
    ),
  ),

在这里插入图片描述


2 文字背景色 & 渐变色文字

TextStyle 的backgroundColor可为文字添加背景色

   Text(
     '文字添加背景色',
     style: TextStyle(
       fontSize: 24,
       color: Colors.white,
       fontWeight: FontWeight.bold,
       backgroundColor: Colors.red,
     
Flutter中添加带描边渐变文字,你可以使用`Paint`和`Path`类结合`TextPainter`来实现。首先,你需要创建一个自定义的文本装饰器,然后覆盖`paint`方法。下面是一个简单的示例: ```dart import 'package:flutter/material.dart'; class GradientTextWithStroke extends StatelessWidget { final String text; final Color gradientStartColor; final Color gradientEndColor; final double strokeWidth; final Color strokeColor; GradientTextWithStroke({ required this.text, required this.gradientStartColor, required this.gradientEndColor, required this.strokeWidth, required this.strokeColor, }); @override Widget build(BuildContext context) { return Text( text, style: TextStyle( color: Colors.black, // 文字颜色 decoration: InputDecoration( contentPadding: EdgeInsets.all(8), border: OutlineInputBorder( borderSide: BorderSide(width: strokeWidth, color: strokeColor), ), // 使用自定义装饰器 prefixIcon: Icon(Icons.arrow_forward, color: strokeColor), prefixPosition: PrefixPosition.start, suffixIcon: Icon(Icons.arrow_back, color: strokeColor), suffixPosition: PrefixPosition.end, ), // 添加渐变背景 decoration: BoxDecoration( gradient: LinearGradient( colors: [gradientStartColor, gradientEndColor], begin: Alignment.topLeft, end: Alignment.bottomRight, ), borderRadius: BorderRadius.circular(4), // 如果需要圆角 ), ), painter: CustomTextPainter(), ); } } class CustomTextPainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { var path = Path(); TextPainter tp = TextPainter( text: TextSpan(text: 'your text'), style: TextStyle(color: Colors.black), ); tp.layout(); tp.paint(canvas, Rect.fromLTWH(0, 0, size.width, size.height)); path.addRect(Rect.fromLTWH(0, 0, size.width, size.height)); Paint paint = Paint() ..color = strokeColor ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round ..strokeWidth = strokeWidth; canvas.drawPath(path, paint); } @override bool shouldRepaint(CustomPainter oldDelegate) => true; // 更新时总是重新绘制 } ``` 在这个例子中,我们创建了一个名为`GradientTextWithStroke`的Widget,其中包含一个自定义的`CustomTextPainter`,这个画家会在原文字的基础上额外画出一条描边。请注意,你需要替换`path.addRect()`内的矩形大小以适应你的实际文字内容。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值