Featured Image
Software Development

Flutter Widget In Focus — Chip (Know It All)

Photo by DDP on Unsplash

This blog post explores the Chip widget, a small but mighty component of the Flutter framework, which you might have already observed but not fully noticed.

 We all use the Gmail app in our day-to-day life, and you may have seen Chips in action while composing an email.

Gmail App from Google

 

Gmail App from Google

So when we select any name from the list, It gets transformed into something that is visually very compact but shows details of what exactly selected.

That is called ‘Chip

Chips are compact elements that represent an input, attribute, or action.

Basic Chip widget in Flutter looks like…

 
Flutter chip demo

 

UI structure

 
UI structure – Chip

 

Chip(
  avatar: CircleAvatar(
    backgroundColor: Theme.of(context).accentColor,
    child: Text('D'),
  ),
  label: Text('David'),
)

Let’s see some of its properties quickly –

ⓛabel: required

It accepts any widget but, Mostly it is good to have Text widget represting name of any entity,action etc.

 
Example of a basic Chip with a text label.

 

ⓐvatar:

It accepts any widget and can be used to show image, intial letter of any entity,action etc.

 
Illustration of a Chip with an avatar showing an initial letter.

 

ⓑackgroundColor:

Showing background color of chip when it is unselected and enabled.

 
Chip with custom background color when unselected.

 

ⓛabelStyle:

Used to apply any TextStyle to the label.

 
Chip demonstrating custom text style applied to the label.

 

ⓛabelPadding:

Adds some padding around the label

 
Chip with added padding around the label for better visual spacing.

 

ⓔlevation:

It accepts double value and used to elevate the chip

 
Chip displayed with shadow effect indicating elevation.

 

ⓢhadowColor:

Gives color to the shadow of the chip when elevation is set

 
Chip with a colored shadow to showcase the shadowColor property.

 

ⓓeleteIcon:

Used to display delete icon. This will be only visible when onDeleted is set.

 
Chip featuring a delete icon, indicating it can be removed.

 

ⓞnDeleted:

It’s a callback when delete icon is pressed inside the chip.

 
Visual cue on a Chip suggesting the delete action callback.

 

ⓓeleteIconColor:

Color to be set for delete icon

 
Chip with a delete icon highlighted in a custom color.

 

ⓓeleteButtonTooltipMessage:

Set a message to appear on tooltip of delete icon.

 
Chip with a tooltip message appearing.

 

ⓢhape:

Can be used to give shapes to the border of the chip. something like this.

 
Chip with a custom border shape to demonstrate the shape property.

 

Congratulations!! You have got the basic idea about how Chip widget works in Flutter.

Now let’s see types of chip

➊ Input chips

Input chips represent the information that was presented as part of the selection.

A very common example of this is already shown at the start of this story. i.e Gmail’s composing screen.

It can also be used for showing suggested responses in any messaging app. So that when the user clicks on it, the message contained in the input chip goes as a response.

Example of Input chip with onPressed event.

 
Example of Input chip with onPressed event.

 

InputChip(
  avatar: CircleAvatar(
    backgroundColor: Theme.of(context).accentColor,
    child: Text('D'),
  ),
  label: Text('David'),
  onPressed: () {
    print('David got clicked');
  },
)

➋ Choice chips

The choice chip allows selecting a single chip from the list. Sometimes it can replace the other widgets which allow single selection like toggle button and radio button.

Example of Choice chip with onSelected event.

 
Example of Choice chip with onSelected event.

 

Wrap(
  children: [
    ChoiceChip(
      label: Text('Small'),
      selected: false,
      onSelected: (bool selected) {
        setState(() {});
      },
    ),
    SizedBox(
      width: 10,
    ),
    ChoiceChip(
      label: Text('Big'),
      selected: true,
      onSelected: (bool selected) {
        setState(() {});
      },
      selectedColor: Theme.of(context).accentColor,
    ),
  ],
)

➌ Filter chips

Filter chip contains options, attributes, categories so that users can filter from it. You can easily replace the checkbox with Filter chip.

Example of Filter chip with onSelected event.

 
Example of Filter chip with onSelected event.

 

Wrap(
  children: [
    FilterChip(
      label: Text('Popcorn'),
      selected: false,
      onSelected: (bool selected) {
        setState(() {});
      },
    ),
    SizedBox(
      width: 10,
    ),
    FilterChip(
      label: Text('Coke'),
      labelStyle: TextStyle(
          color: widget.isSelected ? Colors.black : Colors.white),
      selected: widget.isSelected,
      onSelected: (bool selected) {
        setState(() {
          widget.isSelected = !widget.isSelected;
        });
      },
      selectedColor: Theme.of(context).accentColor,
      checkmarkColor: Colors.black,
    ),
  ],
)

➍ Action chips

Action chips can be used to invoke some action on primary content. It can not be disabled because if it is not needed then it should not even available to users.

For example, the user can take action like playing or downloading the song.

 
Example of Action chip

 

Wrap(
  children: [
    ActionChip(
      label: Text('Play'),
      onPressed: () {
        //TODO
      },
    ),
    SizedBox(
      width: 10,
    ),
    ActionChip(
      avatar:
          widget.isSelected ? CircularProgressIndicator() : null,
      label: Text(
          '${widget.isSelected ? 'Downloading...' : 'Download'}'),
      labelStyle: TextStyle(color: Colors.white),
      onPressed: () {
        setState(() {
          widget.isSelected = !widget.isSelected;
        });
      },
    ),
  ],
)

That looks like you have learned a lot today.

Full code can be found here

Conclusion:

 The Chip widget, which we discussed in this post, is a vital and multifaceted component of the Flutter toolkit that allows developers to make UI components interactive and dynamic with ease. From input and choice chips to filter and action chips, the possibilities are as limitless as your creativity.

Remember, the Chip widget isn’t just about aesthetics; it’s about functionality and user experience. By implementing Chips thoughtfully, you can significantly enhance the way users interact with your app, making the experience both enjoyable and efficient.

Are you ready to take your app to the next level? At Aubergine Solutions, we specialize in crafting custom solutions that stand out in the digital landscape. Whether you’re looking to integrate the Chip widget into your existing app or seeking comprehensive Flutter app development services, our team of experts is here to transform your vision into reality. Contact us today.

 

If you enjoyed reading this blog, then do explore our other posts too:

Mastering Dart’s Extension Methods for Enhanced Flutter Development

Designing a Custom Notebook Page in Flutter with CustomPaint

Quick Guide to Crafting an Onboarding Page Indicator in Flutter

Building an Interactive Superhero App with Flutter

 
author
Pinkesh Darji
I love to solve problems using technology that improves user’s life on a major scale. Over the last several years, I have been developing and leading various mobile apps in different areas. More than just programming, I love to write technical articles. I have written many high-quality technical articles. I have worked with various startups to build their dream app. I have been involved in product development since my early days and know insights into it. I have provided my valuable input while taking some crucial decisions of the app by brainstorming with a design, QA team. Over the last 3 years, I have been developing mobile apps and libraries using Google’s Flutter framework. I mentor junior Flutter developers and review their code. You will also find me talking on various Flutter topics in local meetups.