Bandeiras que são geradas aleatoriamente, mas não repetem a cor
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'exercicio2',
theme: ThemeData(primarySwatch: Colors.red),
home: Scaffold(
appBar: AppBar(
title: const Text('exercicio2'),
backgroundColor: Colors.red.shade300),
body: Container(
alignment: AlignmentDirectional.topCenter,
color: Colors.red.shade100,
padding: const EdgeInsets.fromLTRB(0, 25, 0, 25),
child: ListView(
children: [
Flag(colors: [
returnColor(Random().nextInt(2)),
returnColor(Random().nextInt(2) + 2),
returnColor(Random().nextInt(2) + 4)
]),
Flag(colors: [
returnColor(Random().nextInt(2)),
returnColor(Random().nextInt(2) + 2),
returnColor(Random().nextInt(2) + 4)
]),
Flag(colors: [
returnColor(Random().nextInt(2)),
returnColor(Random().nextInt(2) + 2),
returnColor(Random().nextInt(2) + 4)
]),
Flag(colors: [
returnColor(Random().nextInt(2)),
returnColor(Random().nextInt(2) + 2),
returnColor(Random().nextInt(2) + 4)
]),
Flag(colors: [
returnColor(Random().nextInt(2)),
returnColor(Random().nextInt(2) + 2),
returnColor(Random().nextInt(2) + 4)
]),
],
)),
),
);
}
}
class Flag extends StatefulWidget {
List<Color> colors;
Flag({required this.colors, Key? key}) : super(key: key);
@override
State<Flag> createState() => _FlagState();
}
class _FlagState extends State<Flag> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: renderContainers(widget.colors),
),
);
}
}
Container doContainer(Color color) {
return Container(
width: 200,
height: 40,
color: color,
);
}
List<Container> renderContainers(List<Color> list) {
List<Container> containers = [];
for (var color in list) {
containers.add(doContainer(color));
}
return containers;
}
Color returnColor(int i) {
print(i);
if (i == 0) {
return Colors.blue;
} else if (i == 1) {
return Colors.amber;
} else if (i == 2) {
return Colors.green;
} else if (i == 3) {
return Colors.red;
} else if (i == 4) {
return Colors.deepPurple;
} else {
return Colors.white;
}
}