I recommend you study the documentation of the components Listtile and Checkboxlisttile to better understand how they work. Remember that they were created only to facilitate the implementation of useful components that are used in numerous apps. In his case it is noted that he did not perform the basic tests of each one alone exploiting its properties.
No need to insert a CheckboxListTile
within a ListTile
. The CheckboxListTile
was already created to show the check, he was born for it. In this example all you need is it.
I have put together an example that can be analyzed by you in order to understand how it works.
Example
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
final itens = <Map>[
{"nome": "Arroz", "check": false},
{"nome": "Feijão", "check": false},
{"nome": "Macarrão", "check": false},
{"nome": "Leite", "check": false},
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: itens.length,
itemBuilder: (ctx, index) {
return CheckboxListTile(
title: Text(itens[index]["nome"]),
subtitle: Text(
"Este item ${itens[index]["check"] ? "" : "não"} está selecionado",
),
value: itens[index]["check"],
onChanged: (newValue) {
setState(() {
itens[index]["check"] = newValue;
});
print(
"Mudou check da palavra ${itens[index]["nome"]} para: $newValue",
);
},
);
},
);
}
}
This code can be copied and pasted on Dartpad. I recommend that you use it a lot to create examples and understand the behaviors of each component that is already offered ready to use.
If I understand your code correctly, there are points to be considered... The
ListTile()
was created to present information in an organized and beautiful way and works well. TheCheckboxListTile()
is a variation ofListTile
... understanding this, what reason made you put aCheckboxListTile
within aRow
which in turn is inside aListTile
?... Tried to use only theCheckboxListTile
and fill in the attributes he has?– Leonardo Paim
I haven’t tried, but I’ll try now! It’s just that I’m trying to show a list, which is in another class, but I can’t call 'Meal.ingredientes', which is the list that will have the checkbox on the side, I tried to use a '.Tolist()' and '.Tostring()', but I have no idea what to do...
– Aiko Kikuchi
I answered an example of using a list with a
check
and can run it on the same dartpad... before what you presented code was what gave to guide... in case it is not enough, edit your question and put the full code of the application and so it will be possible to analyze what is happening.– Leonardo Paim