Flutter 基础Widgets Text()之TextStyle详解

125.jpg

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 {
  @override
  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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,842评论 1 45
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,349评论 0 11
  • CSS基础 本文包括CSS基础知识选择器(重要!!!)继承、特殊性、层叠、重要性CSS格式化排版单位和值盒模型浮动...
    廖少少阅读 3,247评论 0 40
  • CSS参考手册 一、初识CSS3 1.1 CSS是什么 CSS3在CSS2.1的基础上增加了很多强大的新功能。目前...
    没汁帅阅读 3,769评论 1 13
  • 第一章 F12: element.style 内联样式(可以直接在上面写代码进行简单调试) user agent...
    fastwe阅读 1,563评论 0 0