为了账号安全,请及时绑定邮箱和手机立即绑定

Flutter 基础Widgets Text()之TextStyle详解

Text概述

即一个单一样式的文本
Text Widget就是显示单一样式的文本字符串。字符串可能会跨越多行,也可能全部显示在同一行上,具体取决于布局约束。

style参数可选。如果省略了,文本将使用最近的DefaultTextStyle的样式。如果给定样式的TextStyle.inherit属性为true(默认值),则给定样式将与最近的DefaultTextStyle合并。例如,比如可以在使用默认字体系列和大小时使文本变为粗体。

第一个构造函数:

const Text(this.data, {
    Key key,
    this.style,
    this.strutStyle,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
    this.semanticsLabel,
  }) : assert(data != null),
       textSpan = null,
       super(key: key);

1. style及构造函数

style对文本的样式,颜色,字体大小等有更加详尽的定制,如果省略该参数则使用DefaultTextStyle

const TextStyle({
    this.inherit = true,
    this.color,
    this.fontSize,
    this.fontWeight,
    this.fontStyle,
    this.letterSpacing,
    this.wordSpacing,
    this.textBaseline,
    this.height,
    this.locale,
    this.foreground,
    this.background,
    this.shadows,
    this.decoration,
    this.decorationColor,
    this.decorationStyle,
    this.debugLabel,
    String fontFamily,
    List<String> fontFamilyFallback,
    String package,
  }) : fontFamily = package == null ? fontFamily : 'packages/$package/$fontFamily',
       _fontFamilyFallback = fontFamilyFallback,
       _package = package,
       assert(inherit != null),
       assert(color == null || foreground == null, _kColorForegroundWarning);
  • inherit 是否将null值替换为祖先文本样式中的值(例如,在TextSpan树中)。如果为false,则没有显式值的属性将恢复为默认值:白色,字体大小为10像素,采用无衬线字体。
  • color 字体的颜色
  • fontSize 文字大小,单位为像素,如果没有指定大小则默认为14像素,可以乘以textScaleFactor来增加字体大小以便用户更加方便的阅读
  • fontWeight 字体厚度,可以使文本变粗或变细
  • fontStyle 字体变形,有两种 FontStyle.normal(字体直立), FontStyle.italic(字体倾斜)
  • letterSpacing 字母间距,整数拉开字母距离,若是负数则拉近字母距离
  • wordSpacing,单词间距,同上
  • textBaseline 用于对齐文本的水平线
  • height 文本行高,为字体大小的倍数
  • locale 用于选择区域特定符号的区域设置
  • foreground 这个未知
  • background 文本的背景颜色
  • shadows 文本的阴影可以利用列表叠加处理,例如shadows: [Shadow(color:Colors.black,offset: Offset(6, 3), blurRadius: 10)], color即阴影的颜色, offset即阴影相对文本的偏移坐标,blurRadius即阴影的模糊程度,越小越清晰
  • decoration 文字的线性装饰,比如 underline 下划线, lineThrough删除线
  • decorationColor 文本装饰线的颜色
  • decorationStyle 文本装饰线的样式,比如 dashed 虚线
  • debugLabel 这种文本样式的可读描述,此属性仅在调试构建中维护。

简单使用案例:

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
// Text详解
class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter_Wieghts',
      home: Scaffold(
        appBar: AppBar(
        title: Text('Text Learn'),
      ),
      body: Center(
        child: Text('Hello World'*4,
        style: TextStyle(
          inherit: true,
          color: Colors.white,
          fontSize: 30.0,
          fontWeight: FontWeight.w400,
          fontStyle: FontStyle.italic,
          letterSpacing: 5,
          wordSpacing: 20,
          textBaseline: TextBaseline.alphabetic,
          height: 1.2,
          locale: Locale('fr', 'CH'),
          background: Paint() ..color = Colors.blue,
          shadows: [Shadow(color:Colors.black,offset: Offset(6, 3), blurRadius: 10)],
          decoration: TextDecoration.underline,
          decorationColor: Colors.black54,
          decorationStyle: TextDecorationStyle.dashed,
          debugLabel: 'test',
        ),
        ),
       ),
      )
    );
  }
}
 

示例效果:

image.png

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Python工程师
手记
粉丝
23
获赞与收藏
95

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消