如何在Flutter中向文本小部件添加自定义删除线

时间:2019-05-15 03:18:41

标签: dart flutter flutter-layout

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

enter image description here

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

样式:TextStyle(装饰:TextDecoration.lineThrough),

2 个答案:

答案 0 :(得分:1)

作为选择

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),
      ),
    );
  }
}

结果 enter image description here

和删除线图片 enter image description here

答案 1 :(得分:0)

你可以像下面这样实现

   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,
                ),
              ),
            )
          ],
        ))

enter image description here