Skip to content

Instantly share code, notes, and snippets.

@iam111
Last active October 4, 2023 10:36
Show Gist options
  • Save iam111/82485adf0f16ddcb3553a6d9d9bdfee8 to your computer and use it in GitHub Desktop.
Save iam111/82485adf0f16ddcb3553a6d9d9bdfee8 to your computer and use it in GitHub Desktop.
Generate sample list with flutter
import 'dart:math';
import 'package:flutter/material.dart';
class GenerateList extends StatefulWidget {
const GenerateList({super.key});
@override
State<GenerateList> createState() => _GenerateListState();
}
class _GenerateListState extends State<GenerateList> {
List<Map> generatedList = [];
@override
void initState() {
super.initState();
generateList();
}
void generateList() {
generatedList = List<Map>.generate(
20,
(i) => {
'username': 'tom$i',
'email': 'tom$i@example.com',
"mobiles": List<String>.generate(
2, (index) => "9999${Random().nextInt(1000000)}"),
"isOnline": Random().nextBool()
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Generate Random List"),
),
body: SingleChildScrollView(
child: Column(
children: [
generatedList.isNotEmpty
? ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: generatedList.length,
itemBuilder: (context, index) => Column(
children: [
Column(
children: [
ListTile(
leading: const Icon(Icons.account_circle),
title: Text(generatedList[index]["username"]),
trailing: Icon(
Icons.circle,
color: generatedList[index]["isOnline"]
? Colors.green
: Colors.red,
size: 15,
),
subtitle: Text(generatedList[index]["email"]),
)
],
),
const SizedBox(
height: 10,
),
],
))
: Container(),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment