Flutter - Dialog에서 State다루기

Chance·2022년 10월 14일
0

showDialog

SimpleDialog

State를 다룰 수 없다.

AlertDialog

State를 다룰수 있는 Alert
= StatefulBuilder를 사용해서 alert창 안에서 상태를 변경할 수 있다.

예시

showDialog(
  context: context,
  builder: (BuildContext context) {

    int selectedRadio = 0; // Declare your variable outside the builder
    
    return AlertDialog( 
      content: StatefulBuilder(  // You need this, notice the parameters below:
        builder: (BuildContext context, StateSetter setState) {
          return Column(  // Then, the content of your dialog.
            mainAxisSize: MainAxisSize.min,
            children: List<Widget>.generate(4, (int index) {
              return Radio<int>(
                value: index,
                groupValue: selectedRadio,
                onChanged: (int value) {
                  // Whenever you need, call setState on your variable
                  setState(() => selectedRadio = value);
                },
              );
            }),
          );
        },
      ),
    );
  },
);

0개의 댓글

관련 채용 정보