Open In App

Flutter – Chip Widget

Chip is a material design widget which comes built-in with flutter. It can simply be described as a compact element holding an icon and text, usually a rounded rectangle in the background. It can serve many purposes, like it can be simply used as a button, representing a user with a CircleAvatar and text, or topic tags in the blog articles, etc.

Constructor of Chip Class:

const Chip(
{Key key,
Widget avatar,
@required Widget label,
TextStyle labelStyle,
EdgeInsetsGeometry labelPadding,
Widget deleteIcon,
VoidCallback onDeleted,
Color deleteIconColor,
String deleteButtonTooltipMessage,
ShapeBorder shape,
Clip clipBehavior: Clip.none,
FocusNode focusNode,
bool autofocus: false,
Color backgroundColor,
EdgeInsetsGeometry padding,
VisualDensity visualDensity,
MaterialTapTargetSize materialTapTargetSize,
double elevation,
Color shadowColor}
)

Properties of Chip Widget:

Example: 






import 'package:flutter/material.dart';
 
//Material design library
void main() {
  runApp(
    //widget tree starts here
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('GeeksforGeeks'),
          backgroundColor: Colors.greenAccent[400],
          centerTitle: true,
        ), //AppBar
        body: Center(
          /** Chip Widget **/
          child: Chip(
            elevation: 20,
            padding: EdgeInsets.all(8),
            backgroundColor: Colors.greenAccent[100],
            shadowColor: Colors.black,
            avatar: CircleAvatar(
              backgroundImage: NetworkImage(
                  "https://pbs.twimg.com/profile_images/1304985167476523008/QNHrwL2q_400x400.jpg"), //NetworkImage
            ), //CircleAvatar
            label: Text(
              'GeeksforGeeks',
              style: TextStyle(fontSize: 20),
            ), //Text
          ), //Chip
        ), //Center
      ), //Scaffold
    ), //MaterialApp
  );
}

Output:



Explanation: In this flutter application the parent widget in the body is Center which is taking Chip widget as it child. Inside the chip widget the elevation property is set to 20 px which is making the Chip widget appear elevated from the background. Then we have the padding property adding 8 px empty space in the Chip. The backgroundColor is greenAccent[400] and the avatar is taking in CircleAvatar which is holding a gfg logo (NetworkImage). And at last the label property is holding text whose font-size is 20 px. Doing all this we get a nice looking Chip widget which can find it use in many places.


Article Tags :