
버튼을 누르면 컬러가 바뀌는 Container 예제
void main() {
runApp(MaterialApp(home: HomeScreen()));
}
class HomeScreen extends StatelessWidget {
Color color = Colors.blue;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
if (color == Colors.blue) {
color = Colors.red;
} else {
color = Colors.blue;
}
print('색상 변경 : color : $color');
},
child: Text('색상변경!'),
),
SizedBox(height: 32),
Container(width: 50, height: 50, color: color)
],
),
),
);
}
}
