zoukankan      html  css  js  c++  java
  • [Flutter] 带图标的Text

    先看下效果图(框起来的地方)

    水平方向:

    垂直方向效果:

     

    我们经常需要这样的效果,文字前面或上方加上图标。

    import 'package:flutter/material.dart';
    
    /// icon text
    class IconText extends StatelessWidget {
      final String text;
      final Icon icon;
      final double iconSize;
      final Axis direction;
      /// icon padding
      final EdgeInsetsGeometry padding;
      final TextStyle style;
      final int maxLines;
      final bool softWrap;
      final TextOverflow overflow;
      final TextAlign textAlign;
    
      const IconText(this.text,
          {Key key,
          this.icon,
          this.iconSize,
          this.direction = Axis.horizontal,
          this.style,
          this.maxLines,
          this.softWrap,
          this.padding,
          this.textAlign,
          this.overflow = TextOverflow.ellipsis})
          : assert(direction != null),
            assert(overflow != null),
            super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return icon == null
            ? Text(text ?? '', style: style)
            : text == null || text.isEmpty
                ? (padding == null ? icon : Padding(padding: padding, child: icon))
                : RichText(
                    text: TextSpan(style: style, children: [
                      WidgetSpan(
                          child: IconTheme(
                        data: IconThemeData(
                            size: iconSize ??
                                (style == null || style.fontSize == null
                                    ? 16
                                    : style.fontSize + 1),
                            color: style == null ? null : style.color),
                        child: padding == null
                            ? icon
                            : Padding(
                                padding: padding,
                                child: icon,
                              ),
                      )),
                      TextSpan(
                          text: direction == Axis.horizontal ? text : "
    $text"),
                    ]),
                    maxLines: maxLines,
                    softWrap: softWrap ?? true,
                    overflow: overflow ?? TextOverflow.clip,
                    textAlign: textAlign ?? (direction == Axis.horizontal ? TextAlign.start : TextAlign.center),
                  );
      }
    }

    用方和 Text 类似,只是加了些属性。

  • 相关阅读:
    python_16(bootstrap)
    python_15(jquery)
    python_14(js)
    .net 定义泛型方法解析XML数据赋值给相应对象
    SQL Server 数字字符串位数不够补0
    SQL Server 跨服务器查询
    JQ1.5 为动态追加的元素添加事件
    radio group 的change 事件
    记录兼职工作中遇到的问题-IIS 服务器站点无法启动
    记录第一份工作的最后一次需求-百分比环形进度条
  • 原文地址:https://www.cnblogs.com/yangyxd/p/13223134.html
Copyright © 2011-2022 走看看