文本組件顯示一個(gè)但一樣式的文本字符串,字符串可能多行顯示渔欢,也可能全部顯示在一行中,具體取決于布局約束瘟忱。其提供兩種構(gòu)造函數(shù)奥额。
樣式參數(shù)時(shí)可選的。當(dāng)省略時(shí)访诱,文本將使用最接近的DefaultTextStyle
中的樣式垫挨。如果給定樣式的TextStyle.inherit
屬性為true(默認(rèn)),則給定樣式將于最接近的DefaultTextStyle
的樣式合并触菜。這種合并行為是很有用的九榔,例如,使用默認(rèn)字體系列和大小時(shí)使文本粗體顯示。
- 若使用文本對(duì)觸摸事件作出反應(yīng)帚屉,請(qǐng)將其包裝在
GestureDetector
組件中,并使用GestureDetector.onTap
處理漾峡。 - 在Material Design應(yīng)用程序中攻旦,考慮使用
FlatButton
代替,或者如果不適用生逸,至少使用InkWell
來代替GestureDetector
牢屋。 - 若使用部分文本做交互,需要使用
RichText
槽袄,并將TapeReservator
指定為文本相關(guān)部分的TextSpan.recognizer
烙无。-
RichText
:提供了對(duì)文本樣式的更多控制; -
DefaultTextStyle
:為文本組件提供默認(rèn)樣式遍尺。
-
1. new Text():創(chuàng)建一個(gè)文本組件
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Text組件"),
),
body: Center(
child: Text(
"Hello world",
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold
),
),
),
),
);
}
}
new Text
2. new Text.rich():使用TextSpan創(chuàng)建文本組件
Text.rich(
TextSpan(text: "Text.rich",children: [
TextSpan(text: "01",style: TextStyle(fontSize: 25.0)),
TextSpan(text: "02"),
TextSpan(text: "03"),
TextSpan(text: "04")
]),
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
overflow: TextOverflow.ellipsis,
)
new Text.rich
Text屬性
Text屬性
本節(jié)內(nèi)容到此結(jié)束截酷,若在使用過程中遇到問題,歡迎留言交流乾戏,我們一起成長(zhǎng)迂苛。