안드로이드에서 여러 텍스트들 사이에서 원하는 Style을 적용하고 싶은 경우에 Span을 사용한다.
Flutter에서 이러한 처리를 하기 위해 RichText 위젯을 사용하는데 사용법은 이렇다.
RichText(
text: TextSpan(
style: const TextStyle(
color: Colors.white,
fontSize: 18,
),
children: [
const TextSpan(text: "정답은 "),
TextSpan(
text: "사과",
style: const TextStyle(
color: Colors.yellow, fontWeight: FontWeight.bold)),
const TextSpan(text: "에요."),
],
);
RichText로 감싸고 TextSpan을 넣어서 기본적으로 사용할 Style을 지정한다.
이후에 children에서 Text를 지정해주면 된다.
RichText에 text 속성에 TextSpan을 넣고 text를 지정해줄 수도 있기는 한데
모든 텍스트를 children쪽에 모으는 것이 더 깔끔해 보인다.
'프로그래밍 > Flutter' 카테고리의 다른 글
[Flutter] 둥근 버튼 만들기 (0) | 2022.04.03 |
---|---|
[Flutter] List<TextSpan> in ChangeNotifier (0) | 2022.04.02 |
[Flutter] 같은 행에 Widget 정렬하기 (0) | 2022.03.31 |
[Flutter] BottomSheet 크기 조정 (0) | 2022.03.31 |
[Flutter] Null-Safety (0) | 2022.03.27 |
댓글