当前位置: 首页 > article >正文

Flutter基本组件Text使用

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

Text简单Demo

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显示的文本。

 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类型的值。

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

设置文本颜色

TextStyle(color: Colors.blue)

 

设置文本背景颜色

style:TextStyle(backgroundColor: Colors.green)

设置文本字体大小

style: TextStyle(fontSize: 100)

设置文本加粗

style: TextStyle(fontWeight: FontWeight.bold)

 

设置文本为斜体

 style: TextStyle(fontStyle: FontStyle.italic)

 

设置文本之间的间隙

   style: TextStyle(letterSpacing: 10,)
                   
                   
                    

设置文本内单词间距

 style: TextStyle(wordSpacing: 100)

设置文本行高

  style: TextStyle(height: 10,)
                    

设置文本阴影

style: TextStyle(

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

设置文本内容删除线

   style: TextStyle(
                    decoration: TextDecoration.lineThrough, decorationColor: Colors.yellow,)

设置文本内容下划线

       style: TextStyle(
                    decoration: TextDecoration.underline, decorationColor: Colors.yellow,)

设置文本对齐方式

 textAlign: TextAlign.center

居中

设置文本换行

softWrap:true换行;false不换行;
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(),
        ],
      ),
    );
  }
}

设置文本溢出

overflow: TextOverflow.ellipsis

设置文本显示最大行数

maxLines: 3

指定文本方向

textDirection: TextDirection.rtl

富文本

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(),
        ],
      ),
    );
  }
}

 


http://www.kler.cn/news/294954.html

相关文章:

  • 嵌入式面试刷题
  • 商城系统的数据库
  • 电脑录屏杂音太大怎么办 电脑录屏杂音去除办法有哪些 解决录屏电流声等问题技巧与工具推荐
  • 如何选择国内大带宽服务器租用?
  • ❤《实战纪录片 1 》原生开发小程序中遇到的问题和解决方案
  • 一个vue重新回顾,好多年前写的
  • 常见的排序
  • Leetcode 152. 乘积最大子数组(Medium)
  • 通信工程学习:什么是ARQ自动重传请求
  • 【计算机视觉】语义分割输入图像尺寸
  • 快速傅里叶变换(FFT)及其在多项式乘法中的应用 —— 深入分析与 Python 实现
  • android AccessibilityService合法合规采集大众点评app商店商品详情(2024-09-02)
  • 【Qt笔记】QListWidget控件详解
  • 经济管理专业数据库介绍
  • 算法学习:模拟
  • Unity Adressables 使用说明(三)构建内容(Build Content)
  • 85、 探针
  • Java基础 1. Java开发环境搭建
  • 数据处理与数据填充在Pandas中的应用
  • 基于 RocketMQ 的云原生 MQTT 消息引擎设计
  • 智能体叙事实验:MixlabNodes新增Her页面
  • Android --- observer和observerForever的区别
  • Ansible自动化运维入门:从基础到实践的全面指南
  • 福建聚鼎科技:开一家装饰画店铺需要投资多少钱
  • Java|Java 中 JSONPath 的使用
  • history增加时间显示
  • PostgreSQL的repmgr工具介绍
  • centos基本命令
  • 店匠科技携手Stripe共谋电商支付新篇章
  • 漫谈设计模式 [14]:迭代器模式