Bỏ qua để đến nội dung

📝 Text Widget

Text widget hiển thị một chuỗi văn bản:

Text('Hello, Flutter!')

Customize text với TextStyle:

Text(
'Styled Text',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
fontStyle: FontStyle.italic,
),
)
Property Kiểu Ví dụ
fontSize double 20.0
fontWeight FontWeight FontWeight.bold
color Color Colors.red
fontStyle FontStyle FontStyle.italic
letterSpacing double 2.0
wordSpacing double 5.0
height double 1.5 (line height)
decoration TextDecoration TextDecoration.underline

Xử lý text quá dài:

Text(
'This is a very long text that might overflow the container',
overflow: TextOverflow.ellipsis, // Thêm ...
maxLines: 2,
)
Giá trị Mô tả
TextOverflow.clip Cắt text
TextOverflow.fade Làm mờ dần
TextOverflow.ellipsis Thêm …
TextOverflow.visible Cho phép tràn

Container(
width: 200,
child: Text(
'Centered text',
textAlign: TextAlign.center,
),
)
Giá trị Mô tả
TextAlign.left Căn trái
TextAlign.right Căn phải
TextAlign.center Căn giữa
TextAlign.justify Căn đều

Hiển thị text với nhiều styles khác nhau:

RichText(
text: TextSpan(
text: 'Hello ',
style: TextStyle(color: Colors.black),
children: [
TextSpan(
text: 'Flutter',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: '!',
style: TextStyle(color: Colors.red),
),
],
),
)

Hoặc dùng Text.rich:

Text.rich(
TextSpan(
text: 'Price: ',
children: [
TextSpan(
text: '\$99',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.green,
),
),
],
),
)

Cho phép copy text:

SelectableText(
'You can select and copy this text',
style: TextStyle(fontSize: 16),
)

Text(
'Decorated Text',
style: TextStyle(
decoration: TextDecoration.underline,
decorationColor: Colors.red,
decorationStyle: TextDecorationStyle.dashed,
decorationThickness: 2,
),
)
Decoration Mô tả
TextDecoration.underline Gạch chân
TextDecoration.lineThrough Gạch ngang
TextDecoration.overline Gạch trên

flutter:
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- asset: assets/fonts/Roboto-Bold.ttf
weight: 700
Text(
'Custom Font',
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 18,
),
)

Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Introduction to Flutter',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
'Flutter is Google\'s UI toolkit for building beautiful apps...',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
height: 1.5,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
SizedBox(height: 4),
Text(
'Read more',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.w500,
),
),
],
)

Widget/Property Mục đích
Text Hiển thị text cơ bản
TextStyle Style cho text
overflow Xử lý text tràn
maxLines Giới hạn số dòng
textAlign Căn chỉnh text
RichText / Text.rich Text nhiều styles
SelectableText Text có thể copy