How do I clip clickable area of my container? - flutter

Whole container is getting clicked, other than circle shouldn't be clicked.
I'm shaping my container to circle.
What I want to do
Here's my code
InkWell(
onTap: () {},
hoverColor: Colors.yellow,
child: Container(
width: 300,
height: 300,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.red,
),
),
)

You need to use customBorder by providing CircleBorder().
InkWell(
customBorder: const CircleBorder(),
But for this case, you need to wrap with Material widget with CircleBorder().
Run on dartPad
The complete widget will be like
Material(
shape: const CircleBorder(),
color: Colors.red, // container color,to have splash color effect
child: InkWell(
customBorder: const CircleBorder(),
splashColor: Colors.white,
onTap: () {
debugPrint("tapped");
},
hoverColor: Colors.yellow,
child: Container(
width: 300,
height: 300,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent, // material color will cover this
),
),
),
),
Reference answer.

You can wrap the child of your container with a stack and then give 2 children: one square container and one circular container both with the same colour and then wrap the circular container with a gesture detector or inkwell.
If tough to understand by only text let me know i will write the code snippet for you.
Do upvote if helpful.

Use this
Container(
width: 300,
height: 300,
color: Colors.yellow,
child: GestureDetector(
onTap: () {},
child: Container(
width: 300,
height: 300,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.red,
),
),
),
),

You can populate the borderRadius property of the InkWell like this:
InkWell(
borderRadius: BorderRadius.circular('Your customized double value'),
onTap: () {},
hoverColor: Colors.yellow,
child: Container(
width: 300,
height: 300,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.red,
),
),
)

This will solve your problem;
InkWell(
customBorder: const CircleBorder(),
But if you want also material ripple effect on your red container you can use this code;
ClipOval(
child: Container(
width: 300,
height: 300,
color: Colors.red,
child: TextButton(
onPressed: () {},
child: Center(),
),
),
),

Related

How to add ripple effect to a Container with decoration in Flutter?

I want to add a ripple effect to a custom Container. I have used Inkwell and Material widgets to achieve this behavior, but it does not work. The code is as follows:
#override
Widget build(BuildContext context) {
return Center(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: (){},
child: Container(
width: 150,
height: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 2),
borderRadius: BorderRadius.circular(18),
color: Colors.deepPurpleAccent,
),
alignment: Alignment.center,
child: Text(
"A box container",
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
The result is:
I also used a transparent color on Container and the purple color on Material as follows:
#override
Widget build(BuildContext context) {
return Center(
child: Material(
color: Colors.deepPurpleAccent,
child: InkWell(
onTap: () {},
child: Container(
width: 150,
height: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 2),
borderRadius: BorderRadius.circular(18),
color: Colors.transparent,
),
alignment: Alignment.center,
child: Text(
"A box container",
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
As a result, the ripple effect occurs, but the Container looks like this (not what I want):
I also swapped the Container and Material widgets with each other, applied clip on the Container as follows:
#override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 150,
height: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 2),
borderRadius: BorderRadius.circular(18),
color: Colors.transparent,
),
clipBehavior: Clip.hardEdge,
child: Material(
color: Colors.deepPurpleAccent,
child: InkWell(
onTap: () {},
child: Center(
child: Text(
"A box container",
style: TextStyle(color: Colors.white),
),
),
),
),
),
);
}
Again, the ripple effect occurs, but the Container does not look as I want to (you can spot the little difference between the following photo and the first photo, in the edges of the Container):
I want the Container to look exactly as in the first photo with a ripple effect.
Note: The behavior that I want, can be exactly achieved through using Ink and Inkwell widgets, but when used in a ListView, it causes item render problems.
You were very close. You needed to have borderRadius on Material and InkWell. Try it this way
child: Material(
color: Colors.deepPurpleAccent,
borderRadius: BorderRadius.circular(18),
child: InkWell(
borderRadius: BorderRadius.circular(18),
onTap: () {},
child: Container(
width: 150,
height: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 2),
borderRadius: BorderRadius.circular(18),
color: Colors.transparent,
),
alignment: Alignment.center,
child: Text(
"A box container",
style: TextStyle(color: Colors.white),
),
),
),
),

Flutter- adjust the custom button tap area

Widget customButton() {
return Container(
child: InkWell(
onTap: () {},
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.amber,
shape: BoxShape.circle,
),
child: Icon(Icons.auto_fix_normal),
),
),
);
}
I'm trying to create custom button with inkwell, but button can tapping from the outside of the button . How i can adjust the button tap area ?
Using customBorder: CircleBorder(), on InkWell fixed the splash effect, but to work it properly I am extending the snippet.
Widget customButton() {
return Container(
decoration: const BoxDecoration(
shape: BoxShape.circle,
),
child: Material(
color: Colors.orange,
shape: const CircleBorder(),
child: InkWell(
splashColor: Colors.black,
onTap: () {},
customBorder: const CircleBorder(),
child: Ink(
decoration: const BoxDecoration(shape: BoxShape.circle),
height: 100,
width: 100,
child: const Icon(Icons.holiday_village),
),
),
),
);
}

