LoginSignup
0
2

More than 1 year has passed since last update.

【Flutter】TextFormFieldについてのメモ

Last updated at Posted at 2021-12-18

TextFormFieldに関するメモ

InputDecoration

text

TextFormField(
  decoration: const InputDecoration(
     labelText: 'LabelText',
     hintText: 'HintText',
     helperText: 'HelperText',
     // errorText: 'ErrorText', // エラー状態になり下の写真で青色の部分が赤色に変わる
     // hintMaxLines: 10,       // hintTextの最大表示行数を指定
  ),
),

Screenshot_1639812477.png
Screenshot_1639812480.png

  • labelText
    • どんなに長くても1行しか表示できない
  • hintText・helperText・errorText
    • ○○MaxLines を指定することで指定した行数が表示される
    • ※それぞれhintMaxLines・helperMaxLines・errorMaxLinesで指定する

prefixText・suffixText

TextFormField(
  decoration: const InputDecoration(
     labelText: 'LabelText',
     hintText: 'ここに記入できる',
     prefixText: 'PrefixText',
     suffixText: 'SuffixText',

     // 分かりやすいように赤文字に変更した
     prefixStyle: TextStyle(color: Colors.red),
     suffixStyle: TextStyle(color: Colors.red),
  ),
),

Screenshot_1639814365.png

prefixTextは左側に、suffixTextは右側に表示される。
上記の場合、記入できるのはprefixTextとsuffixTextの間のみ

border

何も指定しない場合はアンダーライン

decoration: const InputDecoration(
   labelText: 'LabelText',
   border: OutlineInputBorder(),
),

Screenshot_1639813197.png

icon

TextFormField(
  decoration: const InputDecoration(
     labelText: 'Outline',
     border: OutlineInputBorder(),
     icon: Icon(Icons.perm_identity),
  ),
),

TextFormField(
  decoration: const InputDecoration(
     labelText: 'Underline',
     icon: Icon(Icons.perm_identity),
  ),
),

Screenshot_1639813412.png

iconは外部に表示される

prefixIcon・suffixIcon

TextFormField(
  decoration: const InputDecoration(
     labelText: 'Outline',
     border: OutlineInputBorder(),
     prefixIcon: Icon(Icons.perm_identity),
     suffixIcon: Icon(Icons.search),
  ),
),

TextFormField(
  decoration: const InputDecoration(
     labelText: 'Underline',
     prefixIcon: Icon(Icons.perm_identity),
     suffixIcon: Icon(Icons.search),
  ),
),

Screenshot_1639813697.png

prefixIconは左側にsuffixIconは右側に表示される。

textInputAction

keyboardType

autofocus

デフォルトはfalse

autofocus: true, // その画面に来たら自動的にフォーカスされる

obscureText

デフォルトはfalse

obscureText: true, // 入力された文字を隠す。パスワードに最適

Screenshot_1639815381.png

maxLength

maxLengthだけを指定すると、指定された文字数を超えて入力することは出来ない。

maxLength: 10, // 文字数制限

Screenshot_1639815584.png

maxLengthEnforcement

maxLengthEnforcementを一緒に指定すると、文字数を超えても入力は出来る。(ただし、エラー表示される)

maxLength: 10,
maxLengthEnforcement: MaxLengthEnforcement.none,

Screenshot_1639816038.png

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