im trying to implement list of filterchips in get.defaultDialog() but the chip is not selected after the setstate() function. is it possible to create a stateful getx dialogbox in flutter Getx
Get.defaultDialog(
title: "Select lable",
content: Wrap(
spacing: 8,
runSpacing: 8,
children:
controller.lables.map((lable) {
return FilterChip(
label: Text(lable),
selected:
filter.contains(lable),
onSelected: (val) {
if (val) {
filter.add(lable);
} else {
filter.removeWhere(
(element) {
return element == lable;
});
}
setState(() {});
});
}).toList()),
actions: <Widget>[
TextButton(
onPressed: () {
Get.back();
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {},
child: const Text('OK'),
),
],
);
Make a new statefull class for body of your dialog and pass it like
Get.defaultDialog(
title: "Select lable",
// pass statefull class which contain your dialog body
content:DialogBody(labels:controller.labels),
actions: <Widget>[
TextButton(
onPressed: () {
Get.back();
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {},
child: const Text('OK'),
),
],
);
now i make a new state full class which contain you dialog body
class DialogBody extends StatefulWidget {
DialogBody({super.key,required this.lables});
List lables;
@override
State<DialogBody> createState() => _DialogBodyState();
}
class _DialogBodyState extends State<DialogBody> {
@override
Widget build(BuildContext context) {
return Wrap(
spacing: 8,
runSpacing: 8,
children: widget.lables.map((lable) {
return FilterChip(
label: Text(lable),
selected: filter.contains(lable),
onSelected: (val) {
if (val) {
filter.add(lable);
} else {
filter.removeWhere((element) {
return element == lable;
});
}
setState(() {});
});
}).toList());
}
}
You can also pass some parameters to you class (dialog content/body)