Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Input Chip In Flutter

There are a few chips characterized by Material Design, one of which is the input chip. Input chips are commonly used to address client input in a conservative structure or give ideas to the client. Besides label and avtar, an input chip can likewise have a delete icon. In Flutter, you can make that sort of chip utilizing the InputChip widget.

This blog will explore the Input Chip In Flutter. We perceive how to execute a demo program. We will learn how to use the Input Chip class in your flutter applications.

InputChip class – material library – Dart API
A material design input chip. Input chips represent a complex piece of information, such as an entity (person, place…api. flutter.dev

Table Of Contents::

Introduction

Constructor

Properties

Code Implement

Code File

Conclusion



Introduction:

InputChip is a material widget in a flutter that addresses a mind-boggling snippet of data in a conservative structure. Flutter gives a widget called InputChip which permits us to add an input chip to our application.

The input chip by default is disabled. We can empower it by setting onSelected. We can give a label, and leading and trailing symbols to it. Different kinds of chips are Chip, ChoiceChip, ActionChip, and FilterChip.

Demo Module ::

This demo video shows how to use the input chip in a flutter and shows how an input chip will work in your flutter applications. We will show a user press the chip, then the chip will be selected, and also the user will delete the chip. It will be shown on your devices.

Constructor:

To utilize InputChip, you need to call the constructor underneath:

To make an Input chip in the flutter we need to utilize the constructor of the InputChip class given by the flutter.

const InputChip({
Key? key,
this.avatar,
required this.label,
this.labelStyle,
this.labelPadding,
this.selected = false,
this.isEnabled = true,
this.onSelected,
this.deleteIcon,
this.onDeleted,
this.deleteIconColor,
this.deleteButtonTooltipMessage,
this.onPressed,
this.pressElevation,
this.disabledColor,
this.selectedColor,
this.tooltip,
this.side,
this.shape,
this.clipBehavior = Clip.none,
this.focusNode,
this.autofocus = false,
this.backgroundColor,
this.padding,
this.visualDensity,
this.materialTapTargetSize,
this.elevation,
this.shadowColor,
this.selectedShadowColor,
this.showCheckmark,
this.checkmarkColor,
this.avatarBorder = const CircleBorder(),
@Deprecated(
'Migrate to deleteButtonTooltipMessage. '
'This feature was deprecated after v2.10.0-0.3.pre.'
)
this.useDeleteButtonTooltip = true,
})

One required property for the InputChip widget is the label property. The label can be any widget generally a text widget. To utilize the InputChip widget we need to furnish this property with a value.

Properties:

There are some properties of InputChip are:

  • > selected — This property is utilized to set the chip’s state to be chosen or unselected. It takes a boolean value setting that truly will make the chip chosen and false will make the chip unselected.
  • > onSelected — This property is utilized to update the selected or unselected state of the Input chip and perform some actions when the chip is selected or unselected.
  • > isEnabled — This property is utilized to enable or disable the input chip. It takes a boolean value. By default the value is true. Regardless of whether we set this property to true, we need to set one of the callbacks onSelected, onDelete, or onPressed to enable the button.
  • > disabledcolor — This property is utilized to apply a color to the inputChip when the chip is disabled we will utilize this property. So to disable the button I’m eliminating every one of the callbacks.
  • > showCheckmark — This property is utilized to show/hide away the check mark that seems when we select the chip. It takes a boolean value.
  • > pressElevation — This property is utilized to change the amount of elevation we want to apply when we press the chip.

How to implement code in dart file :

You need to implement it in your code respectively:

Create a new dart file called item_model.dart inside the lib folder.

First, we want an ItemModel class for holding the information of Inputchip. The ItemModel class will have three boundaries label, color, and isSelected. The label will hold the mark of the chip, the color will hold the backgroundColor and isSelected will hold the chosen or unselected condition of the Input chip.

import 'dart:ui';

class ItemModel {
String label;
Color color;
bool isSelected;

ItemModel(this.label, this.color, this.isSelected);
}

Create a new dart file called main.dart inside the lib folder.

In the main. dart file, we will create a new class MyHomePage(). In this class, we will first create a List of types ItemModel and provide the data for chips.

final List<ItemModel> _chips = [
ItemModel("Android", Colors.green, false),
ItemModel("Flutter", Colors.blueGrey, false),
ItemModel("Ios", Colors.deepOrange, false),
ItemModel("Python", Colors.cyan, false),
ItemModel("React JS", Colors.teal, false),
];

In the body, we will add the Column widget. In this widget, we will add an image and wrap widget. In this widget, we will add direction was horizontal and its children were itemsChips() method.

Center(
child: Column(
children: [
Image.asset(
"assets/logo.png",
height: 300,
width: 350,
),
Wrap(direction: Axis.horizontal, children: itemsChips()),
],
)),

Now we will deeply define itemsChips() method are:

This method was on the list of widgets. We will add the InputChip widget. In this widget, we will add an avatar, label, backgroundColor, selected, onDeleted, onSelected, and then return chips.

List<Widget> itemsChips() {
List<Widget> chips = [];
for (int i = 0; i < _chips.length; i++) {
Widget item = Padding(
padding: const EdgeInsets.only(left: 10, right: 5),
child: InputChip(
avatar: CircleAvatar(
backgroundColor: Colors.white,
child: Text(_chips[i].label[0].toUpperCase()),
),
label: Text(_chips[i].label),
labelStyle: const TextStyle(color: Colors.white),
backgroundColor: _chips[i].color,
selected: _chips[i].isSelected,
onDeleted: () {
setState(() {
_chips.removeAt(i);
});
},
onSelected: (bool value) {
setState(() {
_chips[i].isSelected = value;
});
},
),
);
chips.add(item);
}
return chips;
}

When we run the application, we ought to get the screen’s output like the underneath screen capture.

Final Output

Code File:

import 'package:flutter/material.dart';
import 'package:flutter_input_chip_demo/item_model.dart';
import 'package:flutter_input_chip_demo/splash_screen.dart';

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

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.red,
),
debugShowCheckedModeBanner: false,
home: const Splash(),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);

