如何在 Flutter 中为 Text Widget 添加自定义删除线 [英] How to add custom Strikethrough to Text Widget in Flutter

查看:13
本文介绍了如何在 Flutter 中为 Text Widget 添加自定义删除线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将自定义删除线添加到如下文本小部件的方法

I am looking for a way to add a custom strikethrough to a text widget like below

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

I looked at the Text Style API, but couldn't find any option to customize the Strikethrough graphic.

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

结果

和删除线图像

这篇关于如何在 Flutter 中为 Text Widget 添加自定义删除线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