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 - ChoiceChipを選択しても切り替わらない問題の解決方法

Posted at

Flutterでアプリ開発中,この問題に直面して調べていたのですが,
日本語で解決方法を載せている方が見当たらなかったため作成しました.

問題

showModalBottomSheet上に配置したChoiceChipを選択しても更新されない.
showModalBottomSheetを動かすとChoiceChipが更新されるため,変数の代入が問題ではない.

example_1.gif

解決方法

ChoiceChipをStatefulBuilderで囲むと解決します.

Code

example.dart
...
  Future<void> showBottomSheet(BuildContext context) {
    int choiceIndex = 0;
    return showModalBottomSheet<dynamic>(
      context: context,
      builder: (BuildContext context) {
        return SizedBox(
          height: 844,
          child: Column(
            children: [
+             StatefulBuilder(
+               builder: (BuildContext context, StateSetter setState) {
+                 return 
                  Container(
                    padding: const EdgeInsets.only(top: 20),
                    child: Wrap(
                      spacing: 20,
                      children: [
                        ChoiceChip(
                          label: const Text("選択 1"),
                          selected: choiceIndex == 0,
                          backgroundColor: Colors.grey[700],
                          selectedColor: Colors.white,
                          onSelected: (_) {
                            setState(() {
                              choiceIndex = 0;
                            });
                          },
                        ),
                        ChoiceChip(
                          label: const Text("選択 2"),
                          selected: choiceIndex == 1,
                          backgroundColor: Colors.grey[700],
                          selectedColor: Colors.white,
                          onSelected: (_) {
                            setState(() {
                              choiceIndex = 1;
                            });
                          },
                        ),
                      ],
                    ),
                  );
+               },
+             ),
              SizedBox(
                  height: 300,
                  child: CupertinoDatePicker(
                    use24hFormat: true,
                    initialDateTime: DateTime.now(),
                    mode: CupertinoDatePickerMode.time,
                    onDateTimeChanged: (time) {},
                  )),
            ],
          ),
        );
      },
    );
  }
...

動作例

example_2.gif

参考

解決した時のStack OverFlow

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?