์ฌ๋ฌ ๊ฐ์ง ์์ ฏ์ ๋ํด ๊ณต๋ถ๋ฅผ ํ๋ ์ค, AnimatedContainer
๋ฅผ ํ์ตํ๊ฒ ๋์๋ค. ํฐ์นํ๋ฉด ์ด๋ฏธ์ง์ ์ฌ์ด์ฆ๊ฐ ๋๋ค์ผ๋ก ๋ณํ๋๋ก ํ๋ ์์ ์๋ค. ์ด๋ฏธ์ง ๋ง๊ณ ๋ ๋ค๋ฅธ ์์ ฏ๋ค๋ ํด๋ณด๋ ์ค, ๊ฐ์๊ธฐ 'ํฐ์นํ ๋๋ง๋ค ์๊น๋ ๊ฐ์ด ๋ณํ๋ฉด ๋ ๋ณผ๋ง ํ๊ฒ ๋๋ฐ?' ์๊ฐ์ด ๋ค์ด ์ฐพ์๋ณด๊ฒ ๋์๋ค.
๋๋ค์ผ๋ก ์์์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์ด ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค. ๊ธ์ ๋ณด๊ณ ๋ง์์ ๋๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด๋ณด์.
Colors ํด๋์ค๋ 18๊ฐ์ ๊ฒฌ๋ณธ ๋จธํฐ๋ฆฌ์ผ ๋์์ธ ์์ ํ๋ ํธ๋ฅผ ์ ๊ณตํ๋ค.
Random ํด๋์ค์๋ 0๋ถํฐ ์ง์ ๋ ์ต๋๊ฐ๊น์ง ๋ฒ์์์ ์์์ ์ ์๋ฅผ ์์ฑํ ์ ์๋ nextInt
๋ผ๋ ๋ฉ์๋ ์ด๋ฆ์ด ํจ๊ป ์ ๊ณต๋๋ค.
๋๋คํ ์์์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
Color _randomColor = Colors.primaries[Random().nextInt(Colors.primaries.length)];
๋ง์ฝ ๋์ฑ ๋๋ค์ฑ์ ์ฃผ๊ณ ์ถ์ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
Color _randomColor = Colors.primaries[Random().nextInt(Colors.primaries.length)]
[Random.nextInt(9) * 100];
๐๋ฐ๋์ โpackage:flutter/material.dartโ ์ โdart:mathโ. ๋ฅผ import ํด์ค์ผ ํ๋ค.
์ ์ฒด ์ฝ๋
import 'package:flutter/material.dart';
import 'dart:math';
(์ค๋ต)
class _MyHomePageState extends State<MyHomePage> {
var _size = 100.0;
final _random = Random();
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter'),
),
body: Center(
child: GestureDetector(
onTap: () {
final random = Random();
setState(() {
_size = random.nextInt(200).toDouble() + 100;
});
},
child: AnimatedContainer(
duration: Duration(seconds: 1),
width: _size,
height: _size,
child: Container(
color: Colors.primaries[_random.nextInt(Colors.primaries.length)]
[_random.nextInt(9) * 100],
),
curve: Curves.linearToEaseOut,
),
),
),
);
Colors ํด๋์ค๋ ์๋ฆ๋ต๊ณ ํ์ค์ ์ธ ์ปฌ๋ฌ๋ฅผ ์ ๊ณตํ์ง๋ง, ์ปฌ๋ฌ์ ์๊ฐ ๋ง์ง ์๋ค๋ ์ ์ด ์๋ค.
Color ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์๋ฐฑ๋ง ๊ฐ์ ๋๋ค ์์์ ์์ฑํ ์ ์๋ค.
// ์๋ต
child: Container(
color: Color.fromARGB(
_random.nextInt(256),
_random.nextInt(256),
_random.nextInt(256),
_random.nextInt(256),
),
),
๋๋ค ์์์ ์์ฑํ๋ ๋ฐ ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ ํ์๊น์ง ์์ ๊ฒ์ด๋ค. ํ์ง๋ง ํ๋ฌ๊ทธ์ธ ๋ง์ ์ฅ์ ์ด ์๋ค.
์๋ฅผ ๋ค์ด random-color
ํจํค์ง๋ ๋๋ค์ผ๋ก ์์ฑ๋๋ ์์์ ๋ฒ์๋ ์คํ์ผ์ ์ง์ ํ์ฌ ํด๋นํ๋ ์กฐ๊ฑด์ ๋ง๋ ์์๋ง ์ป์ ์ ์๋ค.
์ค์น
dependencies:
random_color: ^1.0.5
๊ฐ๋จํ ์์
import 'package:random_color/random_color.dart';
RandomColor _randomColor = RandomColor();
Color _color = _randomColor.randomColor(colorHue: ColorHue.red);
์ ์ฝ๋๋ ๋ถ์ ๊ณ์ด์ ์์๋ง ๋ฐํํ๋ค.
์ค๋์ ๋๋ค์ผ๋ก ์์์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด์๋ค. ์ค์ ๊ฐ๋ฐ์ ํ๋ฉฐ ์ฌ์ฉํ ์ผ์ ๊ทธ๋ ๊ฒ ๋ง์ง๋ ์๊ฒ ์ง๋ง ๋ค์ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ ์ ์๋ ์ข์ ๊ฒฝํ์ด์๋ค.
๊ฐ์ธ์ ์ผ๋ก ์กฐ๊ธ ๊ท์ฐฎ์๋ random-color
ํจํค์ง๋ฅผ ๋ฐ์ ์ฌ์ฉํ๋๊ฒ ์ข์ ๊ฒ ๊ฐ๋ค.๐ฅด