Flutter Icons Widget

The Flutter icons widget is a fundamental component that provides symbolic representations to enhance user interfaces. Icons communicate functionality through visual metaphors, making apps more intuitive and user-friendly.

In Flutter, icons are primarily implemented using the Icon widget, which renders vector graphics from icon fonts. This approach ensures that Flutter icons remain crisp and clear at any resolution.

// Basic Flutter icons widget implementation  
Icon(  
  Icons.favorite,  
  color: Colors.red,  
  size: 24.0,  
)  

Material Design Icons in Flutter

Material Design icons are the default icon set in Flutter, accessible through the Icons class. These Flutter icons follow Google's Material Design guidelines and are available without additional package dependencies.

// Material Design Flutter icons widget examples  
Column(  
  children: [  
    Icon(Icons.home, size: 30),  
    Icon(Icons.settings, size: 30),  
    Icon(Icons.account_circle, size: 30),  
    Icon(Icons.notifications, size: 30),  
    Icon(Icons.menu, size: 30),  
  ],  
)  

Material Design provides over 1,000 Flutter icons in various categories:

  • Navigation icons (back, menu, more)
  • Action icons (search, favorite, share)
  • Communication icons (email, message, call)
  • Content icons (create, delete, save)
  • Toggle icons (check box, radio button, switch)
  • And many more

The complete set of Material Flutter icons can be found in the Material Design Icons library.

Cupertino Icons in Flutter

For iOS-styled applications, Flutter provides Cupertino icons that match Apple's design language. To use these Flutter icons, you need to include the cupertino_icons package in your pubspec.yaml file:

dependencies:  
  flutter:  
    sdk: flutter  
  cupertino_icons: ^1.0.5  

Then you can use Cupertino icons in your Flutter application:

// Cupertino Flutter icons widget example  
Icon(  
  CupertinoIcons.heart_fill,  
  color: Colors.red,  
  size: 24.0,  
)  

Common Cupertino Flutter icons include:

// Various Cupertino Flutter icons widget examples  
Row(  
  mainAxisAlignment: MainAxisAlignment.spaceAround,  
  children: [  
    Icon(CupertinoIcons.home),  
    Icon(CupertinoIcons.settings),  
    Icon(CupertinoIcons.person),  
    Icon(CupertinoIcons.bell),  
    Icon(CupertinoIcons.ellipsis),  
  ],  
)  

Icon Widget Properties

The Flutter icons widget comes with several properties that allow for extensive customization:

PropertyTypeDescription
iconIconDataThe icon to display (required)
sizedoubleThe size of the icon (default: 24.0)
colorColorThe color of the icon
semanticLabelStringSemantic label for accessibility
textDirectionTextDirectionDirection for rendering the icon
shadowsList<Shadow>List of shadows applied to the icon

Here's how to use these properties with the Flutter icons widget:

// Flutter icons widget with custom properties  
Icon(  
  Icons.lightbulb_outline,  
  size: 36.0,  
  color: Colors.amber,  
  semanticLabel: 'Light bulb icon',  
  shadows: [  
    Shadow(  
      blurRadius: 4.0,  
      color: Colors.black.withOpacity(0.3),  
      offset: Offset(2.0, 2.0),  
    ),  
  ],  
)  

Working with Icon Data

The IconData class is the foundation of Flutter icons, defining the font, codepoint, and other characteristics. You can create custom IconData objects for your own Flutter icons:

// Custom IconData for Flutter icons widget  
const IconData customIcon = IconData(  
  0xe800,  // Unicode code point  
  fontFamily: 'MyCustomIcons',  
  fontPackage: 'my_icon_package',  
);  
  
// Using custom IconData with Flutter icons widget  
Icon(customIcon)  

Custom Icons in Flutter

You can create and use custom Flutter icons by following these steps:

  1. Generate an icon font using tools like FlutterIcon or Fontello
  2. Add the generated font files to your Flutter project
  3. Register the font in your pubspec.yaml file
  4. Create a class to access your custom Flutter icons

Here's an example of registering a custom icon font:

# In pubspec.yaml  
flutter:  
  fonts:  
    - family: MyCustomIcons  
      fonts:  
        - asset: fonts/MyCustomIcons.ttf  

And here's how to create a class for your custom Flutter icons:

// Custom Flutter icons widget class  
class MyCustomIcons {  
  static const _kFontFam = 'MyCustomIcons';  
  static const String? _kFontPkg = null;  
  
  static const IconData custom_icon1 = IconData(0xe800, fontFamily: _kFontFam, fontPackage: _kFontPkg);  
  static const IconData custom_icon2 = IconData(0xe801, fontFamily: _kFontFam, fontPackage: _kFontPkg);  
  static const IconData custom_icon3 = IconData(0xe802, fontFamily: _kFontFam, fontPackage: _kFontPkg);  
}  
  
// Using custom Flutter icons widget  
Icon(MyCustomIcons.custom_icon1)  

Font Awesome Icons in Flutter

Font Awesome provides a popular icon set that can be integrated into Flutter applications. To use Font Awesome Flutter icons, add the font_awesome_flutter package:

dependencies:  
  flutter:  
    sdk: flutter  
  font_awesome_flutter: ^10.5.0  

Then import and use Font Awesome Flutter icons in your code:

import 'package:font_awesome_flutter/font_awesome_flutter.dart';  
  
