Flutter- adjust the custom button tap area - flutter

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),
),
),
),
);
}

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

How to add highlighting border on buttonbar Flutter?

I have buttonbar with a few buttons, here is the code:
ButtonBar(
children: [
Container(
width: 40,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: <Color>[
Colors.purpleAccent,
Colors.pinkAccent,
],
stops: const <double>[
0.5,
0,
],
),
shape: BoxShape.circle,
),
child: ElevatedButton(
onPressed: () =>
setColors(Colors.purpleAccent, Colors.pinkAccent),
style: ElevatedButton.styleFrom(
shape: CircleBorder(),
primary: Colors.transparent,
shadowColor: Colors.transparent,
),
child: Ink())
),
...
)
I want to highlight selected button with border, something like this:
How can I do this with current architecture?
Try below code I have try using ListView.builder(), not ButtonBar hope its help to you.use StatefulWidget for below code
declare index and bool variable:
bool isPressed = false;
int? buttonIndex;
Your Widget:
Center(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 10,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.all(3),
padding: EdgeInsets.all(2),
height: 40,
width: 40,
decoration: BoxDecoration(
color: isPressed && buttonIndex == index
? Colors.primaries[index]
: Colors.transparent,
shape: BoxShape.circle,
),
child: Container(
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: ClipOval(
child: Container(
height: 40,
width: 40,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
backgroundColor: Colors.primaries[index],
shape: CircleBorder(),
),
onPressed: () {
setState(() {
isPressed = !isPressed;
buttonIndex = index;
});
},
child: const SizedBox(),
),
),
),
),
);
},
),
),
Result Screen normal/without button click->
Result Screen with button click->
Test above code on Dartpad

How do I clip clickable area of my container?

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(),
),
),
),

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