TextField에서 사용자에게 받는 내용을 숫자로만 제한해야할 때가 있다.
keyboardType 옵션을 사용해서 숫자 키패드만 나오게 할 수가 있는데, 이럴 경우 문자열 붙여넣기가 가능하여 문자열이 들어갈 수 있다.
💡해결방법
숫자를 완전히 허용하지 않게 하려면, inputFormatters 옵션에서 필터링을 주면 됨!
final _costController = TextEditingController();
CupertinoTextField(
cursorColor: style.colors['main1'],
placeholder: '금액을 입력해주세요',
padding: EdgeInsets.all(13),
controller: _costController,
onChanged: onChanged,
maxLines: 1,
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp('[0-9]'),
)
],
),

✔️ TitleInput은 CupertinoTextField를 직접 커스텀한 위젯입니다!
✔️ 0~9까지의 숫자만 허용하겠다는 의미! (allow 사용)
'Frontend > Flutter' 카테고리의 다른 글
| [Flutter] SnackBar 다중 알림 방지, 스낵바 여러번 뜨지 않게 하기 (0) | 2024.02.26 |
|---|---|
| [Flutter] 상태관리 라이브러리 GetX (0) | 2024.02.22 |
| [Flutter] TextFormField를 사용하여 textfield 상태관리 하기 (2) | 2024.02.21 |
| [Flutter] 앱 아이콘 변경(앱 이미지 설정) (0) | 2024.02.12 |
| [Flutter] remove AppBar leading icon, AppBar에서 뒤로가기 버튼 없애기 (2) | 2024.02.12 |