flutter控件練習demo地址:githubandroid
Text: 單一格式的文本 使用比較多的 , 至關於 android 中的TextViewgit
import 'package:flutter/material.dart';
class TextDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Container(
color: Colors.red,
width: 200,
height: 200,
child: Text(
"1234561222222222222222222222222222333222ssssssssssssssssss",
textAlign: TextAlign.left,
// 對齊方式
// textDirection: TextDirection.ltr, // 文本流向
softWrap: false, // 是否容許換行 , 若是設置了 maxLines 此屬性失效
overflow: TextOverflow.ellipsis,
// 文本的截斷方式
// TextOverflow.ellipsis 以三個點結尾 ,
// TextOverflow.clip(默認的截斷方式)強制截斷,沒有任何商量
// TextOverflow.fade 褪色的截斷
textScaleFactor: 2,// 放大比例(估計通常用不到,通常直接設置 style 來設置 大小
maxLines:3, // 最大的行數
// semanticsLabel: "adfadsfasdf", (暫時不知道什麼用)
));
}
}
複製代碼
RichText Text.rich: 能夠作出多種樣式的文本絢麗多彩, 至關於 android 中的SpannableStringgithub
Text.rich 和 Text 源碼都是經過 RichText 實現的
複製代碼
class TextRichDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("TextRich"),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text.rich(
TextSpan(
// 必須設置一個父TextStyle 不然 字體是白色
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
text: "Text.rich 實現: ",
children: <TextSpan>[
TextSpan(
text: '絢麗',
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.normal)),
TextSpan(
text: '文本',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
)),
TextSpan(
text: '樣式',
style: TextStyle(
fontStyle: FontStyle.italic,
color: Colors.black,
fontSize: 18,
decoration: TextDecoration.lineThrough,
fontWeight: FontWeight.normal)),
]),
style: TextStyle(height: 2),
),
RichText(
text: TextSpan(
// 必須設置一個父TextStyle 不然 字體是白色
style:
TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
text: "RichText 實現: ",
children: <TextSpan>[
TextSpan(
text: '絢麗',
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.normal)),
TextSpan(
text: '文本',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.blue)),
TextSpan(
text: '樣式',
style: TextStyle(
fontStyle: FontStyle.italic,
color: Colors.black,
fontSize: 18,
decoration: TextDecoration.lineThrough,
fontWeight: FontWeight.normal)),
],
))
],
),
));
}
}
複製代碼
Text 的 樣式bash
class TextStyleDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
Paint paintBlue = Paint();
paintBlue.color = Colors.blue;
Paint paintRed = Paint();
paintRed.color = Colors.red;
// TODO: implement build
return Scaffold(
appBar: AppBar(title: Text("TextStyle"),centerTitle: true,),
body: Padding(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"inherit=false時 默認的文字顏色是白色,至關於徹底從新樣式",
style: TextStyle(inherit: false, color: Colors.red),
),
Divider(),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(
"fontWeight: ",
style: TextStyle(fontWeight: FontWeight.w700),
),
Text(
"FontWeight.w400(默認)",
style: TextStyle(fontWeight: FontWeight.w400),
),
Text(
"FontWeight.w700(正常加粗)",
style: TextStyle(fontWeight: FontWeight.w700),
),
],
),
Divider(),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(
"fontStyle: ",
style: TextStyle(fontWeight: FontWeight.w700),
),
Text(
"FontStyle.normal(正常)",
style: TextStyle(fontStyle: FontStyle.normal),
),
Text(
"FontStyle.italic(斜體)",
style: TextStyle(fontStyle: FontStyle.italic),
),
],
),
Divider(),
Text(
"letterSpacing,每一個字符的間距",
style: TextStyle(letterSpacing: 4),
),
Divider(),
Text(
"wordSpacing 單詞 每一個單詞之間的距離(至關於給空格設置的距離)",
style: TextStyle(wordSpacing: 10),
),
Divider(),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(
"textBaseline: ",
style: TextStyle(fontWeight: FontWeight.w700),
),
Text(
"TextBaseline.ideographic",
style: TextStyle(textBaseline: TextBaseline.ideographic),
),
Text(
" TextBaseline.alphabetic",
style: TextStyle(textBaseline: TextBaseline.alphabetic),
),
],
),
Divider(),
Text(
"foreground 至關於設置paint,來繪製文字",
style: TextStyle(foreground: paintBlue),
),
Divider(),
Text(
"background 文本的背景",
style: TextStyle(background: paintRed),
),
Divider(),
Text(
"shadows 文本的陰影",
style: TextStyle(shadows: [
BoxShadow(
color: Colors.grey, offset: Offset(-1, -1), blurRadius: 5)
]),
),
Divider(),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(
"decoration: ",
style: TextStyle(fontWeight: FontWeight.w700),
),
Text(
"TextDecoration.overline(上劃線)",
style: TextStyle(decoration: TextDecoration.overline),
),
Text(
"TextDecoration.none(沒有劃線)",
style: TextStyle(decoration: TextDecoration.none),
),
Text(
"TextDecoration.underline(下劃線)",
style: TextStyle(decoration: TextDecoration.underline),
),
Text(
"TextDecoration.lineThrough(中劃線,刪除線)",
style: TextStyle(
decoration: TextDecoration.lineThrough,
textBaseline: TextBaseline.alphabetic),
),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(
"decorationStyle: ",
style: TextStyle(fontWeight: FontWeight.w700),
),
Text(
"TextDecorationStyle.solid(實線)",
style: TextStyle(
decoration: TextDecoration.lineThrough,
decorationStyle: TextDecorationStyle.solid),
),
Text(
"TextDecorationStyle.double(兩條線)",
style: TextStyle(
decoration: TextDecoration.lineThrough,
decorationStyle: TextDecorationStyle.double),
),
],
),
],
)),
);
}
}
複製代碼