0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Flutterでパスワード入力フォームを作る(表示・非表示切り替え付き)

0
Posted at

パスワード入力では、入力内容を隠す(マスクする)だけでなく、
「表示・非表示を切り替える機能」もよく使われます。

今回はその実装方法を紹介します。

実装イメージ

Screenshot 2026-04-12 17.39.56.png

class PasswordField extends StatefulWidget {
  final Function(String) onChanged;

  const PasswordField({super.key, required this.onChanged});

  @override
  State<PasswordField> createState() => _PasswordFieldState();
}

class _PasswordFieldState extends State<PasswordField> {
  bool obscure = true;

  @override
  Widget build(BuildContext context) {
    return TextField(
      obscureText: obscure,
      onChanged: widget.onChanged,
      decoration: InputDecoration(
        hintText: 'パスワード',
        prefixIcon: const Icon(Icons.lock),
        suffixIcon: IconButton(
          icon: Icon(obscure ? Icons.visibility_off : Icons.visibility),
          onPressed: () {
            setState(() {
              obscure = !obscure;
            });
          },
        ),
        filled: true,
        fillColor: Colors.white.withValues(alpha: 0.6),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
      ),
    );
  }
}

obscureText: obscure,
trueのとき、入力された文字が「●●●」のように隠されます。

obscure = !obscure;
ボタン押下時にtrue / falseを切り替えることで、
パスワードの表示状態を変更しています。

suffixIcon: IconButton(...)
入力欄の右側にボタンを配置しています。

使用方法

String password = '';


PasswordField(
  onChanged: (value) {
    setState(() {
      password = value;
    });
  },
),

以上で、パスワード入力UIの作成は完了です。

今回はシンプルにTextFieldで実装しましたが、
Flutterにはバリデーションが簡単に行えるTextFormFieldも用意されています。
こちらについては次回解説していきます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?