Flutter에서 글 머리 기호 생성

Aug 18 2020

flutter에서 textField를 사용하여 사용자 입력을 시도하고 있지만 글 머리 기호 목록으로 가져 가고 싶습니다. flutter_markdown이라는 패키지를 사용해야한다고 생각했지만 어떻게해야할지 모르겠습니다. 모든 마지막 줄은 자동으로 글 머리 기호로 시작해야합니다.

답변

1 JigarPatel Aug 18 2020 at 18:47

이를 수행 할 수있는 한 가지 방법은 이와 같은 것입니다.

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  TextEditingController tc = TextEditingController();
  int currentTextLength = 0;
  
  @override
  Widget build(BuildContext context) {
    return TextField(
      maxLines: 4,
      controller: tc,
      onChanged: (String newText){
        if(newText[0] != '•'){
          newText = '• ' + newText;
          tc.text = newText;
          tc.selection = TextSelection.fromPosition(TextPosition(offset: tc.text.length));
        }
        if(newText[newText.length - 1] == '\n' && newText.length > currentTextLength){
          tc.text = newText + '• ';
          tc.selection = TextSelection.fromPosition(TextPosition(offset: tc.text.length));
        }
        currentTextLength = tc.text.length;
      }
    );
  }
}

참고 : 이것은 완벽한 솔루션이 아닙니다. 기존 텍스트 중간에 텍스트 편집을 시작하면 글 머리 기호를 넣지 않기 때문에. 그러나 이것은 당신에게 방향을 제시 할 수 있습니다.

2 JupiterChan Aug 18 2020 at 15:46

글 머리 기호 텍스트 필드 목록을 원하면 flutter_markdown이 적합하지 않을 수 있습니다. 마크 다운 텍스트를 표시하기 때문입니다.

TextFormField(
              decoration: InputDecoration(
                border: InputBorder.none, prefix: Text("."), // Or a Dot image
              ),
            ),
sleepingkit Aug 19 2020 at 12:09

글 머리 기호 기능이있는 텍스트 필드를 원할 경우이 패키지가 적합 할 수 있습니다.

https://pub.dev/packages/zefyr

직접 작성하면 복잡합니다.

사용 방법을 배우려면 예제 코드를 살펴보십시오.

https://github.com/memspace/zefyr/tree/master/packages/zefyr/example/lib