I want to be able to click the padding around the GestureDetector widget and trigger onTap method of it. I dont want to change the size of the Icon itself. Here is the code so far.
class InfoButton extends StatelessWidget {
final String text;
final double padding;
static const double defaultPadding = 5;
const InfoButton({Key key, @required this.text, this.padding = defaultPadding}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(padding),
decoration: BoxDecoration(border: Border.all()),
child: GestureDetector(
child: Icon(
Icons.info_outline,
color: Colors.blue,
size: 20,
),
onTap: () {
print('clicked');
},
),
);
}
}
How to I achieve this in flutter?
To set padding for Container widget in Flutter, set padding property wit the required EdgeInsets value. Via EdgeInsets class object, we can set padding for top, right, bottom, and left with separate values, or a set a same value for padding on all sides.
But in Flutter, if you want add some extra space around a widget, then you wrap it in a Padding widget. Now to add padding, wrap the Text widget with a Padding widget. In Android Studio this can be accomplished by placing your cursor on the widget and pressing Option+Enter (or Alt+Enter in Windows/Linux).
When passing layout constraints to its child, padding shrinks the constraints by the given padding, causing the child to layout at a smaller size. Padding then sizes itself to its child's size, inflated by the padding, effectively creating empty space around the child.
Just include Container
as the child of GestureDetector
instead of the Icon
and set behavior
property of GestureDetector
to HitTestBehavior.opaque
like this
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
print('clicked');
},
child : Container(
padding: EdgeInsets.all(padding),
decoration: BoxDecoration(border: Border.all()),
child: Icon(
Icons.info_outline,
color: Colors.blue,
size: 20,
)
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With