Skip to content

Instantly share code, notes, and snippets.

@vincevargadev
Created December 4, 2019 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincevargadev/d30690be2116e2ae7d78f60413a0f875 to your computer and use it in GitHub Desktop.
Save vincevargadev/d30690be2116e2ae7d78f60413a0f875 to your computer and use it in GitHub Desktop.
I tried out the new DartPad!
import 'dart:math' as math;
import 'package:flutter/material.dart';
final dscTeam = [
{
'name': 'Viviana Sutedjo',
'nickName': 'Vivi',
'color': getRandomColor(),
},
{
'name': 'Berzan Yildiz',
'nickName': 'Berzan',
'color': getRandomColor(),
},
{
'name': 'Florian Müller',
'nickName': 'Flo',
'color': getRandomColor(),
},
{
'name': 'Andreas Zimmerer',
'nickName': 'Andi',
'color': getRandomColor(),
},
];
final guests = [
{
'name': 'Vince Varga',
'nickName': 'Vince',
'color': getRandomColor(),
},
];
// Simple Dart function
Color getRandomColor() {
return Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1);
}
class DscList extends StatefulWidget {
@override
createState() => DscListState();
}
class DscListState extends State<DscList> {
final Map<String, int> tapCount = {};
@override
initState() {
super.initState();
List.from(dscTeam)
..addAll(guests)
..forEach((m) {
tapCount[m['name']] = 0;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: ListView(
padding: const EdgeInsets.all(16),
children: <Widget>[
Text('DSC Team', style: Theme.of(context).textTheme.title),
for (var t in dscTeam)
PersonTile(
name: t['name'],
nickName: t['nickName'],
color: t['color'],
onTap: () => handleTap(t['name']),
count: tapCount[t['name']],
),
Text('Guests', style: Theme.of(context).textTheme.title),
for (var g in guests)
PersonTile(
name: g['name'],
nickName: g['nickName'],
color: g['color'],
onTap: () => handleTap(g['name']),
count: tapCount[g['name']],
),
],
),
),
);
}
handleTap(name) => setState(() => tapCount[name]++);
}
class PersonTile extends StatelessWidget {
const PersonTile({
@required this.name,
@required this.nickName,
@required this.color,
@required this.count,
@required this.onTap,
});
final String name;
final String nickName;
final int count;
final Color color;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
return ListTile(
leading: CircleAvatar(
child: Text(name[0]),
backgroundColor: color,
),
title: Text(name),
subtitle: Text(nickName),
trailing: Text('$count'),
onTap: onTap);
}
}
// Arrow
void main() => runApp(DscList());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment