如何解决 "Checkbox" 图标的问题?

How can I solve a problem with the "Checkbox" icon?

我遵循德语 flutter 教程。在本教程中,我们构建了一个简单的待办事项列表。我有以下问题:

https://github.com/simpleclub/startup_teens_flutter/blob/master/lib/stateless_widgets.dart

这是第 35 行的 link 到 Github repo.The 错误。我该怎么办?

试试下面的代码:

 leading: Checkbox(
      onChanged: (bool? value){
        // your code for onChanged
      },
      value: false,
    ),

这是完整的工作code

我认为您遇到的错误与

类似
Error: Required named parameter 'onChanged' must be provided.

如前所述,您必须提供 onChanged 函数才能使用复选框。您还应该使用变量来确定值,并在 onChanged 函数中更新该变量。

class ToDoItem extends StatelessWidget {
  final String title;
  bool check = false; //Variable to monitor checkbox state
  
  ToDoItem(this.title); //I've removed const to allow check to be variable

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 22),
      child: ListTile(
        contentPadding: EdgeInsets.symmetric(vertical: 8.0),
        leading: Checkbox(
            value: check,   //Use check variable here
            onChanged: (bool? v) { //onChanged parameter here
              check = v ?? false;
            }),
        title: Text(
          title,
          style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600, color: Colors.black54),
        ),
        trailing: Icon(Icons.delete_outline),
      ),
    );
  }
}

注意:您当前使用的是 StatelessWidget。但是,您需要一个 StatefulWidget 才能成功更新复选框。