LoginSignup
12

More than 3 years have passed since last update.

posted at

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

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"),
);

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
What you can do with signing up
12