How to Remove Iconbutton Padding in Flutter?

In this tutorial, we are going to learn to remove Iconbutton padding in Flutter using five different ways.

Each way is demonstrated with full source code to remove padding around the icon button so that you can easily copy and paste the code in your Flutter app.

Remove Iconbutton Padding in Flutter

5 Ways to Remove Iconbutton Padding in Flutter

Sure, here are complete code examples for each of the methods to remove the padding around an IconButton widget in Flutter:

1. Using the IconButton.iconSize property

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('IconButton Padding Example'),
        ),
        body: Center(
          child: IconButton(
            iconSize: 40.0, // Increase this value to reduce padding
            icon: Icon(Icons.add),
            onPressed: () {
              // Handle button press
            },
          ),
        ),
      ),
    );
  }
}
  • The IconButton widget has a iconSize property that allows you to control the size of the icon.
  • By setting the iconSize to a larger value, you can effectively reduce the padding around the icon.

2. Using the IconButton.padding property:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('IconButton Padding Example'),
        ),
        body: Center(
          child: IconButton(
            padding: EdgeInsets.zero, // Remove all padding
            icon: Icon(Icons.add),
            onPressed: () {
              // Handle button press
            },
          ),
        ),
      ),
    );
  }
}
  • The IconButton widget has a padding property that allows you to directly set the padding around the icon.
  • By setting the padding to a smaller value or EdgeInsets.zero, you can remove the padding.

3. Using the IconButton.constraints for Remove Iconbutton Padding in Flutter

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('IconButton Padding Example'),
        ),
        body: Center(
          child: IconButton(
            constraints: BoxConstraints(
              minWidth: 20.0, // Adjust these values to reduce padding
              minHeight: 20.0,
            ),
            icon: Icon(Icons.add),
            onPressed: () {
              // Handle button press
            },
          ),
        ),
      ),
    );
  }
}
  • The IconButton widget has a constraints property that allows you to specify the minimum and maximum size constraints for the button.
  • By setting the constraints to a smaller value, you can effectively reduce the padding around the icon.

4. Wrapping the IconButton with a Padding widget

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('IconButton Padding Example'),
        ),
        body: Center(
          child: Material(
            shape: CircleBorder(),
            clipBehavior: Clip.hardEdge,
            color: Colors.transparent,
            child: Ink(
              decoration: BoxDecoration(
                color: Colors.blue,
                shape: BoxShape.circle,
              ),
              child: IconButton(
                icon: Icon(Icons.add),
                color: Colors.white,
                onPressed: () {
                  // Handle button press
                },
              ),
            ),
          ),
        ),
      ),
    );
  }
}
  • You can wrap the IconButton with a Padding widget and set the padding property to a negative value.
  • This approach can remove the padding around the IconButton and adjust the spacing as needed.

5. Creating a custom IconButton widget

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('IconButton Padding Example'),
        ),
        body: Center(
          child: CustomIconButton(
            icon: Icons.add,
            onPressed: () {
              // Handle button press
            },
          ),
        ),
      ),
    );
  }
}

class CustomIconButton extends StatelessWidget {
  final IconData icon;
  final VoidCallback onPressed;

  CustomIconButton({
    required this.icon,
    required this.onPressed,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      child: Icon(icon),
    );
  }
}
  • You can create a custom IconButton widget by extending the StatelessWidget class and overriding the build method.
  • In the custom widget, you can remove the padding or adjust it according to your requirements.

Related Reading:

Output

Remove Iconbutton Padding in Flutter Output

Conclusion

In each example, the key part is either adjusting the properties of the IconButton widget or wrapping it with another widget to remove the padding. The Scaffold and Center widgets are used to position the IconButton in the center of the screen.

If you haven’t already installed Flutter, you can follow the official: Flutter Installation Guide. to set it up.

Share:
Ambika Dulal

Ambika Dulal is a Flutter Developer from Nepal who is passionate about building beautiful and user-friendly apps. She is always looking for new challenges and is eager to learn new things. She is also a strong believer in giving back to the community and is always willing to help others.

Leave a Comment

AO Logo

App Override is a leading mobile app development company based in Kathmandu, Nepal. Specializing in both Android and iOS app development, the company has established a strong reputation for delivering high-quality and innovative mobile solutions to clients across a range of industries.

Services

UI/UX Design

Custom App Development

Mobile Strategy Consulting

App Testing and Quality Assurance

App Maintenance and Support

App Marketing and Promotion

Contact

App Override

New Plaza, Kathmandu