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

问题描述

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

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

我查看了Text Style 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中向文本小部件添加自定义删除线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:36