Flutter---Text
基本属性和属性参数
textAlign: TextAlign.left ,//对齐方式:center/left/right
maxLines: 1,//设置最大行数
//溢出处理方式:ellipsis末尾显示省略号/clip直接裁剪溢出部分/fade渐变淡出
overflow: TextOverflow.clip,
style: TextStyle(//字体的风格
color: Colors.red,//字体颜色
//背景的颜色,可以设置透明度background: Paint()..color = Colors.yellow.withOpacity(0.3), // 30%透明度
fontWeight: FontWeight.bold,
fontSize: 15,//字号fontStyle: FontStyle.italic,//斜体
letterSpacing: 3,//字符间距
wordSpacing: 5,//单词间距decoration: TextDecoration.overline,//划线:underline下划线/lineThrough删除线/overline上划线
decorationColor: Colors.blue,//划线颜色
decorationStyle: TextDecorationStyle.solid,//划线样式:solid实线/dashed虚线
inherit: false,//是否继承父样式
代码实例
main.dart
import 'dart:io';import 'package:flutter/material.dart'; //导入Material设计组件库void main() {runApp(const MyApp()); //应用入口
}class MyApp extends StatelessWidget { //应用根目录 (StatelessWidget不可变UI)const MyApp({super.key});//默认构造函数@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo', //应用标题(显示在任务管理器)theme: ThemeData( //全局主题配置//colorScheme:自动生成的配色方案,textTheme:预定义的文字样式colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),),//首页,应用的第一个页面home: const MyHomePage(title: 'Flutter Demo Home Page'),//构造MyHomePage传入标题);}
}class MyHomePage extends StatefulWidget { //主页(有状态),(StatefulWidget可变UI)//构造函数const MyHomePage({super.key, required this.title});//接收title参数,required表示强制必须传递该参数final String title; //存储通过构造函数传入的标题@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> { //主页状态管理@overrideWidget build(BuildContext context) {return Scaffold( //页面骨架appBar: AppBar(//顶部应用栏组件//将AppBar的背景色设置为当前主题的反转主色backgroundColor: Theme.of(context).colorScheme.inversePrimary,title: Text(widget.title),//使用传入的标题),body: Center(//居中布局child: Column( //垂直排列子组件mainAxisAlignment: MainAxisAlignment.center,children: [Container(width: double.infinity, //占满父容器宽度child: Text('Hello everyone',textAlign: TextAlign.left ,//对齐方式:center/left/rightmaxLines: 1,//设置最大行数//溢出处理方式:ellipsis末尾显示省略号/clip直接裁剪溢出部分/fade渐变淡出overflow: TextOverflow.clip,style: TextStyle(//字体的风格color: Colors.red,//字体颜色//背景的颜色,可以设置透明度background: Paint()..color = Colors.yellow.withOpacity(0.3), // 30%透明度fontWeight: FontWeight.bold,fontSize: 15,//字号fontStyle: FontStyle.italic,//斜体letterSpacing: 3,//字符间距wordSpacing: 5,//单词间距decoration: TextDecoration.overline,//划线:underline下划线/lineThrough删除线/overline上划线decorationColor: Colors.blue,//划线颜色decorationStyle: TextDecorationStyle.solid,//划线样式:solid实线/dashed虚线inherit: false,//是否继承父样式),),),],),),);}
}