どうも、ちょげ(@chogetarou)です。
TextFormFieldのフォーカスが外れた時に処理をする方法を紹介します。
方法

TextFormFieldがフォーカスが外れた時に処理をするには、Focusウェジェットを使います。
まず、TextFormFieldをFocusでラップします。
次に、Focusの引数「onFocusChange」に「(hasFocus) {}」を指定します。
そして、関数内で引数がfalseの条件分岐をし、そこでフォーカスが外れた時の処理を指定します。
Focus(
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
),
),
onFocusChange: (hasFocus) {
if (!hasFocus) {
//フォーカスが外れた時の処理
}
},
),
使用例
以下は、使用例です。
final _editController = TextEditingController();
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Focus(
child: TextFormField(
controller: _editController,
decoration: InputDecoration(
border: OutlineInputBorder(),
),
),
onFocusChange: (hasFocus) {
if (!hasFocus) {
_editController.clear();
}
},
),
),
),
),
);
}

[Flutter]TextFormFieldにプレースホルダーを表示するには?
TextFormFieldにプレースホルダーを表示する方法を紹介します。

[Flutter]TextFormFieldのテキストを上に寄せる方法
TextFormFieldのテキストを上に寄せる方法を紹介します。

[Flutter]TextFormFieldのテキストを縦方向で真ん中に寄せるには?
TextFormFieldのテキストを縦方向で中央に寄せる方法を紹介します。

[Flutter]TextFormFieldのプレースホルダーのテキストサイズを設定するには?
TextFormFieldで表示するプレスホルダーのテキストの大きさを設定する方法を紹介します。
コメント