I have used FilterChip and using it many screens…but to avoid duplicate and lengthy codes i want to create customFilterChip widget with only three properties
onSelect method
label String
isSelected bool
but i dont know how to deal with onSelect method while creating final method…
class CustomFilterChipWidget extends StatelessWidget {
final bool isSelected;
final String lable;
//how declare function onSelect;
const CustomFilterChipWidget({Key? key,required this.isSelected,required this.lable,required this.onSelect}) : super(key: key);
@override
Widget build(BuildContext context) {
return FilterChip(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
showCheckmark: false,
selectedColor: Colors.white,
backgroundColor: Colors.white,
shape: StadiumBorder(
side: BorderSide(
width: isSelected ? 2 : 0,
color: isSelected
? Colors.blue
: Colors.grey),
),
selected: isSelected,
label: Text(
lable,),
onSelected: onSelect);
}
}
as in filter chip
it requires value in argument
onSelected: (value) {
}
2
Answers
You were close: first, accept a function
onSelected
:and then call it:
The complete code will be:
See also
You can literally create your own version of a widget, by redefining the widget itself, for that, you just need to extend the original class, and pay attention to all the required properties that FilterChip needs:
If you are not sure what properties you can access or modify, navigate to the
FilterChip
(CTRL+Left Click on the class name in Visual Studio Code on Windows), and you can check all the properties, data types, and methods of the class.