I'm making a cinema app. I made the seat selection using checkbox. I would like to deactivate the checkbox of the reserved seat among 200 seats (column: 10 row: 20).
I know that if I input onChanged of the checkbox as null, it will be disabled. The problem is that I created 200 CheckBoxes in one CheckBox class. Is there a part to disable only a part of it?
Positioned(
top: 45,
left: 16,
right: 16,
child: GridView.count(
shrinkWrap: true,
crossAxisCount: 20,
mainAxisSpacing: 2,
crossAxisSpacing: 2,
children: List.generate(200, (index) {
int column = (index % 200).toInt();
return Theme(
data: ThemeData(
primarySwatch: Colors.red,
unselectedWidgetColor: Colors.blueGrey,
),
child: Checkbox(
checkColor: Colors.white,
value: widget.seat![widget.index!][column],
onChanged: (selected) {
setState(() {
// widget.selected[row][column] = selected!;
if (widget.seat![widget.index!][column] == false) {
widget.seat![widget.index!][column] = true;
seat_select ;
} else if (widget.seat![widget.index!][column] == true) {
widget.seat![widget.index!][column] = false;
seat_select--;
}
});
}),
);
}),
),
),
CodePudding user response:
first, as you said, you need to put null
if your seat is taken
children: List.generate(200, (index) {
// int column = (index % 200) // wont this always be 0?
int column = index % 20;
int row = index/20;
return Theme(
child: Checkbox(
checkColor: Colors.white,
value: widget.seat![widget.index!][column],
onChanged:
_isSeatTaken(row, column) ?
null
: (selected) {
// do something
},
),
);
},
),
and then, here you would put the logic for figuring out which seats are taken
bool _isSeatTaken(int row, int column) {
return row == 10 && column == 20
}
CodePudding user response:
problem solving!!
Positioned(
top: 45,
left: 16,
right: 16,
child: GridView.count(
shrinkWrap: true,
crossAxisCount: 20,
mainAxisSpacing: 2,
crossAxisSpacing: 2,
children: List.generate(200, (index) {
int column = (index % 200).toInt();
return Theme(
data: ThemeData(
primarySwatch: Colors.red,
unselectedWidgetColor: Colors.blueGrey,
),
child: Checkbox(
checkColor: Colors.white,
value: widget.seat![widget.index!][column],
onChanged: _isSeatTaken(column)
? null
: (selected) {
setState(() {
// widget.selected[row][column] = selected!;
if (widget.seat![widget.index!][column] ==
false) {
widget.seat![widget.index!][column] = true;
seat_select ;
} else if (widget.seat![widget.index!]
[column] ==
true) {
widget.seat![widget.index!][column] = false;
seat_select--;
}
});
}),
);
}),
),
),
bool _isSeatTaken(int column) {
if (widget.seat![widget.index!][column] == true) {
return true;
}
return false;
}