Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make padding of container tap-able?

Tags:

flutter

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?

like image 821
Əlişiram Avatar asked Apr 15 '19 12:04

Əlişiram


People also ask

How do you put a padding on a container?

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.

How do you add a padding to a container in Flutter?

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).

What is padding in Dart?

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.


1 Answers

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,
                  )
                ),
              );
            }
like image 50
Keerti Purswani Avatar answered Oct 21 '22 19:10

Keerti Purswani