flutter Text 控件介绍

一、使用方法

  1. 构造方法
  • 设置普通的text Text("")
Text(this.data, {Key key,this.style,this.textAlign,this.textDirection,this.locale,this.softWrap,this.overflow,this.textScaleFactor,this.maxLines,this.semanticsLabel,})Text('Hello, $_name! How are you?',textAlign: TextAlign.center,overflow: TextOverflow.ellipsis,style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20),)复制代码
  • 设置textSpan Text.rich()
Text.rich(this.textSpan, {Key key,this.style,this.textAlign,this.textDirection,this.locale,this.softWrap,this.overflow,this.textScaleFactor,this.maxLines,this.semanticsLabel,})Text.rich(TextSpan(text: 'Hello', // default text stylechildren: <TextSpan>[TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic,color: Colors.red,fontSize: 20)),TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20)),],))
复制代码

二、常用属性

  1. 设置样式 通过TextStyle 设置文字的样式
TextStyle({this.inherit = true,//是否继承this.color,//字体颜色this.fontSize,//字体大小this.fontWeight,//字体厚度,也就是字体粗细this.fontStyle,//字体样式 normal或者italicthis.letterSpacing,//字母间隙(负值可以让字母更紧凑)this.wordSpacing,//单词间隙(负值可以让单词更紧凑)this.textBaseline,//文本绘制基线(alphabetic/ideographic)this.height,//高度this.locale,//区域设置this.foreground,//设置前景this.background,//设置背景颜色this.shadows,//设置阴影this.decoration,//设置文字装饰(none/underline/overline/lineThrough)this.decorationColor,//设置文字装饰颜色this.decorationStyle,//文字装饰的风格(solid/double/dotted/dashed/wavy)this.debugLabel,//?String fontFamily,//?String package,//?})
复制代码

2、设置字体对齐方式 TextAlign

  • center 将文本对齐容器的中心
  • end 对齐容器后缘上的文本。
 对于从左到右的文本(TextDirection.ltr),这是右边缘。对于从右到左的文本(TextDirection.rtl),这是左边缘。
复制代码
  • justify 拉伸结束的文本行以填充容器的宽度,对齐边缘。

  • left 对齐容器左边缘的文本。

  • right 对齐容器右边缘的文本。

  • start 对齐容器前缘上的文本。

对于从左到右的文本(TextDirection.ltr),这是左边缘。
对于从右到左的文本(TextDirection.rtl),这是正确的边缘。
复制代码

3、maxLines 文本要跨越的可选最大行数。如果文本超过给定的行数,则会根据溢出将其截断。

4、textDirection 用于设置文本的对齐方式

  • 有些语言是从左到右书写的(例如,英语,泰米尔语或中文),而其他语言是从右到左书写的(例如阿拉姆语,希伯来语或乌尔都语)。有些也是混合写的,例如阿拉伯语大多是从右到左书写的,数字从左到右书写。textDirection 主要就是用于处理文字的对齐形式。
  • TextDirection有两种枚举:
    TextDirection.trl 从左到右TextDirection.trl 从右到左
复制代码

5、overflow 当文字超出屏幕的时候,设置处理方式

    TextOverflow.clip(末位裁剪)TextOverflow.fade(末位渐隐)TextOverflow.ellipsis(末位省略号)
复制代码

6、设置字体显示倍率

    textScaleFactor: 2.0,//设置字体显示倍率为原来字体的两倍
复制代码

三、一个完整的例子

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget{@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Text Demo',theme: ThemeData(primarySwatch: Colors.green),home: TextPageDemo(title: 'Text Demo'),);}
}class TextPageDemo extends StatefulWidget {TextPageDemo({Key key, this.title}) : super(key: key);final String title;@override_TextPageDemoState createState() => _TextPageDemoState();
}class _TextPageDemoState extends State<TextPageDemo>{@overrideWidget build(BuildContext context) {var _name = "flutter ";return Scaffold(appBar: AppBar(title: Text(widget.title),),body: Container(width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: ListView(children: <Widget>[Text('1.Hello, $_name! How are you? \n',textAlign: TextAlign.center,style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20),),// 例子3 设置TextSpanText.rich(TextSpan(text: '2. Hello', // default text stylechildren: <TextSpan>[TextSpan(text: ' beautiful ',style: TextStyle(fontStyle: FontStyle.italic,color: Colors.red,fontSize: 20,debugLabel: "得到的",fontFamily: "aaaaaaa")),TextSpan(text: 'world \n',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20)),],)),Text(//例子3 设置当文字的对齐形式'3. Hello flutter! Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter! \n',
//             enum TextAlign {
//           /// Align the text on the left edge of the container.
//           left,
//           /// Align the text on the right edge of the container.
//           right,
//           /// Align the text in the center of the container.
//           center,
//           /// Stretch lines of text that end with a soft line break to fill the width of
//           /// the container.
//           /// Lines that end with hard line breaks are aligned towards the [start] edge.
//           justify,
//           /// Align the text on the leading edge of the container.
//           ///
//           /// For left-to-right text ([TextDirection.ltr]), this is the left edge.
//           ///
//           /// For right-to-left text ([TextDirection.rtl]), this is the right edge.
//           start,
//           /// Align the text on the trailing edge of the container.
//           ///
//           /// For left-to-right text ([TextDirection.ltr]), this is the right edge.
//           ///
//           /// For right-to-left text ([TextDirection.rtl]), this is the left edge.
//           end,
//           }textAlign: TextAlign.left,maxLines: 5,textDirection: TextDirection.ltr,),Text(// 例子4 设置当文字超出屏幕的时候,如何处理 ,及设置文本的书写方向'4. Hello flutter! Hellod flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!\n',textAlign: TextAlign.left,textDirection: TextDirection.ltr,style: TextStyle(fontSize: 20),overflow: TextOverflow.fade,),Text(//例子5 设置是否自动换行'5. Hello flutter! Hellod flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!Hello flutter!\n',textAlign: TextAlign.left,softWrap: true,//设置是否自动换行textDirection: TextDirection.ltr,style: TextStyle(fontSize: 20),overflow: TextOverflow.fade,),Text(//例子6 设置字体显示倍率,可以通过设置字体显示倍率调整文字的大小'6.Hello flutter text \n',textAlign: TextAlign.left,softWrap: true,//设置是否自动换行textDirection: TextDirection.rtl,style: TextStyle(fontSize: 20),overflow: TextOverflow.fade,textScaleFactor: 2.0,//设置字体显示倍率为原来字体的两倍),],),),);}
}复制代码

转载于:https://juejin.im/post/5c909ac0e51d456b9514ad5f

Flutter 系列文章:Flutter Text 控件介绍相关推荐

  1. Flutter 系列文章:Flutter Icon 控件介绍

    Flutter Icon 控件介绍 一.使用方法 构造方法 Icon(this.icon, {Key key,this.size,this.color,this.semanticLabel,this. ...

  2. flutter初体验之基础控件知识

    在naive开发中,大家对view肯定很熟悉,它代表着所有控件的祖先.在flutter中,也存在这么一个所有控件的祖先---Widget.Widget类是一个抽象类,定义在系统的framework.d ...

  3. Flutter Container、Center设置控件居中背景及其他属性

    Flutter Container.Center设置控件居中背景及其他属性 //控件可以居中Container(alignment: Alignment.bottomCenter,//设置控件内容的位 ...

  4. ASP.NET Atlas简单控件介绍——InputControl,TextBox,Button和CheckBox

    作者:Dflying Chen (http://dflying.cnblogs.com/) 注:本系列文章比较基础,基本为Atlas官方文档的翻译,熟悉Atlas的朋友可以跳过. 本系列有三篇文章: ...

  5. App控件定位:Android 控件介绍及元素定位方法

    本文将分享Android相关基础知识和Android APP控件定位工具的使用方法. 目录 Android基础知识 Android布局 Android四大组件 1.activity 2.Service ...

  6. operamasks-ui2.0 +MVC4.0+EF5.0实战之一 开篇及布局控件介绍

    两年前,曾打算自己开发一个web开发框架,把部门.人员.权限.日志作为基本服务加入进去,在其基础上可以做业务快速开发,结果没有坚持下去,仅仅开了个头就夭折了.究其原因,一方面是采用自己完全不熟悉的新技 ...

  7. ASP.NET Atlas简单控件介绍——Sys.Component基类与Sys.UI.Control基类

    作者:Dflying Chen (http://dflying.cnblogs.com/) 本系列有三篇文章: ASP.NET Atlas简单控件介绍--Sys.Component基类与Sys.UI. ...

  8. Qt常用控件介绍(一)

    Qt常用控件介绍 Qt Creator 的使用技巧 Qt Creator的常用快捷键 按钮 QPushButton QToolButton QRadioButton QCheckBox QComman ...

  9. 【cocosStdio系列】之UI控件下

    [cocosStdio系列]之UI控件下       大家好,我是Lampard       今天与大家通过一个demo了解cocos给我们提供的各个UI控件 reference:<cocos2 ...

最新文章

  1. Java项目:校园人力人事资源管理系统(java+Springboot+ssm+mysql+jsp+maven)
  2. 网页爬虫 python-Python爬虫解析网页的4种方式
  3. 有简历,为何还要自我介绍?
  4. T7-Dropout 解决 overfitting 过拟合
  5. 微信小程序隐藏菜单栏目下转发按钮的几个方法
  6. 二进制(1):无符号编码和补码编码
  7. vue通过数据驱动实现表格行的增加与删除
  8. Linux下安装MySQL8
  9. 【leetcode】链表题(python)
  10. [转]Linux平台下的service程序编写指南
  11. vue 钉钉授权第三方WEB网站扫码登录功能
  12. 音视频——Codec初始化及Omx组件创建
  13. QUOTED_IDENTIFIER选项设置不正确
  14. 汇编语言里 eax, ebx, ecx, edx, esi, edi, ebp, esp 寄存器 含义
  15. 升级到JUnit5的7个理由
  16. oracle口试问题,Oracle口试复习(二)
  17. 计算机word表格求和怎么操作,Word表格编辑技巧:利用“公式”命令求和-word技巧-电脑技巧收藏家...
  18. BlazePose: On-device Real-time Body Pose tracking
  19. mysql 唯一键_MySQL数据库8(十)唯一键
  20. 【转载】一个硕士程序员的求婚日记——做开发的不是木头人!

热门文章

  1. VB快速读取 TextBox 第 N 行的资料
  2. WordPress网站访问慢解决方案(超详细图文教程)
  3. 手把手教你训练一个秒杀科比的投篮AI,不服来练 | 附开源代码
  4. 芯片大神Jim Keller从特斯拉离职,转投“宿敌”英特尔
  5. 谍照曝光!特斯拉正在测试完全自动驾驶
  6. Waymo捷豹合推电动无人车I-PACE,将加入无人出租车队
  7. 2018年13个AI趋势 | CB Insights报告
  8. zabbix的trigger
  9. LVS的VS/NAT及VS/DR类型实现
  10. POJ 2553 The Bottom of a Graph