Skip to Content

Text Widget trong Flutter

1. Cơ bản

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

Text('Hello, Flutter!')

2. TextStyle

Customize text với TextStyle:

Text( 'Styled Text', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.blue, fontStyle: FontStyle.italic, ), )

Các thuộc tính phổ biến

PropertyKiểuVí dụ
fontSizedouble20.0
fontWeightFontWeightFontWeight.bold
colorColorColors.red
fontStyleFontStyleFontStyle.italic
letterSpacingdouble2.0
wordSpacingdouble5.0
heightdouble1.5 (line height)
decorationTextDecorationTextDecoration.underline

3. Text Overflow

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.clipCắt text
TextOverflow.fadeLàm mờ dần
TextOverflow.ellipsisThêm …
TextOverflow.visibleCho phép tràn

4. Text Alignment

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

5. RichText và TextSpan

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, ), ), ], ), )

6. SelectableText

Cho phép copy text:

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

7. Text Decoration

Text( 'Decorated Text', style: TextStyle( decoration: TextDecoration.underline, decorationColor: Colors.red, decorationStyle: TextDecorationStyle.dashed, decorationThickness: 2, ), )
DecorationMô tả
TextDecoration.underlineGạch chân
TextDecoration.lineThroughGạch ngang
TextDecoration.overlineGạch trên

8. Custom Fonts

Thêm font vào pubspec.yaml

flutter: fonts: - family: Roboto fonts: - asset: assets/fonts/Roboto-Regular.ttf - asset: assets/fonts/Roboto-Bold.ttf weight: 700

Sử dụng

Text( 'Custom Font', style: TextStyle( fontFamily: 'Roboto', fontSize: 18, ), )

9. Ví dụ thực tế

Article Preview

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, ), ), ], )

📝 Tóm tắt

Widget/PropertyMục đích
TextHiển thị text cơ bản
TextStyleStyle cho text
overflowXử lý text tràn
maxLinesGiới hạn số dòng
textAlignCăn chỉnh text
RichText / Text.richText nhiều styles
SelectableTextText có thể copy
Last updated on