Tentei criar esse projeto pra adicionar as bandeiras conforme clicava no Botão, porém as bandeiras só aparecem se der um hot reload... tem alguma forma de fzr funcionar?
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Flag> flags = [];
@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: flags,
)),
floatingActionButton: FloatingActionButton(
onPressed: () {
flags.add(createFlag());
},
child: const Icon(Icons.add),
)));
}
}
class Flag extends StatelessWidget {
final List<Color> colors;
const Flag({required this.colors, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: renderContainers(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) {
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;
}
}
Flag createFlag() {
return Flag(colors: [
returnColor(Random().nextInt(2)),
returnColor(Random().nextInt(2) + 2),
returnColor(Random().nextInt(2) + 4)
]);
}