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

Flutter基本组件Text使用

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

Text简单Demo

import 'package:flutter/material.dart';class MyTextDemo extends StatelessWidget {const MyTextDemo({super.key});@overrideWidget 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类型的值。

 @overrideWidget 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});@overrideWidget 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});@overrideWidget 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.lryc.cn/news/433102.html

相关文章:

  • DDS基本原理--FPGA学习笔记
  • 有temp表包含A,B两列,使用SQL,对B列进行处理,形成C列,按A列顺序,B列值不变,则C列累计技术,B列值变化,则C列重新开始计数
  • 【H2O2|全栈】关于HTML(6)HTML基础(五 · 完结篇)
  • 2024第三届大学生算法大赛 真题训练一 解题报告 | 珂学家
  • IIS网站允许3D模型类型的文件
  • Linux 性能调优之CPU上下文切换
  • 【无标题】符文价值的退化页
  • DFS 算法:洛谷B3625迷宫寻路
  • 结构开发笔记(七):solidworks软件(六):装配摄像头、摄像头座以及螺丝,完成摄像头结构示意图
  • Android 15 新特性快速解读指南
  • 【机器人工具箱Robotics Toolbox开发笔记(十九)】机器人工具箱Link类函数参数说明
  • 排查SQL Server中的内存不足及其他疑难问题
  • 输送线相机拍照信号触发(博途PLC高速计数器中断立即输出应用)
  • 【数学分析笔记】第3章第1节 函数极限(6)
  • 程序员如何写笔记?
  • Linux网络——Socket编程函数
  • HarmonyOS 是如何实现一次开发多端部署 -- HarmonyOS自学1
  • 嵌入式硬件-ARM处理器架构,CPU,SOC片上系统处理器
  • 《JavaEE进阶》----12.<SpringIOCDI【扫描路径+DI详解+经典面试题+总结】>
  • Selenium 自动化测试:常用函数与实例代码
  • python网络爬虫(五)——爬取天气预报
  • 四.海量数据实时分析-Doris数据导入导出
  • 一. 从Hive开始
  • Linux下的PWM驱动
  • 日语输入法平假名和片假名切换
  • Oracle向量搜索及其应用场景
  • 【排序算法】六、快速排序补充:三指针+随机数法
  • PyTorch torch.cdist函数介绍及示例代码
  • CTK框架(四): 插件编写
  • 深入理解C代码中的条件编译