I'M currently working on a to-do app following a tutorial this is the checkbox part
I'm trying to lift it's state up the tree, but when I do so the stateless widget just keeps rebuilding non stop , I tried adding key , adding const converting it to stateful but nothing seems to work it also doesn't provide me with any kind of error or exceptions messages
note : when I set the onChecked to null the statelessWidget runs twice when it should only build once..
below is the code:
import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
class _TaskTileState extends State {
bool isChecked = false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
"Complete Todoey",
style: TextStyle(
decoration: isChecked == true
? TextDecoration.lineThrough
: TextDecoration.none),
),
trailing: TaskCheckBox(
checkBoxState: isChecked,
toggleCheckBoxState:
(bool checkBoxState) {
Future.delayed(Duration.zero,
() async {
setState(() {
print(
"===================$isChecked==============$checkBoxState======================");
isChecked = checkBoxState;
});
});
}),
);
}
}
class TaskCheckBox extends StatelessWidget {
const TaskCheckBox(
{Key? key,
required this.checkBoxState,
required this.toggleCheckBoxState})
: super(key: key);
final bool checkBoxState;
final Function toggleCheckBoxState;
@override
Widget build(BuildContext context) {
print(
"=====================================$checkBoxState+++++++++++++++++++++++++++++++++++++++++");
return Checkbox(
value: checkBoxState,
onChanged:
null, // toggleCheckBoxState(checkBoxState),
);
}
}
a snip from the console output :
Performing hot restart... Syncing files to device Android SDK built for x86... I/flutter ( 3401): ===================false==============false====================== I/flutter ( 3401): =====================================false+++++++++++++++++++++++++++++++++++++++++ I/flutter ( 3401): ===================false==============false====================== I/flutter ( 3401): =====================================false+++++++++++++++++++++++++++++++++++++++++ I/flutter ( 3401): ===================false==============false====================== I/flutter ( 3401): =====================================false+++++++++++++++++++++++++++++++++++++++++ I/flutter ( 3401): ===================false==============false====================== I/flutter ( 3401): =====================================false+++++++++++++++++++++++++++++++++++++++++ I/flutter ( 3401): ===================false==============false====================== I/flutter ( 3401): =====================================false+++++++++++++++++++++++++++++++++++++++++
a snip from the output when onChanged is set to null :
Restarted application in 2,694ms. I/flutter ( 3401): =====================================false+++++++++++++++++++++++++++++++++++++++++ I/flutter ( 3401): =====================================false+++++++++++++++++++++++++++++++++++++++++