Флажок Flutter не работает в AlertDialog - PullRequest
0 голосов
/ 04 мая 2020

Я делаю флажок в AlertDialog. Я хочу, чтобы пользователь поставил галочку перед загрузкой. Моя проблема в том, что если я установлю переменную как false, флажок не изменится на true или изменится на false . Вот мой код:

bool _isChecked = false;

List<String> _texts = [
 "I have confirm the data is correct",
 "I have agreed to terms and conditions.",
];

showConfirmationDialog(BuildContext context){
      AlertDialog alert=AlertDialog(
        title: Text('Confirmation'),
        content: 
        ListView(
        padding: EdgeInsets.all(8.0),
        children: _texts.map((text) => CheckboxListTile(
          title: Text(text),
            value: _isChecked,
            onChanged: (val) {
            setState(() {
            _isChecked = val;           
              });
            },
          )).toList(),
        ),
        actions: <Widget>[
          SizedBox(
            width: 300, 
            child:RaisedButton(
              color: Colors.blue,
              onPressed: () => upload(context), 
              child:Text('Upload'),
            )
          )
        ],
      );
      showDialog(barrierDismissible: false,
        context:context,
        builder:(BuildContext context){
          return alert;
        },
      );
  }

Кто-нибудь может мне помочь?

Ответы [ 3 ]

1 голос
/ 04 мая 2020

Вам нужен виджет с отслеживанием состояния, потому что showDialog не может изменить состояние.

Следующий код поможет вам понять больше.

class DeleteWidget extends StatefulWidget {
  @override
  _DeleteWidgetState createState() => _DeleteWidgetState();
}

class _DeleteWidgetState extends State<DeleteWidget> {
  var currentSection;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: RaisedButton(
            onPressed: () {
              showConfirmationDialog(context);
            },
            child: Text("data"),
          ),
        ),
      ),
    );
  }

  showConfirmationDialog(BuildContext context) {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return CustomDialog();
      },
    );
  }
}

class CustomDialog extends StatefulWidget {
  @override
  _CustomDialogState createState() => _CustomDialogState();
}

class _CustomDialogState extends State<CustomDialog> {
  List<bool> _isChecked = [false, false];
  bool canUpload = false;
  List<String> _texts = [
    "I have confirm the data is correct",
    "I have agreed to terms and conditions.",
  ];
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Confirmation'),
      content: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Expanded(
              child: ListView(
                  shrinkWrap: true,
                  padding: EdgeInsets.all(8.0),
                  children: [
                    ListView.builder(
                      shrinkWrap: true,
                      itemCount: _texts.length,
                      itemBuilder: (_, index) {
                        return CheckboxListTile(
                          title: Text(_texts[index]),
                          value: _isChecked[index],
                          onChanged: (val) {
                            setState(() {
                              _isChecked[index] = val;
                              canUpload = true;
                              for (var item in _isChecked) {
                                if (item == false) {
                                  canUpload = false;
                                }
                              }
                            });
                          },
                        );
                      },
                    ),
                  ]),
            ),
          ],
        ),
      ),
      actions: <Widget>[
        SizedBox(
            width: 300,
            child: RaisedButton(
              color: Colors.blue,
              onPressed: canUpload
                  ? () {
                      print("upload");
                    }
                  : null,
              child: Text('Upload'),
            ))
      ],
    );
  }
}
0 голосов
/ 04 мая 2020
bool _isChecked = false;

List<String> _texts = [
 "I have confirm the data is correct",
 "I have agreed to terms and conditions.",
];      

showConfirmationDialog(BuildContext context) {
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (context) {
            return StatefulBuilder(
              builder: (context, setState) {
                return AlertDialog(
                  title: Text('Confirmation'),
                  content: ListView(
                    padding: EdgeInsets.all(8.0),
                    children: _texts
                        .map((text) => CheckboxListTile(
                              activeColor: Colors.pink,
                              title: Text(text),
                              value: _isChecked,
                              onChanged: (val) {
                                setState(() {
                                  _isChecked = val;
                                });
                              },
                            ))
                        .toList(),
                  ),
                  actions: <Widget>[
                    SizedBox(
                        width: 300,
                        child: RaisedButton(
                          color: Colors.blue,
                          onPressed: () => () {
                            debugPrint("upload image");
                          },
                          child: Text('Upload'),
                        ))
                  ],
                );
              },
            );
          },
        );
      }
0 голосов
/ 04 мая 2020

внутри класса добавить


void isChecked(bool newValue) => setState(() {
    _isChecked = newValue;

    if (_isChecked) {
      //something here
    } else {
      // change value here
    }
  });

//in checkbox 

  onChanged: (val) {
            isChecked(val);
                    }         

Я надеюсь, что это работает

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...