아래 예제는 폰트를 외부에서 가져와 사용한 예시입니다.
폰트 찾을 때 참고 주소 : https://pub.dev/
클릭하면 전체 코드를 볼 수 있습니다.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: RecipePage(),
theme: ThemeData(
textTheme: GoogleFonts.patuaOneTextTheme(
Theme.of(context).textTheme,
),
),
);
}
}
class RecipePage extends StatelessWidget {
const RecipePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: _appbar(),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: ListView(
children: [
Title(),
Menu(),
ItemComponent("burger"),
ItemComponent("coffee"),
ItemComponent("pizza"),
],
),
));
}
AppBar _appbar() {
return AppBar(
actions: [
Icon(Icons.search),
SizedBox(width: 16),
Icon(CupertinoIcons.heart, color: Colors.redAccent),
SizedBox(width: 16),
],
);
}
}
class ItemComponent extends StatelessWidget {
final text;
ItemComponent(this.text);
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 30),
AspectRatio(
aspectRatio: 2 / 1,
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.asset(
"assets/${text}.jpeg",
fit: BoxFit.cover,
),
),
),
SizedBox(height: 10),
Text("Made ${text}", style: TextStyle(fontSize: 20)),
Text(
"Have you ever made your own ${text}? Once you've tried a homemade ${text}, you'll never go back.",
style: TextStyle(color: Colors.grey, fontSize: 12),
)
],
);
}
}
class Menu extends StatelessWidget {
const Menu({
super.key,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 20),
child: Row(
children: [
MenuItem(mIcon: Icons.food_bank, mText: "ALL"),
SizedBox(width: 25),
MenuItem(mIcon: Icons.emoji_food_beverage, mText: "Coffe"),
SizedBox(width: 25),
MenuItem(mIcon: Icons.fastfood, mText: "Burger"),
SizedBox(width: 25),
MenuItem(mIcon: Icons.local_pizza, mText: "Pizza"),
],
),
);
}
}
class Title extends StatelessWidget {
const Title({
super.key,
});
@override
Widget build(BuildContext context) {
return Text(
"Recipes",
style: TextStyle(fontSize: 30),
);
}
}
class MenuItem extends StatelessWidget {
final mIcon;
final mText;
MenuItem({required this.mIcon, required this.mText});
@override
Widget build(BuildContext context) {
return Container(
width: 60,
height: 80,
decoration: BoxDecoration(
border: Border.all(color: Colors.black12),
borderRadius: BorderRadius.circular(30),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
mIcon,
color: Colors.redAccent,
size: 30,
),
SizedBox(height: 3),
Text("${mText}", style: TextStyle(color: Colors.black87))
// 쌍 따옴표 안의 문자열이 너무 길면 터질 수 있고, 달러를 써서 넣으면 너무 길 때 빈 문자열로 바꿔주기 때문에 괜찮다
],
),
);
}
}
추가로 배운 내용
EdgeInsets
EdgeInsets.symmetric(horizontal: 20)
- 설명:
EdgeInsets
는 위젯의 패딩이나 마진을 설정할 때 사용됩니다.symmetric
는 수평(horizontal)과 수직(vertical) 방향으로 대칭 패딩을 설정합니다.
- 용도: 이 코드는 수평 방향으로 20의 패딩을 적용합니다. 즉, 왼쪽과 오른쪽에 각각 20의 여백이 생깁니다.
BoxDecoration
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black12),
borderRadius: BorderRadius.circular(30),
),
child: ...
)
- 설명:
BoxDecoration
은Container
위젯의 배경, 경계선, 그림자 등을 설정하는 데 사용됩니다.
- border:
Border.all
을 사용하여 모든 경계선의 색상을 설정합니다. 여기서는Colors.black12
로 설정되어 있습니다.
- borderRadius:
BorderRadius.circular
을 사용하여 모든 모서리의 반경을 설정합니다. 여기서는 30으로 설정되어 둥근 모서리를 만듭니다.
핵심 요소
AspectRatio 위젯
AspectRatio
위젯은 자식 위젯의 종횡비(가로와 세로의 비율)를 강제합니다. 이는 이미지나 다른 요소들이 특정 비율을 유지하며 화면에 표시되도록 하는데 유용합니다.사용 예시
AspectRatio(
aspectRatio: 3 / 2, // 가로:세로 비율을 3:2로 설정
child: Image.asset(
'assets/sample.jpg',
fit: BoxFit.cover,
),
);
주요 속성
aspectRatio
: 위젯의 종횡비를 설정하는 데 사용됩니다. 예를 들어,aspectRatio: 3 / 2
는 가로가 3이고 세로가 2인 비율을 의미합니다.
BoxFit 옵션
BoxFit.cover
: 이미지가 Container를 덮도록 크기를 조정합니다. 이미지의 종횡비가 유지되고, Container를 완전히 채우기 위해 이미지의 일부가 잘릴 수 있습니다.
BoxFit.contain
: 이미지가 Container 내부에 맞도록 크기를 조정합니다. 이미지의 종횡비가 유지되고, 이미지 전체가 Container에 맞춰집니다.
BoxFit.fill
: 이미지가 Container를 채우도록 크기를 조정합니다. 이미지의 종횡비는 유지되지 않을 수 있습니다.
BoxFit.fitWidth
: 이미지의 너비가 Container에 맞춰집니다. 이미지의 종횡비가 유지되며, 높이가 잘릴 수 있습니다.
BoxFit.fitHeight
: 이미지의 높이가 Container에 맞춰집니다. 이미지의 종횡비가 유지되며, 너비가 잘릴 수 있습니다.
ClipRRect 위젯
ClipRRect
위젯은 자식 위젯을 둥근 모서리로 자르기 위해 사용됩니다. 이는 이미지나 다른 요소들에 둥근 모서리를 적용할 때 유용합니다.사용 예시
ClipRRect(
borderRadius: BorderRadius.circular(20), // 둥근 모서리 반경을 20으로 설정
child: Image.asset(
'assets/sample.jpg',
fit: BoxFit.cover,
),
);
주요 속성
borderRadius
: 둥근 모서리의 반경을 설정합니다.BorderRadius.circular()
를 사용하여 모든 모서리의 반경을 동일하게 설정할 수 있습니다.BorderRadius.only()
를 사용하면 특정 모서리에만 반경을 설정할 수도 있습니다.
완성된 페이지 이미지

Tip :
저장할 때 자동 정렬 및 임포트 기능

- 위 이미지대로 설정하면 저장할 때 정렬 기능과 임포트를 한번에 할 수 있습니다.
Share article