Flutter는 직관적이고 강력한 UI 구성 도구를 제공하며, 이를 활용해 다양한 디자인 패턴을 구현할 수 있습니다.
이 글에서는 Flutter에서 자주 사용되는 UI 디자인 패턴과 구현 예제를 소개합니다.
Material Design은 Google이 정의한 디자인 언어로, Flutter는 이를 기본적으로 지원합니다. Flutter의 Material 위젯은 Material Design 스타일의 앱을 쉽게 구축할 수 있도록 도와줍니다.
import 'package:flutter/material.dart';
class MaterialDesignExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Material Design Example'),
),
body: Center(
child: Text('Hello, Material Design!'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
),
);
}
}
Cupertino Design은 iOS 스타일의 디자인 언어로, Flutter는 이를 구현하기 위한 Cupertino 위젯 세트를 제공합니다.
import 'package:flutter/cupertino.dart';
class CupertinoDesignExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoApp(
home: CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('Cupertino Design Example'),
),
child: Center(
child: CupertinoButton(
child: Text('Click Me'),
onPressed: () {},
),
),
),
);
}
}
Flutter는 커스텀 위젯을 쉽게 정의할 수 있어 재사용성과 일관성을 높이는 데 유리합니다.
반복적으로 사용되는 UI 요소를 커스텀 위젯으로 만들어 효율성을 높입니다.
import 'package:flutter/material.dart';
class CustomButton extends StatelessWidget {
final String label;
final VoidCallback onPressed;
CustomButton({required this.label, required this.onPressed});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
textStyle: TextStyle(fontSize: 18),
),
child: Text(label),
);
}
}
class CustomWidgetExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Custom Widget Example'),
),
body: Center(
child: CustomButton(
label: 'Press Me',
onPressed: () {
print('Button Pressed');
},
),
),
),
);
}
}
Flutter는 다양한 화면 크기에 적응하는 반응형 디자인을 구현하기 쉽습니다.
화면 크기에 따라 위젯의 크기와 레이아웃을 조정합니다.
import 'package:flutter/material.dart';
class ResponsiveExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(title: Text('Responsive Example')),
body: Center(
child: Container(
width: screenWidth * 0.8,
height: 200,
color: Colors.blue,
child: Center(
child: Text(
'Responsive Box',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
),
);
}
}
부모 위젯의 제약 조건에 따라 동적으로 레이아웃을 변경합니다.
LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > 600) {
return Text('Tablet Layout');
} else {
return Text('Mobile Layout');
}
},
)
Flutter는 Material Design과 Cupertino Design을 기본으로 제공하며, 커스텀 위젯과 반응형 디자인 패턴을 통해 유연하고 강력한 UI를 구현할 수 있습니다. 이 글에서 소개한 패턴들을 활용하여 일관성 있고 사용성 높은 앱을 개발해보세요. 다음 글에서는 고급 애니메이션 패턴에 대해 다룰 예정입니다.
버튼 클릭 시 색상 변화 애니메이션 구현하기 (0) | 2025.01.03 |
---|---|
Flutter 고급 애니메이션 패턴 (0) | 2025.01.02 |
클라우드 파이어스토어를 활용한 고급 데이터 관리 기법 (0) | 2025.01.01 |
Flutter와 Firebase 통합 가이드 (0) | 2025.01.01 |
Sliver와 애니메이션을 결합한 고급 패턴 (0) | 2025.01.01 |