Flutter基本组件Text使用

Text是一个文本显示控件,用于在应用程序界面中显示单行或多行文本内容。

Text简单Demo

Dart 复制代码
import 'package:flutter/material.dart';

class MyTextDemo extends StatelessWidget {
  const MyTextDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("My TEXT TEST")),
      extendBody: true,
      body: Column(
        children: [
          const Spacer(),
          Container(
            margin: const EdgeInsets.all(10),
            child: Center(
              child: Text(
                "text one",
                style: TextStyle(
                    fontSize: 28,
                    fontStyle: FontStyle.italic,
                    color: Colors.blue,
                    letterSpacing: 2,
                    wordSpacing: 10,
                    fontFamily: 'Roboto'),
                textAlign: TextAlign.center,
                softWrap: true,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
          const Spacer(),
        ],
      ),
    );
  }
}

重要属性

data

Text的默认构造函数中有一个必传的参数,且必须作为第一个传入的参数,类型是String,就是Text显示的文本。

Dart 复制代码
 const Text(
    String this.data, {
    super.key,
    this.style,
    this.strutStyle,
    this.textAlign,
    this.textDirection,
    this.locale,
    this.softWrap,
    this.overflow,
    @Deprecated(
      'Use textScaler instead. '
      'Use of textScaleFactor was deprecated in preparation for the upcoming nonlinear text scaling support. '
      'This feature was deprecated after v3.12.0-2.0.pre.',
    )
    this.textScaleFactor,
    this.textScaler,
    this.maxLines,
    this.semanticsLabel,
    this.textWidthBasis,
    this.textHeightBehavior,
    this.selectionColor,
  }) 

style

Text默认样式是DefaultTextStyle类型。如果需要修改样式,可以通过style参数传入一个TextStyle类型的值。

Dart 复制代码
 @override
  Widget build(BuildContext context) { 
    ...
    final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
    ...
}
Dart 复制代码
      style: TextStyle(
                    fontSize: 28,
                    fontStyle: FontStyle.italic,
                    color: Colors.blue,
                    letterSpacing: 2,
                    wordSpacing: 10,
                    fontFamily: 'Roboto'),

设置文本颜色

Dart 复制代码
TextStyle(color: Colors.blue)

设置文本背景颜色

Dart 复制代码
style:TextStyle(backgroundColor: Colors.green)

设置文本字体大小

Dart 复制代码
style: TextStyle(fontSize: 100)

设置文本加粗

Dart 复制代码
style: TextStyle(fontWeight: FontWeight.bold)

设置文本为斜体

Dart 复制代码
 style: TextStyle(fontStyle: FontStyle.italic)

设置文本之间的间隙

Dart 复制代码
   style: TextStyle(letterSpacing: 10,)
                   
                   
                    

设置文本内单词间距

Dart 复制代码
 style: TextStyle(wordSpacing: 100)

设置文本行高

Dart 复制代码
  style: TextStyle(height: 10,)
                    

设置文本阴影

Dart 复制代码
style: TextStyle(

	shadows: [
	  Shadow(color: Colors.black, offset: Offset(3, 2))
	])

设置文本内容删除线

Dart 复制代码
   style: TextStyle(
                    decoration: TextDecoration.lineThrough, decorationColor: Colors.yellow,)

设置文本内容下划线

Dart 复制代码
       style: TextStyle(
                    decoration: TextDecoration.underline, decorationColor: Colors.yellow,)

设置文本对齐方式

Dart 复制代码
 textAlign: TextAlign.center

居中

设置文本换行

复制代码
softWrap:true换行;false不换行;
Dart 复制代码
import 'package:flutter/material.dart';

