1
4

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 3 years have passed since last update.

Flutterのラジオボタンの使い方

Posted at

Flutterのラジオボタンを使っていて、微妙にわかりにくいなと思った部分があるので共有します。

公開ステータスを設定するためのラジオボタンを作りたいと仮定します。

enum Status {
    open,
    hidden,
    notSelected,
}

上記のようなenumでStatusを定義して、Radioのそれぞれのコンストラクタの意味合いを解説しますと

// enumでもintでもStringでも可。後述のvalueやgroupValueの型を決定する
Radio<Status>(
    // 選択されたときのonChangedに吐き出される値
    value: Status.open,
    // groupValueと上記のvalueが一致していた場合に、ラジオボタンは選択済みの表示になる
    groupValue: status,
    onChanged: (value) {
        // 選択時にStatus.openがvalueとして吐き出される
    }
)

上記のような使い方ができました。
初期値としてstatus = Status.notSelectedを指定しておけば、ラジオボタンを未選択な状態にできますし、デフォルトで公開にしておきたければstatsu = Status.openと指定しておくことで実現できます。

1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?