вы можете отследить, какие переключатели включены, с помощью List<bool>
внутри вашего StatefulWidget
и принять решение об изменении состояния любого переключателя в соответствии с состоянием других переключателей, которые вы получаете из массива, что-то вроде это:
class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
List<bool> switchStates;
@override
void initState() {
switchStates = List<bool>(10)..fillRange(0, 10,false);
super.initState();
}
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: 10,
itemBuilder: (ctx, index) => Switch(
value: switchStates[index],
onChanged: (newValue){
if(newValue)//user is trying to set switch state to true, so we don't check value of other switches as this action is always allowed
{
switchStates[index] = newValue;//on
}
else if(!newValue)//user is trying to set switch to false, so we do a check on all other switches before we turn it off
{
bool areAllOff = true;//suppose all are off
for(final state in switchStates){
if(state)//if at least one is on, so we can turn current switch off
switchStates[index]=newValue;//off
break;
}
}
},
),
);
}
}