At the Flutter documentation, the recommended way for building an IconButton
widget with opaque colored background is the following:
class IconButtonExample extends StatelessWidget {
const IconButtonExample({super.key});
@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: Center(
child: Ink(
decoration: const ShapeDecoration(
color: Colors.lightBlue,
shape: CircleBorder(),
),
child: IconButton(
icon: const Icon(Icons.add),
color: Colors.white,
onPressed: () {},
),
),
),
);
}
}
However, if I want to decrease the size of the overall size of the button to 24, like the following:
child: Ink(
height: 24,
width: 24,
decoration: ShapeDecoration(
the icons is not centered anymore, although its content size would fit in the mentioned size.
Even decreasing the Icon
widget size itself to 14, don’t give a satisfying result, and using Center
widget and Alignment.center
also do not solve it.
I’m not willing to use FAB widget, because FAB literally is supposed to float in the screen. The widget I’m trying to achieve is supposed to exist as an generic widget.
4
Answers
24 is the iconSize and it left no space for default padding on IconButton.You can set zero padding or adjust the size
You could just create this button from scratch.
You can use
Transform.scale
to scale that whole widget or you can try to warp thatIconButton
withCircleAvatar
instead ofInk
.Material3 now provides
IconButton
,IconButton.filled
, andIconButton.filledTonal
widgets for flutter.To show an IconButton with a background color you can simply use the IconButton.filled widget. It has multiple options to change icon size, splash radius, splash color, etc.