@override
_MyHomePageState createState() {
return _MyHomePageState();
}
}

class _MyHomePageState extends State<MyHomePage> {
final List<ItemModel> _chips = [
ItemModel("Android", Colors.green, false),
ItemModel("Flutter", Colors.blueGrey, false),
ItemModel("Ios", Colors.deepOrange, false),
ItemModel("Python", Colors.cyan, false),
ItemModel("React JS", Colors.teal, false),
];

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: const Text("Flutter Input Chip Demo"),
centerTitle: true,
automaticallyImplyLeading: false,
backgroundColor: Colors.orangeAccent,
),
body: Center(
child: Column(
children: [
Image.asset(
"assets/logo.png",
height: 300,
width: 350,
),
Wrap(direction: Axis.horizontal, children: itemsChips()),
],
)),
);
}

List<Widget> itemsChips() {
List<Widget> chips = [];
for (int i = 0; i < _chips.length; i++) {
Widget item = Padding(
padding: const EdgeInsets.only(left: 10, right: 5),
child: InputChip(
avatar: CircleAvatar(
backgroundColor: Colors.white,
child: Text(_chips[i].label[0].toUpperCase()),
),
label: Text(_chips[i].label),
labelStyle: const TextStyle(color: Colors.white),
backgroundColor: _chips[i].color,
selected: _chips[i].isSelected,
onDeleted: () {
setState(() {
_chips.removeAt(i);
});
},
onSelected: (bool value) {
setState(() {
_chips[i].isSelected = value;
});
},
),
);
chips.add(item);
}
return chips;
}
}

Conclusion:

In the article, I have explained the Input Chip basic structure in a flutter; you can modify this code according to your choice. This was a small introduction to Input Chip User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Input Chip in your flutter projectsWe will show you what the Introduction is and what are the construction and properties of the Input Chip, and make a demo program for working with Input Chip in your flutter applications. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.