본문 바로가기
프로그래밍/Flutter

[Flutter] RichText

by YuminK 2022. 4. 2.

안드로이드에서 여러 텍스트들 사이에서 원하는 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쪽에 모으는 것이 더 깔끔해 보인다.

댓글