van*_*asa 1 flutter statelesswidget flutter-state
我试图使用构造函数参数传递一个函数,但它向我显示了标题中提到的错误。
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked = false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
'This is a task',
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),
),
trailing: TaskCheckbox(
checkboxState: isChecked,
toggleCheckboxState: (bool checkboxState) {
setState(() {
isChecked = checkboxState;
});
},
),
);
}
}
class TaskCheckbox extends StatelessWidget {
final bool checkboxState;
final Function toggleCheckboxState;
TaskCheckbox(
{required this.checkboxState, required this.toggleCheckboxState});
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.lightBlueAccent,
value: checkboxState,
onChanged: toggleCheckboxState,
);
}
}
Run Code Online (Sandbox Code Playgroud)
我在网上搜索了这个,但没有运气。
因此,如果有人可以帮助我,我会非常感激。
你final Function toggleCheckboxState;需要有一个bool?参数。
尝试这样:
final Function(bool?) toggleCheckboxState;
toggleCheckboxState您还需要在像这样分配时更改代码,
toggleCheckboxState: (bool? checkboxState) {
setState(() {
isChecked = checkboxState!;
});
},
Run Code Online (Sandbox Code Playgroud)
bool? checkboxState此处需要,因为您的类型是 Function(bool?)
| 归档时间: |
|
| 查看次数: |
998 次 |
| 最近记录: |