13
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Flutter】TextFormField, TextField onChanged使用時のカーソル位置が最初に戻ってしまう不具合対応

Last updated at Posted at 2021-05-02

発生していた不具合

TextFormFieldに入力するたびにカーソルの位置が最初に戻ってしまう現象が発生していました。

原因

入力ごとに毎回初期値が変更されたとみなされ、カーソルの位置が初期状態に戻ってしまっていた。

修正方法

初期値入力時に、テキスト情報に併せて、offset情報も渡す。

コード例

controller: TextEditingController.fromValue(
                TextEditingValue(
                    text: initialString ?? "",
                    selection: TextSelection.collapsed(offset: 
                        initialString?.length ?? 0), 
                ),
            ),

実際の修正例
image.png

参考記事

13
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?