the problem is the header . Im trying to build the todoye app and as you can see the call back is considered ValueChanged<bool?> not a simple function so i cant move my logic to a another statefullwidget to reduce the rebuilds in my app i tried different things but i get error evertime please help . here is my code :
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked = false;
void checkBoxCallBack(bool checkBoxState) {
setState(() {
isChecked = checkBoxState;
});
}
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
'finish up angla\'s course today',
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),
),
trailing: TaskCheckBox(
checkBoxState: isChecked,
toggleCheckBoxState: checkBoxCallBack,
),
);
}
}
class TaskCheckBox extends StatelessWidget {
final bool checkBoxState;
final ValueChanged<bool?> toggleCheckBoxState;
const TaskCheckBox({
required this.checkBoxState,
required this.toggleCheckBoxState,
});
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.lime,
value: checkBoxState,
onChanged: toggleCheckBoxState,
);
}
}
the error is on line 27 ...where i want to pass my custom Function to varaible of my statelessWidget.
I hit the same issue and eventually I got this working
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
bool isChecked = false;
void checkboxCallBack(bool? checkboxState) {
setState(() {
isChecked = checkboxState ?? true;
});
}
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
'Task 1',
style: TextStyle(
decoration:
isChecked ? TextDecoration.lineThrough : TextDecoration.none),
),
trailing: TaskCheckbox(isChecked, checkboxCallBack),
);
}
}
class TaskCheckbox extends StatelessWidget {
final bool checkboxState;
final Function(bool?) toggleCheckboxState;
TaskCheckbox(this.checkboxState, this.toggleCheckboxState);
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.lightBlueAccent,
value: checkboxState,
onChanged: toggleCheckboxState,
);
}
}
Changing this function like this
void checkboxCallBack(bool? checkboxState) {
setState(() {
isChecked = checkboxState ?? true;
});
}
and toggleCheckbox variable to:
final Function(bool?) toggleCheckboxState;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With