Flutter - Custom button tap area

5,800

Solution 1

This seems to work, I don't know if it is right to do so or if there is a better way but here you go.

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        color: Colors.blue,
        child: Center(
          child: GestureDetector(
            onTap: () {
              print('clicky');
            },
            child: ClipOval(
              child: Container(
                width: 200,
                height: 200,
                color: Colors.red,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

Solution 2

If you want the keep the splash during the tap, you can do something like this:

    Material(
              color: Colors.green,
              shape: CircleBorder(),
              elevation: 5.0,
              child: InkWell(
                borderRadius: BorderRadius.circular(100.0),
                onTap: () => print("here"),
                child: Container(
                  height: 200.0,
                  width: 200.0,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                  ),
                  child: Icon(Icons.receipt),
                ),
              ),
            ),
Share:
5,800
Alessandro
Author by

Alessandro

Embedded Software Engineer Passionate about software development, striving to add quality. I'm a passionate software developer, and I've slowly learned what I really love about my job: every time I read or talk about Linux, C programming, Kernel, device drivers and other related topics, I feel thrilled. My daily work involves the Linux Kernel and, more broadly, the Android AOSP project. My long-term goal is to be able to contribute in a meaningful way to the mainline of the Linux Kernel and to the Linux world in general.

Updated on December 08, 2022

Comments

  • Alessandro
    Alessandro over 1 year

    I'm building a Flutter application where a big portion of the screen will be occupied by a circular button.

    I already tried several different approaches in order to create a circular button, but I always end up having the same problem: the 'tappable' area is not actually circular, but rectangular.

    Here is an example obtained with a FloatingActionButton:

    Example

    For small-sized buttons this is not really a problem - I would even say that it is somehow helpful - but in my case it is very annoying.

    So my question is: is it possible to restrict the 'tappable' area to a circle?

    Thanks in advance.

  • Alessandro
    Alessandro over 5 years
    Thanks, that's exactly what I was looking for.
  • Doc
    Doc over 5 years
    For material effect, replace GestureDetector with InkWell with onTap().