【问题标题】:How to implement checkbox list in flutter?如何在 flutter 中实现复选框列表?
【发布时间】:2023-02-16 22:23:07
【问题描述】:

我是一个颤振初学者。如何像下面这样在 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,
            ),
            
          ),

【问题讨论】:

  • 你能包括你的完整示例小部件吗

标签: flutter dart


【解决方案1】:

您需要创建两个 bool 变量来控制两个复选框。

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,
            ),
          ),
        ],
      ),
    );
  }

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多