1
resposta

desafio

foi sofrido mas consegui fazer um projeto diferente, kkkk

import 'package:flutter/material.dart'; import 'dart:math';

main() => runApp(desafio());

class desafio extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( leading: IconButton(onPressed: () {}, icon: const Icon(Icons.add_task)), title: Text('Flutter: change my color'), centerTitle: true, ), body: Container( color: Colors.blue, child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ boxes(), boxes(), boxes(), boxes(), ], ), ), ), ); } }

// mundança de cor

class boxes extends StatefulWidget { const boxes({super.key});

@override State createState() => _boxesState(); }

class _boxesState extends State { Color _containerColor = Colors.green; // Cor padrão Color _containerColor2 = Colors.red; Color _containerColor3 = Colors.orange; bool _isRandomColor = false;

void _changeColor() { setState(() { if (_isRandomColor) { // Voltar para a cor padrão _containerColor = _generateRandomColor(); _containerColor2 = _generateRandomColor(); _containerColor3 = _generateRandomColor(); } else { // Definir uma cor aleatória _containerColor = _generateRandomColor(); _containerColor2 = _generateRandomColor(); _containerColor3 = _generateRandomColor(); } _isRandomColor = !_isRandomColor; }); }

Color _generateRandomColor() { final random = Random(); return Color.fromRGBO( random.nextInt(256), random.nextInt(256), random.nextInt(256), 1.0, ); }

@override Widget build(BuildContext context) { return Container( child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Container( color: _containerColor, width: 70, height: 70, ), Container( color: _containerColor2, width: 70, height: 70, ), Container( color: _containerColor3, width: 70, height: 70, ), ElevatedButton( onPressed: () { _changeColor(); }, child: Icon(Icons.rotate_left_outlined), style: ElevatedButton.styleFrom(backgroundColor: Colors.black), ), ], ), ); } }

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

1 resposta

Parabéns! Que bom que não desistiu e superou todas as dificuldades. Continue assim.