Flutter 基础Widgets Text()之TextSty

2019-04-24  本文已影响0人  若数
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);

简单使用案例:

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
上一篇 下一篇

猜你喜欢

热点阅读