如何在 Flutter 中为 Text Widget 添加自定义删除线

How to add custom Strikethrough to Text Widget in Flutter

我正在寻找一种方法来向文本小部件添加自定义删除线,如下所示

我查看了文本样式 API,但找不到任何自定义删除线图形的选项。

style: TextStyle(decoration: TextDecoration.lineThrough),

作为一个选项

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: StrikeThroughWidget(
            child: Text('Apple Juice', style: TextStyle(fontSize: 30)),
          ),
        ),
      ),
    );
  }
}

class StrikeThroughWidget extends StatelessWidget {
  final Widget _child;

  StrikeThroughWidget({Key key, @required Widget child})
      : this._child = child,
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _child,
      padding: EdgeInsets.symmetric(horizontal: 8), // this line is optional to make strikethrough effect outside a text
      decoration: BoxDecoration(
        image: DecorationImage(image: AssetImage('graphics/strikethrough.png'), fit: BoxFit.fitWidth),
      ),
    );
  }
}

结果

和带删除线的图片

你可以像下面这样实现

   Container(
        padding: EdgeInsets.all(20.0),
        child: Stack(
          children: <Widget>[
            Text(
              "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s",
              style: TextStyle(
                fontSize: 20,
              ),
            ),
            Container(
              child: Text(
                "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s",
                style: TextStyle(
                  color: Colors.transparent,
                  decorationColor: Colors.red,
                  decorationStyle: TextDecorationStyle.solid,
                  decoration:
                  TextDecoration.lineThrough,
                  fontSize: 20,
                ),
              ),
            )
          ],
        ))