How to disable clicks detection in a CheckBox that's wrapped by an InkWell in Flutter - flutter

I have those 4 Inkwell(), inside each of them is a CheckBox(),
In the onTap: property of the InkWell() I assigned an unnamed function that does some logic.
When I click on the checkbox, the 'onTap' property of the parent InkWell isn't invoked.
What I wanna do, is when I click on the CheckBox, the onTap of the parent widget InkWell get called, so it's like disabling the CheckBox onChanged property, and letting it just like an unclickable widget.
Note: By assigning null to the onChanged property of CheckBox, the widget still absorb the click, so the onTap property of InkWell doesn't get called.
Here is my code:
InkWell(
onTap: () { /* DO SOMETHING */},
child: Row(
children: [
SizedBox( /* The flag icon */ ),
Text( /* The language name */ ),
Checkbox(
onChanged: null,
value: (selectedLanguage == widget.id) ? true : false,
)
],
),
)

You can wrap your Checkbox widget inside an IgnorePointer
IgnorePointer(
child: Checkbox(
value: selectedLanguage == widget.id,
onChanged: (v) {
// This won't get called
},
),
)

Related

inkwell ontab does not work when clicking on the form

I use the searchField, judging by the documentation, there should be an onTab field in the searcField widget, but apparently the library has been updated and there is no such function anymore, since I need to trigger an event in the block when clicking on the field, I decided to wrap the widget in inkwell, but when clicked, nothing works and the event does not called
BlocBuilder<FillprofileBloc, FillprofileState>(
builder: (context, state) {
return InkWell(
focusColor: Colors.transparent,
onTap: () {
_bloc.add(OnCitySearchTabEvent());
},
child: SearchField(
searchInputDecoration: InputDecoration(
There is a workaround with GestureDetector.
GestureDetector(
behavior: HitTestBehavior.translucent,
onPanDown: (_) {
debugPrint("pan down");
},
child: SearchField(),
),
You can use onTap param of TextField itself here. Or wrap your TextField with IgnorePointer to use InkWell

How to get tap event inside absorb pointer

I have a page containing many widgets which is opened in view only mode. i used AbsorbPointer for ignoring user inputs. but in some cases i have to get onTap event on one button which is inside AbsorbPointer. how can i achieve that ?
I tried using GestureDetector but its not giving onTap event.
any idea??
AbsorbPointer(
absorbing:true,
child: Column(children: [
Text("HELLO"),
Text("HELLO 2"),
GestureDetector(
onTap: () {
showToast("I tried this");
},
child: IconButton(
icon: Icon(
Icons.local_offer,
color: Colors.red,
),
onPressed: () {
print("I need this event");
},
),
)
]),
);
use absorbing: property for your cases... set it to false in the case you want to allow the GestureDetector inside

flutter unable to select dropdown value using the a container onTap

i am trying to use a container to select a dropdown menu by wrapping the dropdown button with a container and then use a Gesture Detector. but i am getting an error
The argument type 'void Function(String?)' can't be assigned to the parameter type 'void Function()?'
but if i make use of the unchange function on the dropdown menu its working fine
GestureDetector(
onTap: (String? newValue){
setState(() {
dropdowntargetGroupValue = newValue!;
});
},
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: dropdowntargetGroupValue,
icon: Visibility(
visible: false, child: Icon(Icons.arrow_downward)),
items: targetGroup.map((String items) {
return DropdownMenuItem(
value: items, child: Text(items));
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdowntargetGroupValue = newValue!;
});
},
),
),
),
You can't use onChanged callback in onTap.
I don't know what you're trying to do by wrapping DropdownButton with GestureDetector here but to make the DropdownButton open when another widget is tapped you've to make custom DropdownButton or use existing one. If you trying to do something when the DropdownButton is tapped, DropdownButton has it's own onTap property, you can use it.

Long pressed menu in flutter

how to make this top bar menu when we do a long press on a card or container.
you can wrap your card widget with InkWell or GestureDetector both have longPress, then you pass a call back to your view to show the menu.
Color _colorFilterTile = Colors.transparent;
InkWell(
onLongPress: (){
//call function
setState(() {
_colorFilterTile= Colors.green.withOpacity(0.5);
});
},
child: Container(
color: _colorFilterTile,
child: ...,
),
)

How to skip Flutter Widget in tap detection?

I'm trying to make a modal bottom sheet with ListTiles that contain a Checkbox as the leading widget. However, the Checkbox will receive tap event instead of the ListTile, now showing the ink ripple effect for the tile and making me implement two onTap/onChanged callbacks.
I have tried not implementing the Checkbox's onChanged callback and wrapping it around an AbsorbPointer widget, however in these two cases the ListTile would also not get the tap event.
return ListTile(
onTap: onTap,
title: Text("Show expired schedules"),
leading: Checkbox(value: snapshot.data, onChanged: (_) => onTap()),
);
I would like to somehow make the Checkbox not tappable, but have the ListTile still receive the tap event. Kind of like AbsorbPointer, except skipping one Widget, not absorbing the tap event entirely.
Wrap your Checkbox widget inside IgnorePointer widget and it should work:
return ListTile(
onTap: onTap,
title: Text("Show expired schedules"),
leading: IgnorePointer(child: Checkbox(value: snapshot.data, onChanged: (_) => onTap())),
);
You can do that using AbsorbPointer too
return ListTile(
onTap: onTap,
title: Text("Show expired schedules"),
leading: AbsorbPointer(
absorbing: _condition, // bool value, true makes it absorb touch event on CheckBox still making ListTile tappable.
child: Checkbox(value: snapshot.data, onChanged: (_) => onTap()),
),
);