LoginSignup
21
14

More than 5 years have passed since last update.

flutterでTextFieldにヒントやラベルを表示する

Posted at

Flutterでテキスト入力のWidgetのTextFieldにヒントやラベルを表示する方法を紹介します。

TextFieldのコンストラクタを覗いてみたところ以下のようにそれらしいパラメタが見つかりません。

  const TextField({
    Key key,
    this.controller,
    this.focusNode,
    this.decoration: const InputDecoration(),
    TextInputType keyboardType: TextInputType.text,
    this.style,
    this.textAlign: TextAlign.start,
    this.autofocus: false,
    this.obscureText: false,
    this.autocorrect: true,
    this.maxLines: 1,
    this.maxLength,
    this.maxLengthEnforced: true,
    this.onChanged,
    this.onSubmitted,
    this.inputFormatters,
    this.enabled,
  })

公式サイトを調べたところdecorationを指定することで、ヒントやラベルの表示をするようです。
以下のようなコードにすることでマテリアルデザインのフォームらしい動きを実現することができます。

TextField(
  decoration: InputDecoration(
    labelText: "Category Name",
    hintText: "Some Hint"),
);

21
14
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
21
14