Flutter: making InkWell circular

Container(
decoration: BoxDecoration(shape: BoxShape.circle),
child: Material(
color: Colors.orange,
child: InkWell(
splashColor: Colors.black,
onTap: () {},
child: Ink(
decoration: BoxDecoration(shape: BoxShape.circle),
height: Get.height * 0.0425,
width: Get.height * 0.0425,
child: Icon(Icons.holiday_village),
),
),
),
),
I want to make this InkWell stuff circular shape. Nothing seems to make it circular. If I take out Material(), then it shows no background color nor the splash color does not appear. How can I reshape this Contaner - InkWell circular to make sure that the button is circular shape with the splashColor?
Use customBorder: CircleBorder(), on InkWell and
shape: const CircleBorder(), on Material
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
),
child: Material(
color: Colors.orange,
shape: const CircleBorder(),
child: InkWell(
splashColor: Colors.black,
onTap: () {},
customBorder: const CircleBorder(),
child: Ink(
decoration: const BoxDecoration(shape: BoxShape.circle),
height: 425,
width: 425,
child: const Icon(Icons.holiday_village),
),
),
),
),

Flutter: How to create round/circular button

I am not able to any sample for creating a circular button like
FloatingActionButton? Is there any way to create it?
Below code creates a rectangular button, is there any for Circular Button?
shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
You can use the MaterialButton widget.
It's shape field can be set to CircleBorder value and that will give you a circular button.
For example, I've modified the code of one of my projects views where I have rounded rectangle buttons to make one of them circular:
Container(
width: 80,
height: 80,
child: MaterialButton(
shape: CircleBorder(
side: BorderSide(
width: 2,
color: Colors.red,
style: BorderStyle.solid,
),
),
child: Text("Login"),
color: Colors.teal,
textColor: Colors.amber,
onPressed: (){},
),
),
Screenshot:
Container(
width: 90,
height: 90,
child: RaisedButton(onPressed: (){
},
color: Colors.deepOrange,
textColor: Colors.white,
shape: CircleBorder(side: BorderSide.none),
child: Text('Login',style: TextStyle(
fontSize: 20.0
),
),
),
)
We can create a circular button by FloatingActionButton widget.
return Container(
alignment: Alignment.center,
child: new SizedBox(
child: FloatingActionButton(
backgroundColor: Colors.red,
child: null,
onPressed: () {
print("Cliked");
},)
));
}
Output:

Flutter: a customized widget that acts as a FAB, Inkwell does not respect the circular shape and ripples are rectangular

For adding a glow effect on a button I have "Frankenstein-ed" this code:
floatingActionButton: Container(
decoration: BoxDecoration(boxShadow: [
new BoxShadow(
color: Colors.pinkAccent,
blurRadius: 50.0,
),
]),
child: Material(
shape: CircleBorder(),
color: Colors.pink,
child: InkWell(
onTap: (){print('tap');},
child: Padding(
padding: const EdgeInsets.all(20.0), child: Icon(Icons.add)),
),
), //Icon(Icons.add),
),
the shape in Material is circular, but the ripple effect that are handled by Inkwell goes outside the circle shape.
I have tried wrapping Inkwell by a Container:
Container(
decoration: BoxDecoration(shape: BoxShape.circle),
child: InkWell(
onTap: (){print('tap');},
child: Padding(
padding: const EdgeInsets.all(20.0), child: Icon(Icons.add)),
),
),
and wrapping Padding by a Container:
floatingActionButton: Container(
decoration: BoxDecoration(boxShadow: [
new BoxShadow(
color: Colors.pinkAccent,
blurRadius: 50.0,
),
]),
child: Material(
shape: CircleBorder(),
color: Colors.pink,
child: InkWell(
onTap: (){print('tap');},
child: Container(
decoration: BoxDecoration(shape: BoxShape.circle),
child: Padding(
padding: const EdgeInsets.all(20.0), child: Icon(Icons.add)),
),
),
), //Icon(Icons.add),
),
but no luck.
Edit after Gaspard Merten's answer:
trying this code does not change the ripple effect's behavior:
Material(
shape: CircleBorder(),
color: Colors.pink,
child: Container(
decoration: BoxDecoration(boxShadow: [
new BoxShadow(
color: Colors.pinkAccent,
blurRadius: 50.0,
),
]),
child: InkWell(
onTap: () {
onClick();
},
child: Container(
decoration: BoxDecoration(shape: BoxShape.circle),
child: Padding(
padding: const EdgeInsets.all(20.0), child: Icon(icon)),
),
), //Icon(Icons.add),
),
);
You can use ClipRRect and clip the material widget instead of the container:
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(7.0)),
child: YourMaterialButton
)
That's happening because the material widget is the direct ancestor of the inkwell. Try to put the Material widget before the container and specify de coloris and the border radius of the material and remove the color argument from the container.
You can use 'customBorder' property to make ripple shape circular irrespective of container shape !
InkWell(
customBorder: CircleBorder(),
onTap: () {}
child: ...
)