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

入力フォームの定番な、メールアドレス入力用のUIを作成してみます。

実装イメージ

Screenshot 2026-04-12 17.12.05.png

サンプルコード

class EmailField extends StatelessWidget {
  final Function(String) onChanged;

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

  @override
  Widget build(BuildContext context) {
    return TextField(
      onChanged: onChanged,
      decoration: InputDecoration(
        hintText: 'メールアドレス',
        prefixIcon: const Icon(Icons.mail),
        filled: true,
        fillColor: Colors.white.withValues(alpha: 0.6),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
        ),
      ),
      keyboardType: TextInputType.emailAddress,
    );
  }
}

final Function(String) onChanged;
TextFieldの入力値を親Widgetに渡すために用意しています。

onChanged: onChanged,
これで、入力のたびに値が親に通知されます。

decoration: InputDecoration(...)
見た目などはここで設定を行います

使用方法

親Widgetで状態を管理します。
親レイアウトはStatefulWidgetにします。

String email = '';


EmailField(
  onChanged: (value) {
    setState(() {
      email = value;
    });
  },
),

以上で、入力フォームの作成は完了です。
シンプルな実装ですが、コンポーネント化することで再利用性の高いUIを作ることができます。

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?