// Font Awesome Flutter icons widget examples  
Row(  
  mainAxisAlignment: MainAxisAlignment.spaceAround,  
  children: [  
    FaIcon(FontAwesomeIcons.github),  
    FaIcon(FontAwesomeIcons.twitter),  
    FaIcon(FontAwesomeIcons.facebook),  
    FaIcon(FontAwesomeIcons.instagram),  
    FaIcon(FontAwesomeIcons.linkedin),  
  ],  
)  

The FaIcon widget is similar to the standard Flutter icons widget but is specifically designed for Font Awesome icons:

// Font Awesome Flutter icons widget with properties  
FaIcon(  
  FontAwesomeIcons.heart,  
  size: 24.0,  
  color: Colors.red,  
)  

Icon Buttons in Flutter

The IconButton widget combines Flutter icons with button functionality, creating tappable icon elements:

// Flutter icons widget as a button  
IconButton(  
  icon: Icon(Icons.thumb_up),  
  tooltip: 'Like',  
  onPressed: () {  
    // Handle button press  
    print('Like button pressed');  
  },  
)  

Key properties of the IconButton widget include:

PropertyTypeDescription
iconWidgetThe icon to display (usually an Icon widget)
onPressedVoidCallbackCallback function when button is pressed
tooltipStringText displayed when long-pressing the button
colorColorThe color of the icon
splashColorColorThe splash color when the button is pressed
highlightColorColorThe highlight color when the button is focused
paddingEdgeInsetsGeometryThe padding around the button
constraintsBoxConstraintsThe size constraints of the button

Example of a styled IconButton with Flutter icons:

// Styled Flutter icons widget button  
IconButton(  
  icon: Icon(Icons.favorite),  
  color: Colors.red,  
  splashColor: Colors.redAccent,  
  tooltip: 'Add to favorites',  
  padding: EdgeInsets.all(16.0),  
  constraints: BoxConstraints(minWidth: 48.0, minHeight: 48.0),  
  onPressed: () {  
    // Handle button press  
  },  
)  

Animated Icons in Flutter

Flutter provides animated transitions between icons through the AnimatedIcon widget, adding dynamic visual feedback to your UI:

// Animated Flutter icons widget  
AnimatedIcon(  
  icon: AnimatedIcons.menu_close,  
  progress: _animationController,  
  size: 30.0,  
  color: Colors.blue,  
)  

To use animated Flutter icons, you need to create an AnimationController:

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {  
  late AnimationController _animationController;  
  bool _isMenuOpen = false;  
  
  @override  
  void initState() {  
    super.initState();  
    _animationController = AnimationController(  
      vsync: this,  
      duration: Duration(milliseconds: 300),  
    );  
  }  
  
  @override  
  void dispose() {  
    _animationController.dispose();  
    super.dispose();  
  }  
  
  void _toggleMenu() {  
    setState(() {  
      _isMenuOpen = !_isMenuOpen;  
      if (_isMenuOpen) {  
        _animationController.forward();  
      } else {  
        _animationController.reverse();  
      }  
    });  
  }  
  
  @override  
  Widget build(BuildContext context) {  
    return Scaffold(  
      appBar: AppBar(  
        title: Text('Animated Icons Demo'),  
      ),  
      body: Center(  
        child: GestureDetector(  
          onTap: _toggleMenu,  
          child: AnimatedIcon(  
            icon: AnimatedIcons.menu_close,  
            progress: _animationController,  
            size: 50.0,  
            color: Colors.blue,  
          ),  
        ),  
      ),  
    );  
  }  
}  

Available animated Flutter icons include:

  • AnimatedIcons.add_event
  • AnimatedIcons.arrow_menu
  • AnimatedIcons.close_menu
  • AnimatedIcons.ellipsis_search
  • AnimatedIcons.event_add
  • AnimatedIcons.home_menu
  • AnimatedIcons.list_view
  • AnimatedIcons.menu_arrow
  • AnimatedIcons.menu_close
  • AnimatedIcons.menu_home
  • AnimatedIcons.pause_play
  • AnimatedIcons.play_pause
  • AnimatedIcons.search_ellipsis
  • AnimatedIcons.view_list

Best Practices for Flutter Icons

To make the most of Flutter icons widget in your applications, follow these best practices:

  1. Maintain Consistent Sizing: Use a consistent size for similar Flutter icons throughout your app.
// Consistent Flutter icons widget sizing  
const double kIconSize = 24.0;  
  
Icon(Icons.home, size: kIconSize),  
Icon(Icons.settings, size: kIconSize),  
Icon(Icons.person, size: kIconSize),  
  1. Use Semantic Labels: Add semantic labels to your Flutter icons for accessibility.
// Accessible Flutter icons widget  
Icon(  
  Icons.volume_up,  
  semanticLabel: 'Increase volume',  
)  
  1. Color Consistency: Match your Flutter icons colors with your app's theme.
// Theme-consistent Flutter icons widget  
Icon(  
  Icons.favorite,  
  color: Theme.of(context).primaryColor,  
)  
  1. Size Adaptation: Scale your Flutter icons based on the device's screen size or text scale factor.
// Responsive Flutter icons widget  
Icon(  
  Icons.touch_app,  
  size: 24.0 * MediaQuery.of(context).textScaleFactor,  
)  
  1. Pair with Text: When necessary, pair Flutter icons with text labels for clarity.
// Flutter icons widget with text  
Row(  
  children: [  
    Icon(Icons.save),  
    SizedBox(width: 8.0),  
    Text('Save'),  
  ],  
)  

Related Topics