【问题标题】:How to make a line through text in Flutter?如何在 Flutter 中通过文本划线?
【发布时间】:2018-06-06 14:30:56
【问题描述】:

在 Flutter 中使用 TextStyle() 类,我如何才能突破旧价格?

【问题讨论】:

    标签: flutter dart text


    【解决方案1】:

    直接将删除线装饰应用于Text 小部件:

    Text('\$8.99', style: TextStyle(decoration: TextDecoration.lineThrough))
    

    您还可以使用RichText 小部件或Text.rich() 构造函数来设置段落的单独跨度。

    基于this example code,显示折扣价:

    RichText()

    new RichText(
      text: new TextSpan(
        text: 'This item costs ',
        children: <TextSpan>[
          new TextSpan(
            text: '\$8.99',
            style: new TextStyle(
              color: Colors.grey,
              decoration: TextDecoration.lineThrough,
            ),
          ),
          new TextSpan(
            text: ' \$3.99',
          ),
        ],
      ),
    )
    

    Text.rich()

    Text.rich(TextSpan(
        text: 'This item costs ',
        children: <TextSpan>[
          new TextSpan(
            text: '\$8.99',
            style: new TextStyle(
              color: Colors.grey,
              decoration: TextDecoration.lineThrough,
            ),
          ),
          new TextSpan(
            text: ' \$3.99',
          ),
        ],
     ),
    )
    

    【讨论】:

    • 这也可以通过其样式属性直接应用于 Text 小部件(请参阅 Tree 的其他答案)。 Text(s, style: TextStyle(decoration: TextDecoration.lineThrough))
    • 只有在提到相关属性之后才应该举例说明。示例有助于新手程序员理解如何使用它,但对于经验丰富的程序员来说,属性只是一个提示,就足够了。还是谢谢!
    • @sud007 我举了一个完整的例子,因为这个问题特别问,“......我怎样才能通过旧价格。” (添加了重点。)我会改进这个答案并提前标注删除线TextDecoration
    • @ChanceSnow 我唯一的重点是支持 SOF 作为解决方案社区,而不是手持编码。但无论如何,这里提供的任何支持都是无私的,需要时间来发布,因此非常感谢,#peace
    • 富文本也可以!
    【解决方案2】:
              style: TextStyle(decoration: TextDecoration.lineThrough),
    

    【讨论】:

      【解决方案3】:

      如果你想用不同的线条颜色,你可以使用它

          Container(
              padding: EdgeInsets.all(20.0),
              child: Stack(
                children: <Widget>[
                  Text(
                    "Lorem Ipsum",
                    style: TextStyle(
                      fontSize: 20,
                    ),
                  ),
                  Container(
                    child: Text(
                      "Lorem Ipsum",
                      style: TextStyle(
                        color: Colors.transparent,
                        decorationColor: Colors.red,
                        decorationStyle: TextDecorationStyle.solid,
                        decoration:
                        TextDecoration.lineThrough,
                        fontSize: 20,
                      ),
                    ),
                  )
                ],
              ))
      

      【讨论】:

        【解决方案4】:

        我用这个

        Column(
            children: [
              Text(
                "sample text"
              ),
              Divider(color: Colors.red,)
            ],
          ),
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。我建议您查看 SO 的 official How to Answer article 以及来自 Jon Skeet 的综合 blog post
        • 为了扩展@AlekseyPotapov 的观点,这对于已确定答案的老问题尤为重要。你能解释一下为什么你的方法可能比几年前@Chance-Snow 接受的答案更可取吗?
        猜你喜欢
        • 2022-11-30
        • 2023-01-22
        • 2021-11-15
        • 2021-10-03
        • 2018-12-10
        • 2014-04-18
        • 2019-12-19
        • 2021-05-11
        • 2023-04-05
        相关资源
        最近更新 更多