如何在flutter中实现复选框列表?

nvbavucw  于 2023-02-20  发布在  Flutter
关注(0)|答案(1)|浏览(122)

我是一个flutter初学者。如何实现flutter中的2个复选框列表?

Padding(
            padding: const EdgeInsets.only(top: 20),
            child: CheckboxlListTitle(
              title: const Text('Title1'),
              value: _isChecked,
              onChanged: (bool? newValue) {
                setState(() {
                  _isChecked = newValue;
                });
              },
              activeColor: Colors.green,
              controlAffinity: ListTileControlAffinity.leading,
              tristate: false,
            ),
            
          ),
zujrkrfu

zujrkrfu1#

你需要创建两个bool变量来控制两个checkBox。

class TestFe161 extends StatefulWidget {
  const TestFe161({super.key});

  @override
  State<TestFe161> createState() => _TestFe161State();
}

class _TestFe161State extends State<TestFe161> {
  bool _isChecked0 = false;
  bool _isChecked1 = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Padding( // you can use helper method/widget to reduce the snippet size.
            padding: const EdgeInsets.only(top: 20),
            child: CheckboxListTile(
              title: const Text('Title1'),
              value: _isChecked0,
              onChanged: (bool? newValue) {
                setState(() {
                  _isChecked0 = newValue ?? false;
                });
              },
              activeColor: Colors.green,
              controlAffinity: ListTileControlAffinity.leading,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 20),
            child: CheckboxListTile(
              title: const Text('Title2'),
              value: _isChecked1,
              onChanged: (bool? newValue) {
                setState(() {
                  _isChecked1 = newValue ?? false;
                });
              },
              activeColor: Colors.green,
              controlAffinity: ListTileControlAffinity.leading,
            ),
          ),
        ],
      ),
    );
  }

相关问题