import 'package:flutter/material.dart';
class CollectionsDemo extends StatelessWidget {
final List<String> fruits = ['Apple', 'Banana', 'Orange'];
final Map<String, String> fruitColors = {
'Apple': 'Red',
'Banana': 'Yellow',
'Orange': 'Orange'
};
final Set<String> uniqueFruits = {'Apple', 'Banana', 'Orange'};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Collections Demo')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Fruits List:', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: fruits.length,
itemBuilder: (context, index) {
return Text('- ${fruits[index]}', style: TextStyle(fontSize: 16));
},
),
SizedBox(height: 20),
Text('Fruit Colors (Map):', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: fruitColors.entries.map((entry) => Text('${entry.key}: ${entry.value}', style: TextStyle(fontSize: 16))).toList(),
),
SizedBox(height: 20),
Text('Unique Fruits (Set):', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: uniqueFruits.map((fruit) => Text(fruit, style: TextStyle(fontSize: 16))).toList(),
),
],
),
),
),
);
}
}This Flutter screen shows three types of collections: a List, a Map, and a Set.
We use a List to hold fruit names and display them in a scrollable list with bullet points.
The Map holds fruit names as keys and their colors as values, displayed as key-value pairs.
The Set holds unique fruit names and is displayed as a simple list.
We use padding and a SingleChildScrollView to make sure the content fits well on the screen and can scroll if needed.