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?

More than 1 year has passed since last update.

Flutter TextFieldの備忘録

Posted at

FlutterのTextFieldの備忘録

参考サイト

目次

  1. 最大文字数を設定する
  2. カーソルを非表示にする
  3. 中央揃えにする
  4. 下線を消す

最大文字数を設定する

最大文字数を設定する方法は2つあるようである。
1つ目はmaxLengthを直接指定する方法である。

TextField(
    maxLength: 10,
)

こちらの場合は現在の文字の入力数と最大数が表示される。
image.png
いや表示してほしくないんだがな...という方は次のinputFormattersを指定する方法が有効である。

TextField(
    inputFormatters: [
        LengthLimitingTextInputFormatter(1),
      ],
)

こちらの場合は現在の文字数と最大文字数が表示されない。
image.png

カーソルを非表示にする

テキストを入力しようとすると出てくる縦線のカーソルを消す。
image.png
TextFieldのshowCursorをfalseにする。

TextField(
    showCursor: false,
)

中央揃えにする

testAlignにTextAlign.centerを入力すればよい。

TextField(
    textAlign: TextAlign.center
)

そのほかにもleft, right, justify, start, endが指定できそうである。
leftが左揃え、rightが右揃え、justifyが両端揃えというのは分かったが、start, endってなんだ?と思ったので調べた。
startは、言語の始まりに合わせるという意味らしい(そのままやな...)。
日本語の場合は左から右に読む言語なので、左揃えになるが、世界には右から左に読んでいく言語もあるらしい。その場合は右揃えにしてくれるらしい。
へぇ~便利な時代やねぇ。

下線を消す

テキストの周り(枠の部分とかかな?)の処理はTextFieldのdecorationにInputDecorationクラスのインスタンスを入れることで、指定できるようである。
今回は下線を消したいので、InputBorder.noneを指定した。

TextField(
    decoration: InputDecoration(
        border: InputBorder.none
    )
)

他にもUnderlineInputBorder()OutlineInputBorder()を指定できるようである。
Underlineは初期の下線だと思われる。Outlineの方はテキストフィールドを囲う四角形を書くことができる。

TextField(
    decoration: InputDecoration(
        border: OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(10.0))
        )
    )
)

OutlineInputBorderには、引数を指定することでいろいろ境界線を変えれそうである。
image.png
丸いテキストフィールドがたくさんwww。
ただ、半径の指定をするためだけに、かなりたくさんのクラスを見る必要があって、なんか面倒だなぁ...と感じてしまった。Border.Radius.allは四隅全部を指定するために必要。左上のみを指定するなどができるっぽい。Radius.circular(radius)も別メソッドとして、Radius.elliptical(x,y)が用意されている。こちらはx,yを指定することで、楕円にすることができるらしい。

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?