Skip to content

Commit 45e6daf

Browse files
authored
Multiselect with Checkboxes added
1 parent b4d0ef0 commit 45e6daf

1 file changed

Lines changed: 165 additions & 0 deletions

File tree

data/draft/flutter.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,3 +1012,168 @@ List<Widget> indicators(imagesLength, currentIndex) {
10121012
});
10131013
}
10141014
```
1015+
## Multiselect with Checkboxes
1016+
```dart
1017+
// main.dart
1018+
import 'package:flutter/material.dart';
1019+
1020+
void main() {
1021+
runApp(const MyApp());
1022+
}
1023+
1024+
class MyApp extends StatelessWidget {
1025+
const MyApp({Key? key}) : super(key: key);
1026+
1027+
@override
1028+
Widget build(BuildContext context) {
1029+
return MaterialApp(
1030+
debugShowCheckedModeBanner: false,
1031+
title: 'KindaCode.com',
1032+
theme: ThemeData(
1033+
// enable Material 3
1034+
useMaterial3: true,
1035+
primarySwatch: Colors.indigo,
1036+
),
1037+
home: const HomePage(),
1038+
);
1039+
}
1040+
}
1041+
1042+
// Multi Select widget
1043+
// This widget is reusable
1044+
class MultiSelect extends StatefulWidget {
1045+
final List<String> items;
1046+
const MultiSelect({Key? key, required this.items}) : super(key: key);
1047+
1048+
@override
1049+
State<StatefulWidget> createState() => _MultiSelectState();
1050+
}
1051+
1052+
class _MultiSelectState extends State<MultiSelect> {
1053+
// this variable holds the selected items
1054+
final List<String> _selectedItems = [];
1055+
1056+
// This function is triggered when a checkbox is checked or unchecked
1057+
void _itemChange(String itemValue, bool isSelected) {
1058+
setState(() {
1059+
if (isSelected) {
1060+
_selectedItems.add(itemValue);
1061+
} else {
1062+
_selectedItems.remove(itemValue);
1063+
}
1064+
});
1065+
}
1066+
1067+
// this function is called when the Cancel button is pressed
1068+
void _cancel() {
1069+
Navigator.pop(context);
1070+
}
1071+
1072+
// this function is called when the Submit button is tapped
1073+
void _submit() {
1074+
Navigator.pop(context, _selectedItems);
1075+
}
1076+
1077+
@override
1078+
Widget build(BuildContext context) {
1079+
return AlertDialog(
1080+
title: const Text('Select Topics'),
1081+
content: SingleChildScrollView(
1082+
child: ListBody(
1083+
children: widget.items
1084+
.map((item) => CheckboxListTile(
1085+
value: _selectedItems.contains(item),
1086+
title: Text(item),
1087+
controlAffinity: ListTileControlAffinity.leading,
1088+
onChanged: (isChecked) => _itemChange(item, isChecked!),
1089+
))
1090+
.toList(),
1091+
),
1092+
),
1093+
actions: [
1094+
TextButton(
1095+
onPressed: _cancel,
1096+
child: const Text('Cancel'),
1097+
),
1098+
ElevatedButton(
1099+
onPressed: _submit,
1100+
child: const Text('Submit'),
1101+
),
1102+
],
1103+
);
1104+
}
1105+
}
1106+
1107+
// Implement a multi select on the Home screen
1108+
class HomePage extends StatefulWidget {
1109+
const HomePage({Key? key}) : super(key: key);
1110+
1111+
@override
1112+
State<HomePage> createState() => _HomePageState();
1113+
}
1114+
1115+
class _HomePageState extends State<HomePage> {
1116+
List<String> _selectedItems = [];
1117+
1118+
void _showMultiSelect() async {
1119+
// a list of selectable items
1120+
// these items can be hard-coded or dynamically fetched from a database/API
1121+
final List<String> items = [
1122+
'Flutter',
1123+
'Node.js',
1124+
'React Native',
1125+
'Java',
1126+
'Docker',
1127+
'MySQL'
1128+
];
1129+
1130+
final List<String>? results = await showDialog(
1131+
context: context,
1132+
builder: (BuildContext context) {
1133+
return MultiSelect(items: items);
1134+
},
1135+
);
1136+
1137+
// Update UI
1138+
if (results != null) {
1139+
setState(() {
1140+
_selectedItems = results;
1141+
});
1142+
}
1143+
}
1144+
1145+
@override
1146+
Widget build(BuildContext context) {
1147+
return Scaffold(
1148+
appBar: AppBar(
1149+
title: const Text('Multi Select'),
1150+
),
1151+
body: Padding(
1152+
padding: const EdgeInsets.all(20),
1153+
child: Column(
1154+
crossAxisAlignment: CrossAxisAlignment.start,
1155+
children: [
1156+
// use this button to open the multi-select dialog
1157+
ElevatedButton(
1158+
onPressed: _showMultiSelect,
1159+
child: const Text('Select Your Favorite Topics'),
1160+
),
1161+
const Divider(
1162+
height: 30,
1163+
),
1164+
// display selected items
1165+
Wrap(
1166+
children: _selectedItems
1167+
.map((e) => Chip(
1168+
label: Text(e),
1169+
))
1170+
.toList(),
1171+
)
1172+
],
1173+
),
1174+
),
1175+
);
1176+
}
1177+
}
1178+
```
1179+

0 commit comments

Comments
 (0)