I’m trying to add a CheckboxListTile but the value of tha checkbox doesn’t updates it always stays false, what can i do? need help, here is the code a have. im working with flutter
class _DashboardState extends State<Dashboard> {
bool isChecked1 = true;
TextEditingController controlNombre = TextEditingController();
TextEditingController controlMail = TextEditingController();
GlobalKey<FormState> keyForm = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
backgroundColor: const Color(0xffff0082),
shadowColor: Colors.white,
title: const Text('Captura de Información'),
),`
body:Form(
key: keyForm,
child: ListView(
padding: const EdgeInsets.fromLTRB(20.0, 40.0, 20.0, 10.0),
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextFieldServices(
'Ingrese Nombre Completo',
controlNombre,
validateText: ValidateText.nombre,
),
CheckboxListTile(
title: const Text(
'Acepto Términos y Condiciones',
),
autofocus: false,
activeColor: const Color(0xffff0082),
checkColor: Colors.white,
selected: isChecked1,
value: isChecked1,
onChanged: (bool? value) {
setState(() {
isChecked1 = value ?? false;
});
},
controlAffinity: ListTileControlAffinity.leading,
tristate: true,
),
]
],
),
]),
));
}
}
I already have a variable bool isChecked1 = false;
i also noticed that if i change the value of the isChecked to true, now the checkbox always stays true.
I need the checkbox to change to true, also it doesn’t get colored when it’s selected.
2
Answers
Make sure define
isChecked1
outside the build method.Try to create a boolean variable outside this code, eg. after declaring your class. Create a function that receives a boolean argument and manipulate your external variable inside your function. Also, use this boolen variable in all places that needs changes in state based on your boolean variable.
I hope it helps you.