Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ponnamkarthik/352ff3b64e6ce159eb8f2c6bd3966969 to your computer and use it in GitHub Desktop.
Save ponnamkarthik/352ff3b64e6ce159eb8f2c6bd3966969 to your computer and use it in GitHub Desktop.
Flutter Multi-select ChoiceChip with max selection
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> reportList = [
"Not relevant",
"Illegal",
"Spam",
"Offensive",
"Uncivil"
];
List<String> selectedReportList = [];
_showReportDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
//Here we will build the content of the dialog
return AlertDialog(
title: Text("Report Video"),
content: MultiSelectChip(
reportList,
onSelectionChanged: (selectedList) {
setState(() {
selectedReportList = selectedList;
});
},
maxSelection: 2,
),
actions: <Widget>[
FlatButton(
child: Text("Report"),
onPressed: () => Navigator.of(context).pop(),
)
],
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("Report"),
onPressed: () => _showReportDialog(),
),
Text(selectedReportList.join(" , ")),
],
),
),
);
}
}
class MultiSelectChip extends StatefulWidget {
final List<String> reportList;
final Function(List<String>)? onSelectionChanged;
final Function(List<String>)? onMaxSelected;
final int? maxSelection;
MultiSelectChip(this.reportList, {this.onSelectionChanged, this.onMaxSelected, this.maxSelection});
@override
_MultiSelectChipState createState() => _MultiSelectChipState();
}
class _MultiSelectChipState extends State<MultiSelectChip> {
// String selectedChoice = "";
List<String> selectedChoices = [];
_buildChoiceList() {
List<Widget> choices = [];
widget.reportList.forEach((item) {
choices.add(Container(
padding: const EdgeInsets.all(2.0),
child: ChoiceChip(
label: Text(item),
selected: selectedChoices.contains(item),
onSelected: (selected) {
if(selectedChoices.length == (widget.maxSelection ?? -1) && !selectedChoices.contains(item)) {
widget.onMaxSelected?.call(selectedChoices);
} else {
setState(() {
selectedChoices.contains(item)
? selectedChoices.remove(item)
: selectedChoices.add(item);
widget.onSelectionChanged?.call(selectedChoices);
});
}
},
),
));
});
return choices;
}
@override
Widget build(BuildContext context) {
return Wrap(
children: _buildChoiceList(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment