flutter๋ก UI ์์
์ Row์ Column๋ก ๋ฐฐ์น ๋ ์ด์์์ ์ก์ ์ ์๋ค. Row()์ผ๋ก ๊ฐ์ธ์ children ์์ฑ์ Widget(Container, Text, SizedBox ๋ฑ)์ ๋ฃ์ด์ฃผ๋ฉด ๊ฐ๋ก๋ก ์์ ฏ๋ค์ด ๋ฐฐ์น๊ฐ ๋๋ค. Column()์ ์ธ๋ก๋ก ๋ฐฐ์น๊ฐ ๋๋ค.
MainAxisAlignment & CrossAxisAlignment
Row์ ๊ฒฝ์ฐ main axis์ ์ํ์ผ๋ก ์คํ๋๊ณ cross axis์ ์์ง์ผ๋ก ์คํ๋๋ค. Column์ ๊ฒฝ์ฐ main axis์ ์์ง์ผ๋ก ์คํ๋๊ณ cross axis์ ์ํ์ผ๋ก ์คํ๋๋ค.
๐ Sample Code
Transform.scale(
scale: 5,
child: const Icon(
Icons.euro_rounded,
color: Colors.white,
size: 98,
),
์์ ฏ์ ๋ถ๋ฆฌํ์ฌ ์ฌ์ฌ์ฉํ ์ ์๋ ์ปดํฌ๋ํธ๋ก ๋ง๋ค ์ ์๋ค.
command + .
์์ Extract Widget ์ ํ ํ ์ด๋ฆ์ ์ง์ ํ๋ฉด ์๋์ผ๋ก ํด๋น ํ์ผ ๋ด์์ ๋ถ๋ฆฌ๋ฅผ ํด์ค๋ค.
๐ Sample Code
//button.dart
import 'package:flutter/material.dart';
class Button extends StatelessWidget {
final String text;
final Color bgColor;
final Color textColor;
const Button({
super.key,
required this.text,
required this.bgColor,
required this.textColor,
});
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: bgColor,
borderRadius: BorderRadius.circular(20),
),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 20,
horizontal: 50,
),
child: Text(
text,
style: TextStyle(
color: textColor,
fontSize: 17,
),
),
),
);
}
}
๋ฒํผ์ ์ฌ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด ํด๋์ค ์ด๋ฆ๊ณผ ํจ๊ป ํ์ํ ํ๋ผ๋ฏธํฐ๋ฅผ ์์ฑํด์ฃผ๋ฉด ๋๋ค. ๋ฐ์ ์ํ์ฝ๋๋ required๋ text, bgColor, textColor์ ์ ์ด์ฃผ์ด ์์ฑํ์๋ค.
๐ Sample Code
Button(
text: "Transfer",
bgColor: Color(0xFFF1B33B),
textColor: Colors.black,
),
DevTools๋ Dart์ Flutter๋ฅผ ์ํ ์ฑ๋ฅ ๋ฐ ๋๋ฒ๊น
๋๊ตฌ์ด๋ฉฐ UI ๋ ์ด์์, ์ํ(state)๋ฅผ ์ ๊ฒํ ์ ์๋๋ก ๋์์ค๋ค. ๊ทธ ์ธ CPU, Network, Debug Memory issue, ์ฝ๋ ๋ถ์, ์ง๋จ ๋ฑ ์ฌ๋ฌ ๋ณตํฉ์ ์ธ ๊ธฐ๋ฅ๋ค์ ์ ๊ณตํ๋ค. ์ฃผ์ํ ์ ์ ์ฑ์ ๊ตฌ๋ํ ์ํ์ฌ์ผ ํ๋ค๋ ๊ฒ์ด๋ค. ๊ทธ ์ดํ ํด๋น ํ๋ฉด์ ์๋ open devTools๋ฅผ ์คํํ ์ ์๋ค.
๊ทธ ์ค widget Inspector๋ฅผ ์คํํ๋ฉด ์์ ฏ ํธ๋ฆฌ๋ฅผ ์๊ฐํํ๊ณ ํ์ํ ์ ์๋ค.
class CurrencyCard extends StatelessWidget {
final String name, code, amount;
final IconData icon;
final bool isInverted;
final _blackColor = const Color(0xFF1F2123);
final double offset; //์ ๋ณ์ ์ ์ธ
const CurrencyCard({
super.key,
required this.name,
required this.code,
required this.amount,
required this.icon,
required this.isInverted,
required this.offset,
});
Widget build(BuildContext context) {
return Transform.translate(
offset: Offset(0, offset), //offset ์ค์
child: Container(
clipBehavior: Clip.hardEdge,
//(์๋ต)
์ฐธ๊ณ ์๋ฃ
https://nomadcoders.co/flutter-for-beginners/lectures/4139
https://docs.flutter.dev/ui/layout
https://docs.flutter.dev/tools/devtools/inspector
https://www.w3.org/TR/css-align-3/
https://css-tricks.com/almanac/properties/j/justify-content/