参数类型 'Function' 无法分配给参数类型 'void Function(bool?)?'
The argument type 'Function' can't be assigned to the parameter type 'void Function(bool?)?'
我正在尝试使用构造函数参数传递一个函数,但它显示了标题中提到的错误。
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,
);
}
}
我在网上搜索了这个,但没有成功。
所以,如果有人能帮助我,我将不胜感激。
您的 final Function toggleCheckboxState;
需要一个 bool?
参数。\
这样试试:
final Function(bool?) toggleCheckboxState;
您还需要像这样在分配 toggleCheckboxState
时更改代码,
toggleCheckboxState: (bool? checkboxState) {
setState(() {
isChecked = checkboxState!;
});
},
bool? checkboxState
是必需的,因为您的类型是 Function(bool?)
`import 'dart:ui';
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked = false;
checkBoxCallback(bool checkBoxState) {
setState(() {
isChecked = checkBoxState;
});
}
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
"Complete Assignments",
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
fontSize: 20,
),
),
trailing: TaskCheckbox(
checkBoxState: isChecked, toggleCheckBoxState: checkBoxCallback), );
}
}
class TaskCheckbox extends StatelessWidget {
final bool checkBoxState;
final Function toggleCheckBoxState;
const TaskCheckbox(
{Key? key,
required this.checkBoxState,
required this.toggleCheckBoxState})
: super(key: key);
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.blue,
value: checkBoxState,
onChanged: (newValue) {
toggleCheckBoxState(newValue);
});
}
}
'I am convinced that you were looking at the course of Anjela Yu and me too I found the solution'@关键字是关于空安全的向下转换
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 as void Function(bool?)?
);
}
}
遇到同样的问题,下面是我喜欢使用的代码示例。
有关详细信息,请查看文档:Checkbox class
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool isChecked = false;
@override
Widget build(BuildContext context) {
Color getColor(Set<MaterialState> states) {
const Set<MaterialState> interactiveStates = <MaterialState>{
MaterialState.pressed,
MaterialState.hovered,
MaterialState.focused,
};
if (states.any(interactiveStates.contains)) {
return Colors.blue;
}
return Colors.red;
}
return Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.resolveWith(getColor),
value: isChecked,
onChanged: (bool? value) {
setState(() {
isChecked = value!;
});
},
);
}
}
我正在尝试使用构造函数参数传递一个函数,但它显示了标题中提到的错误。
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,
);
}
}
我在网上搜索了这个,但没有成功。
所以,如果有人能帮助我,我将不胜感激。
您的 final Function toggleCheckboxState;
需要一个 bool?
参数。\
这样试试:
final Function(bool?) toggleCheckboxState;
您还需要像这样在分配 toggleCheckboxState
时更改代码,
toggleCheckboxState: (bool? checkboxState) {
setState(() {
isChecked = checkboxState!;
});
},
bool? checkboxState
是必需的,因为您的类型是 Function(bool?)
`import 'dart:ui';
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked = false;
checkBoxCallback(bool checkBoxState) {
setState(() {
isChecked = checkBoxState;
});
}
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
"Complete Assignments",
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
fontSize: 20,
),
),
trailing: TaskCheckbox(
checkBoxState: isChecked, toggleCheckBoxState: checkBoxCallback), );
}
}
class TaskCheckbox extends StatelessWidget {
final bool checkBoxState;
final Function toggleCheckBoxState;
const TaskCheckbox(
{Key? key,
required this.checkBoxState,
required this.toggleCheckBoxState})
: super(key: key);
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.blue,
value: checkBoxState,
onChanged: (newValue) {
toggleCheckBoxState(newValue);
});
}
}
'I am convinced that you were looking at the course of Anjela Yu and me too I found the solution'@关键字是关于空安全的向下转换
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 as void Function(bool?)?
);
}
}
遇到同样的问题,下面是我喜欢使用的代码示例。 有关详细信息,请查看文档:Checkbox class
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool isChecked = false;
@override
Widget build(BuildContext context) {
Color getColor(Set<MaterialState> states) {
const Set<MaterialState> interactiveStates = <MaterialState>{
MaterialState.pressed,
MaterialState.hovered,
MaterialState.focused,
};
if (states.any(interactiveStates.contains)) {
return Colors.blue;
}
return Colors.red;
}
return Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.resolveWith(getColor),
value: isChecked,
onChanged: (bool? value) {
setState(() {
isChecked = value!;
});
},
);
}
}