CheckboxListTile Dynamic

 import "package:flutter/material.dart";


void main()
{
runApp(MyApp());
}
class MyApp extends StatelessWidget
{
MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Dynamic checkboxListTile",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: DynamicTile(),
);
}
}
class DynamicTile extends StatefulWidget
{
DynamicTile({super.key});
@override
State<DynamicTile> createState()
{
return DynamicTileState();
}
}
class DynamicTileState extends State<DynamicTile>
{
List<Map> categories = [
{"name": "Swimming", "isChecked": false},
{"name": "Cycling", "isChecked": false},
{"name": "Tennis", "isChecked": false},
{"name": "Boxing", "isChecked": false},
{"name": "Volleyball", "isChecked": false},
{"name": "Bowling", "isChecked": false}
];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Dynamic CheckboxListTile"),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
const Text("Please Choose your favorite Category",
style: TextStyle(fontSize: 16.0),),
const SizedBox(height: 10.0),
const Divider(color: Colors.grey, thickness: 1),
const SizedBox(height: 10.0),
Column(
children: categories.map((favorite) {
return CheckboxListTile(
activeColor: Colors.blue,
checkboxShape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
title: Text(favorite["name"]),
value: favorite["isChecked"],
onChanged: (val) {
setState(() {
favorite["isChecked"] = val;
},);
},
);
},).toList(),
),
const SizedBox(height: 10.0),
const Divider(color: Colors.grey, thickness: 1),
const SizedBox(height: 10.0),
Wrap(
children: categories.map((favorite) {
if(favorite["isChecked"] == true)
{
return Card(
elevation: 3,
color: Colors.deepPurpleAccent,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
favorite["name"],
style: const TextStyle(color: Colors.white),
),
const SizedBox(width: 5.0),
IconButton(
onPressed: () {
setState(() {
favorite["isChecked"] = !favorite["isChecked"];
},);
},
icon: const Icon(Icons.delete, color: Colors.white),
),
],
),
),
);
}
return Container();
},).toList(),
),
],
),
),
),
);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter