Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Custom button tap area

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.

like image 720
Alessandro Avatar asked Jan 13 '19 19:01

Alessandro


2 Answers

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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
like image 190
Doc Avatar answered Oct 28 '22 22:10

Doc


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),
                ),
              ),
            ),
like image 37
diegoveloper Avatar answered Oct 28 '22 23:10

diegoveloper