class MyTextDemo extends StatelessWidget {
  const MyTextDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("My TEXT TEST")),
      extendBody: true,
      body: Column(
        children: [
          const Spacer(),
          Container(
            margin: const EdgeInsets.all(10),
            child: Center(
              child: Text(
                "text one texttexttexttexttexttexttexttexttexttexttexttexttext",
                style: TextStyle(
                    fontSize: 50,
                    fontStyle: FontStyle.italic,
                    color: Colors.blue,
                    backgroundColor: Colors.green,
                    letterSpacing: 0,
                    wordSpacing: 100,
                    fontWeight: FontWeight.bold,
                    fontFamily: 'Roboto',
                    decoration: TextDecoration.underline, decorationColor: Colors.yellow,
                    shadows: [
                      Shadow(color: Colors.black, offset: Offset(3, 2))
                    ]),
                textAlign: TextAlign.center,
                maxLines: 3,
                softWrap: true,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
          const Spacer(),
        ],
      ),
    );
  }
}

设置文本溢出

Dart 复制代码
overflow: TextOverflow.ellipsis

设置文本显示最大行数

Dart 复制代码
maxLines: 3

指定文本方向

Dart 复制代码
textDirection: TextDirection.rtl

富文本

Dart 复制代码
import 'package:flutter/material.dart';

class MyTextDemo extends StatelessWidget {
  const MyTextDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("My TEXT TEST")),
      extendBody: true,
      body: Column(
        children: [
          const Spacer(),
          Container(
            margin: const EdgeInsets.all(10),
            child: Center(
                // child: Text(
                //     "text one texttexttexttexttexttexttexttexttexttexttexttexttext",
                //     style: TextStyle(
                //         fontSize: 50,
                //         fontStyle: FontStyle.italic,
                //         color: Colors.blue,
                //         backgroundColor: Colors.green,
                //         letterSpacing: 0,
                //         wordSpacing: 100,
                //         fontWeight: FontWeight.bold,
                //         fontFamily: 'Roboto',
                //         decoration: TextDecoration.underline,
                //         decorationColor: Colors.yellow,
                //         shadows: [
                //           Shadow(color: Colors.black, offset: Offset(3, 2))
                //         ]),
                //     textAlign: TextAlign.center,
                //     maxLines: 3,
                //     softWrap: true,
                //     overflow: TextOverflow.ellipsis,
                //     textDirection: TextDirection.rtl),

                child: Text.rich(TextSpan(children: [
              TextSpan(
                  text: "hhhhhhhhhhhhhhhhhhhhhhhh",
                  style: TextStyle(
                      color: Colors.red,
                      fontSize: 18.0,
                      fontWeight: FontWeight.w700)),
              TextSpan(
                  text: "ooooooooooooooooooo",
                  style: TextStyle(
                      color: Colors.blue,
                      fontSize: 24.0,
                      fontWeight: FontWeight.w700)),
              TextSpan(
                  text: "iiiiiiiiiiiiiiiiiiiiiiiiiii",
                  style: TextStyle(
                      decoration: TextDecoration.lineThrough,
                      color: Colors.yellow,
                      fontSize: 14.0)),
              TextSpan(text: "lllllllllllllllllllllllllll")
            ]))),
          ),
          const Spacer(),
        ],
      ),
    );
  }
}
相关推荐
little_fat_sheep13 天前
【Rive】Android与Rive交互
android·交互·text·rive·inputs
珍珠是蚌的眼泪3 个月前
Elasticsearch基础_2.数据类型
elasticsearch·es·date·text·keyword·动态映射
kidding7234 个月前
前端***
前端·javascript·es6·text·document·for·attribute
沐沐森的故事6 个月前
Unity之自定义Text组件默认属性值
unity·游戏引擎·自定义·组件·text·setter
伊织code6 个月前
Apple - Cocoa Text Architecture Guide
macos·ios·cocoa·文本·text·编辑·印刷
xiangxiongfly9158 个月前
Compose 简单组件
android·image·jetpack·text·textfield·button·compse
-云-9 个月前
任何样式,javascript都可以操作,让你所向披靡
javascript·css3·web api·style
Strong_TAN9 个月前
HarmonyOS | UI开发 (一) | 基础组件(Text/Span,TextInput/TextArea,Button,Image)
textinput·harmonyos·image·text·button
俊刚、1 年前
【Vue】class与style绑定
前端·javascript·vue.js·style