【Flutter】一、文本及字体样式

一、Text

用于显示文本。

Text(
	'Hello world',
	style: ...
);

1.1 Text构造方法

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,
    this.textWidthBasis,
  }) : assert(
         data != null,
         'A non-null String must be provided to a Text widget.',
       ),
       textSpan = null,
       super(key: key);

1.2属性说明

1.2.1 String data

要显示的文本信息

1.2.2 TextStyle style

设置文本样式(颜色、字体大小等)
TextStyle属性说明:

属性说明
bool inherit是否继承父节点样式,默认为true
Color color设置字体颜色
例:Colors.green
Color backgroundColor设置背景颜色
double fontSize设置字体大小
FontWeight fontWeight设置字体粗细
FontWeight.normal:默认,与w400一致
FontWeight.bold:加粗,与w700一致
FontWeight.w[100-900]
FontStyle fontStyle设置字体样式:
FontStyle.normal:正常
FontStyle.italic:斜体
double letterSpacing字母间间距
double wordSpacing单词间间距
TextBaseline textBaseline对齐文本的水平线
TextBaseline.alphabetic
TextBaseline.ideographic
double height文本行高
Locale locale根据地区以不同方式呈现
Paint foreground文本颜色,使用该属性时,color需为null
Paint()..color = Colors.red
Paint background文本背景色,使用该属性时,backgroundColor需为null
TextDecoration decorationTextDecoration.none: 默认
TextDecoration.lineThrough: 删除线
TextDecoration.underline: 下划线
TextDecoration.overline: 上划线
Color decorationColor控制decoration中产生线的颜色
TextDecorationStyle decorationStyle控制decoration中产生线的样式
TextDecorationStyle.solid: 实线
TextDecorationStyle.double: 双线
TextDecorationStyle.dotted: 点线
TextDecorationStyle.dashed: 虚线
TextDecorationStyle.wavy: 波浪线
double decorationThickness控制decoration产生线的粗细
String fontFamily设置文字字体
List<Shadow> shadows设置字体阴影
Shadow(color: Colors.black, offset: Offset(1.0, 1.0), blurRadius; 5.0)

1.2.3 StrutStyle strutStyle

/// {@macro flutter.painting.textPainter.strutStyle} 
  final StrutStyle strutStyle;

1.2.4 TextAlign textAlign

设置文本对齐方式。

  1. TextAlign.left: 左对齐
  2. TextAlign.right: 右对齐
  3. TextAlign.center: 居中对齐
  4. TextAlign.start: 文本开始位置对齐,类似left
  5. TextAlign.end: 文本结束为止对齐,类似right
  6. TextAlign.justify: 两端对齐

1.2.5 TextDirection textDirection

设置文本方向。该属性决定了TextAlign.start与TextAlign.end的表现方式。

  1. TextDirection.ltr: 文本方向从左到右,此时TextAlign.start左对齐
  2. TextDirection.rtl: 文本方向从右到左,此时TextAlign.start右对齐

1.2.6 Locale locale

待定…

1.2.7 bool softWrap

是否换行。

1.2.8 TextOverflow overflow

文本溢出时的表现形式。

  1. TextOverflow.ellipsis:文本溢出显示省略号
  2. TextOverflow.clip:文本溢出时直接裁剪掉超出部分,不作任何处理
  3. TextOverflow.fade:溢出文本淡入透明
  4. TextOverflow.visible: 不作处理

1.2.9 double textScaleFactor

字体缩小或放大倍数。

1.2.10 int maxLines

文本最大行数。

1.2.11 String semanticsLabel

待定…

1.2.12 TextWidthBasis textWidthBasis

待定…

1.3 示例代码

Text(
            'Hello world!',
            style: TextStyle(
                color: null,
                backgroundColor: null,
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                fontStyle: FontStyle.italic,
                letterSpacing: 5.0,
                wordSpacing: 6.0,
                textBaseline: TextBaseline.ideographic,
                height: 1.5,
                background: Paint()..color = Colors.blue,
                foreground: Paint()..color = Colors.red,
                decoration: TextDecoration.underline,
                decorationColor: Colors.red,
                decorationStyle: TextDecorationStyle.dashed,
                decorationThickness: 3,
                shadows: [
                  Shadow(
                      color: Colors.black,
                      blurRadius: 10.0,
                      offset: Offset(0.0, 0.0)
                  ),
                ]
            ),
            textAlign: TextAlign.start,
            textDirection: TextDirection.ltr,
            softWrap: true,
            overflow: TextOverflow.visible,
            textScaleFactor: 1.5,
          )

二、TextSpan

使文本不同部分按照不同样式显示。如下:
在这里插入图片描述
源码:

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class TextSpanDemo extends StatelessWidget {
  TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
          Text.rich(TextSpan(children: [
            TextSpan(text: 'Hello ', style: TextStyle(color: Colors.blue)),
            TextSpan(
                text: 'world!',
                style: TextStyle(color: Colors.green, fontSize: 20.0))
          ])),
        ]),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text.rich(TextSpan(children: [
              TextSpan(
                text: 'http://',
              ),
              TextSpan(
                  text: 'www.baidu.com',
                  style: TextStyle(color: Colors.green, fontSize: 20.0),
                  recognizer: tapGestureRecognizer..onTap = (){
                    print('www.baidu.com');
                  }
              )
            ])),
          ],
        )
      ],
    );
  }
}

2.1 TextSpan构造器

const TextSpan({
    this.text,
    this.children,
    TextStyle style,
    this.recognizer,
    this.semanticsLabel,
  }) : super(style: style,);

2.2 TextSpan属性说明

属性说明
String text要显示的文本信息
List<InlineSpan> children添加子节点
TextStyle style同Text的style
GestureRecognizer recognizer手势识别器
String semanticsLabel待定…

三、DefaultTextStyle

指定默认样式,可供子节点去继承,如图:
在这里插入图片描述
第二个Text手动设置inherit为false可取消继承。
源码:

import 'package:flutter/material.dart';

class DefaultTextStyleDemo extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: TextStyle(
        fontSize: 20.0,
        color: Colors.green
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                  'Hello World!'
              ),
              SizedBox(height: 20.0,),
              Text(
                'Hello World!',
                style: TextStyle(
                  inherit: false,
                  color: Colors.black
                ),
              ),
            ],
          ),
        ],
      )
    );
  }
}

3.1 DefaultTextStyle构造器

const DefaultTextStyle({
    Key key,
    @required this.style,
    this.textAlign,
    this.softWrap = true,
    this.overflow = TextOverflow.clip,
    this.maxLines,
    this.textWidthBasis = TextWidthBasis.parent,
    @required Widget child,
  }) : assert(style != null),
       assert(softWrap != null),
       assert(overflow != null),
       assert(maxLines == null || maxLines > 0),
       assert(child != null),
       assert(textWidthBasis != null),
       super(key: key, child: child);

3.2 属性说明

同TextStyle

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MAXLZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值