본문 바로가기
Dart/Flutter

[Flutter] `TextField`, `TextFormField` 특정 포커스(Focus) 위치하기

by 검은냥냥이 2022. 12. 23.

플러터에서 `TextField`, `TextFormField`에서 맨 앞 혹은 맨뒤, 입력 텍스트에 특정 위치로 포커스가 커서가 이동되어야 한다면, 아래에 예제와 같이 텍스트필드에 컨트롤러를 지정하고 `selection`으로 `TextSelection 옵션`을 주면 됩니다.

 

예제

/// Controller
Rx<TextEditingController> textEditingController = TextEditingController().obs;

/// View
class MyApp extends GetView<MyAppController> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: TextField(
          controller: controller.textEditingController.value,
          maxLines: 3,
          onTap: () {
            /// 맨 앞에서 3번째로 커서 이동
            controller.textEditingController.value..selection =
            TextSelection.collapsed(offset: 3);
          },
          decoration: InputDecoration.collapsed(
              hintText: "",
              border: InputBorder.none,
        ),
      ),
    );
  }
}
728x90
사업자 정보 표시
레플라 | 홍대기 | 경기도 부천시 부일로 519 화신오피스텔 1404호 | 사업자 등록번호 : 726-04-01977 | TEL : 070-8800-6071 | Mail : support@reafla.co.kr | 통신판매신고번호 : 호 | 사이버몰의 이용약관 바로가기