LoginSignup
4
0

More than 1 year has passed since last update.

Flutter入門 DropDownMenu 配列から選択肢を作成する

Last updated at Posted at 2022-09-24

この記事のゴール

配列からDropDownMenuのitemを作成できるようになる
早速行ってみましょう!

まずは通常のDropDownButton

DropdownButton(
    hint: Text("選択してください"),
    items: [
        DropdownMenuItem(
            child: Text("選択肢1"),
            value: "選択肢1"
        ),
        DropdownMenuItem(
            child: Text("選択肢2"),
            value: "選択肢2"
        ),
    ],
    onChanged: (value){ print(value); }
)

結果

スクリーンショット 2022-09-24 11.21.34.png スクリーンショット 2022-09-24 11.21.15.png

続いて本題の配列から作成するパターン

List<String> selectList = ["選択肢1", "選択肢2"];

DropdownButton(
    hint: Text("選択してください"),
    items: selectList.map((String str) => 
            DropdownMenuItem(child: Text(str), value: str)).toList(),
    onChanged: (value){ print(value); }
    ),

こんな感じで簡単に作成できます。
結果は通常のものと全く同じになります。
注意点は、map関数の最後にtoList()を入れてあげること!

今回は以上です!
これからもFlutterで開発していく中で少しずつ記事にしていきますのでよろしくお願いします!

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