LoginSignup
3
5

More than 1 year has passed since last update.

【Flutter】TextFormFieldにクリアボタンを追加する

Posted at

はじめに

Flutterのテキスト入力フィールドによくある以下のようなクリアボタンを実装する方法を紹介。
スクリーンショット 2021-05-30 9.03.03.png

方法

InputDecorationとTextEditingControllerを用いる。

final TextEditingController _categoryNameController =
      new TextEditingController(text: '');
...
TextFormField(
  controller: _categoryNameController,
  keyboardType: TextInputType.text,
  decoration: InputDecoration(
    hintText: 'カテゴリ名',
      suffixIcon: IconButton(
        onPressed: () =>
            _categoryNameController.clear(),
        icon: Icon(Icons.clear),
      ),
      icon: Icon(
        Icons.folder,
        color: Colors.grey,
      ),
    ),
  )

おわりに

こちらを参考。

3
5
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
3
5