일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
- pytorch
- 영상처리
- 파이토치 트랜스포머를 활용한 자연어 처리와 컴퓨터비전 심층학습
- Flutter
- Computer Architecture
- MDP
- llm을 활용 단어장 앱 개발일지
- Image Processing
- Dreamhack
- Got
- Kaggle
- BFS
- Algorithm
- ML
- BOF
- Widget
- DART
- Stream
- ARM
- C++
- PCA
- bloc
- study book
- FastAPI
- 백준
- system hacking
- rao
- fastapi를 사용한 파이썬 웹 개발
- BAEKJOON
- MATLAB
- Today
- Total
Bull
[Flutter] 플러터에서 마크다운 적용하기! 본문
채팅 관련 서비스를 개발하던 중, GPT api를 불러오는데 생각해보니 GPT는 마크다운으로 답변할 때가 많다. 그래서 응답을 받은 텍스트를 마크다운 그대로 적용시키기 위해 플러터에서 필요했다.
코드블록과 인라인 코드 블록도 이렇게 적용시킬 수 있다.
플러터에서 마크다운을 적용 시키는 패키지는 다음과 같이 flutter_markdown
패키지를 사용한다.
https://pub.dev/packages/flutter_markdown
flutter_markdown | Flutter package
A Markdown renderer for Flutter. Create rich text output, including text styles, tables, links, and more, from plain text data formatted with simple Markdown tags.
pub.dev
기본 예제 말고 현재 서비스 개발중에 있는 코드를 가져와서 설명하겠다.
Container(
padding: EdgeInsets.symmetric(
horizontal: 15, vertical: 7.5),
decoration: BoxDecoration(
color: isUser ? Colors.green : Colors.green[50],
borderRadius: BorderRadius.circular(40),
),
child: MarkdownBody(
data: message.text,
styleSheet: MarkdownStyleSheet(
p: TextStyle(
color:
isUser ? Colors.white : Colors.black87,
fontSize: 16,
height: 1.5,
),
h1: TextStyle(
color:
isUser ? Colors.white : Colors.black87,
fontSize: 24,
fontWeight: FontWeight.bold,
),
h2: TextStyle(
color:
isUser ? Colors.white : Colors.black87,
fontSize: 20,
fontWeight: FontWeight.bold,
),
h3: TextStyle(
color:
isUser ? Colors.white : Colors.black87,
fontSize: 18,
fontWeight: FontWeight.bold,
),
em: TextStyle(
color:
isUser ? Colors.white : Colors.black87,
fontStyle: FontStyle.italic,
),
strong: TextStyle(
color:
isUser ? Colors.white : Colors.black87,
fontWeight: FontWeight.bold,
),
code: TextStyle(
color: const Color.fromARGB(255, 0, 0, 0),
backgroundColor: const Color.fromARGB(255, 251, 255, 246),
),
codeblockDecoration: BoxDecoration(
color: const Color.fromARGB(255, 251, 255, 246),
borderRadius:
BorderRadius.circular(8),
border: Border.all(
color: Colors.grey[300]!),
),
blockquote: TextStyle(
color: Colors.grey[600],
fontStyle: FontStyle.italic,
),
listBullet: TextStyle(
color:
isUser ? Colors.white : Colors.black87,
),
),
),
)
상태에 따라 텍스트를 불러오기 때문에 코드가 조잡해보일 수 있다. 간단하게 프로퍼티를 설명하면 이렇게 하면된다.
Container(
child: MarkdownBody(
data: message.text, // 적용할 텍스트
styleSheet: MarkdownStyleSheet(
p: TextStyle(),
h1: TextStyle(),
h2: TextStyle(),
h3: TextStyle(),
em: TextStyle(),
strong: TextStyle(),
code: TextStyle(),
codeblockDecoration: BoxDecoration(),
blockquote: TextStyle(),
listBullet: TextStyle(),
),
),
)
MarkdownBody
의 부모 위젯을 Container
로 감싸야 한다. 그리고 styleSheet 프로퍼티을 MarkdownStyleSheet
클래스로 설정할 수 있다.
p
: 일반 텍스트h1
: # 헤딩1h2
: ## 헤딩2h3
: ### 헤딩3em
: *이탤릭체* 또는 _이탤릭체_strong
: **볼드체** 또는 __볼드체__code
: '인라인 코드블록' 또는 '''코드 블록''' (백쿼터를 대체)codeblockDecoration
: '인라인 코드블록' 또는 '''코드 블록''' 의 박스 데코레이션blockquote
: > (콜아웃)listBullet
: *, -, 또는 + 리스트 불릿
'Software Framework > Flutter' 카테고리의 다른 글
[Flutter::package] "pdf" package review 하기 (0) | 2024.08.21 |
---|---|
[Flutter::News] LG TV webOS 기반 애플리케이션 Flutter 추진 요약 (0) | 2024.08.20 |
[Flutter::State] Bloc Repository의 역할 (with FastAPI) (0) | 2024.08.15 |
[Flutter::Widget] 토글 스위치...? 토글 버튼? (ToggleButtons class) (0) | 2024.08.14 |
[Flutter::Widget] Drawer icon 변경 방법! (0) | 2024.08.14 |