简体   繁体   中英

AlertDialog with a CheckboxListTile in flutter

I'm having some problems when I use a CheckboxListTile inside a AlertDialog. The problem is that when I try to check, the box doesn't work correctly. This is my code:

import 'package:flutter/material.dart';

void main() {
  runApp(PadelApp());
}

class PadelApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      debugShowCheckedModeBanner: false, //esto es para quitar el cartelito de debug
      title: 'Test',
      theme: ThemeData( primarySwatch: Colors.blue),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget{
  //@override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>{

  var _formkey = GlobalKey<FormState>();

  var _lstgrupos = ['Group 1', 'Group 2'];
  List<String> _SelectedGroupsItems = [];
  String _SelectedGroups = 'Groups';
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Test')),

      body: Form(
        key: _formkey,
        child: Padding(
          padding: const EdgeInsets.all(10.0),
          child: ListView(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: ListTile(
                      title: Text(_SelectedGroups),
                      onTap: () => showDialog(
                        context: context,
                        barrierDismissible: true,
                        builder: (context) {
                          return AlertDialog(
                            title: Text("Choose a group"),
                            content: SingleChildScrollView(
                              child: Container(
                                width: double.infinity,
                                child: Column(
                                  mainAxisSize: MainAxisSize.min,
                                  children: _lstgrupos.map((lst) => CheckboxListTile(
                                    title: Text(lst),
                                    value:_SelectedGroupsItems.contains(lst),
                                    onChanged: (value) {
                                      setState(() {
                                        if (value!) {
                                          if (!_SelectedGroupsItems.contains(lst)) {
                                            _SelectedGroupsItems.add(lst);
                                          }
                                        } else {
                                          if (_SelectedGroupsItems.contains(lst)) {
                                            _SelectedGroupsItems.add(lst);
                                          }
                                        }
                                      });
                                    },
                                  )).toList(),
                                ),
                              ),
                            ),
                            actions: <Widget>[
                              TextButton(
                                child: Text('Cerrar'),
                                  onPressed: ()=> Navigator.of(context).pop(),
                              )
                            ]
                          );
                        }
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

I have tried to modified in several ways but I realize that the problem is in "value" variable (in onChanged: (value)). This variable is always true o always false but doesn't change ???

Thanks in advanced

You need surround your AlertDialog with StatefullBuilder Otherwise the setState does not rebuild the dialog Like this :

StatefulBuilder(
        builder: (context, setState) {
return AlertDialog(...)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM