๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Flutter] build ๋ฉ”์„œ๋“œ ๋ณธ๋ฌธ

๐Ÿ“ฑFlutter/Flutter Framework

[Flutter] build ๋ฉ”์„œ๋“œ

๋ฃจ๋ฆฌ์•ผใ…‘ 2025. 1. 17. 23:51
๋ฐ˜์‘ํ˜•

 

build ๋ฉ”์„œ๋“œ๋Š” Flutter์—์„œ UI๋ฅผ ๊ทธ๋ฆฌ๋Š” ํ•ต์‹ฌ์ ์ธ ์—ญํ• ์„ ํ•œ๋‹ค.
Flutter์—์„œ UI๋Š” ์œ„์ ฏ์œผ๋กœ ๊ตฌ์„ฑ๋˜๊ณ , build ๋ฉ”์„œ๋“œ๋Š” ์ด ์œ„์ ฏ๋“ค์ด ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ฐฐ์น˜๋˜๊ณ  ํ‘œ์‹œ๋  ์ง€ ์ •์˜ํ•œ๋‹ค.

build ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ๊ธฐ?

1. ์ฒ˜์Œ ์œ„์ ฏ์ด ํ™”๋ฉด์— ์ถ”๊ฐ€๋  ๋•Œ
์œ„์ ฏ์ด Flutter ์•ฑ ํŠธ๋ฆฌ์— ์‚ฝ์ž…๋˜๋ฉด, build ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

2. ์œ„์ ฏ์˜ ์˜์กด์„ฑ์ด ๋ณ€๊ฒฝ๋  ๋•Œ
์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์ ฏ์ด ์ฐธ์กฐํ•˜๋Š” InheritedWidget์ด ์—…๋ฐ์ดํŠธ๋˜๋ฉด, ํ•ด๋‹น ์œ„์ ฏ์˜ build ๋ฉ”์„œ๋“œ๊ฐ€ ๋‹ค์‹œ ํ˜ธ์ถœ๋œ๋‹ค.

3. ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ(StatefulWidget)
์ƒํƒœ๊ฐ€ ๋ณ€ํ•˜๋ฉด build ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด UI๊ฐ€ ์—…๋ฐ์ดํŠธ๋œ๋‹ค.

 

build ๋ฉ”์„œ๋“œ์˜ ํŠน์ง•

๋ฐ˜ํ™˜๊ฐ’

ํ•ญ์ƒ ๋‹ค๋ฅธ ์œ„์ ฏ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ํ…์ŠคํŠธ๋ฅผ ํ‘œ์‹œํ•˜๊ฑฐ๋‚˜ ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•˜๋Š” ์œ„์ ฏ์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋ฅธ์šฉ๋„๋กœ ์‚ฌ์šฉ ๊ธˆ์ง€

build ๋ฉ”์„œ๋“œ๋Š” ์œ„์ ฏ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ ์š”์ฒญ ๊ฐ™์€ ์ž‘์—…์€ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

 

build ๋ฉ”์„œ๋“œ ๋™์ž‘ ์›๋ฆฌ

1. ํ˜ธ์ถœ ์‹œ์  ๊ฐ๊ฐ์— ๋งž๊ฒŒ build ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

2. ํ˜ธ์ถœ ๋’ค build ๋ฉ”์„œ๋“œ๋Š” ํ•ญ์ƒ ๋‹ค๋ฅธ ์œ„์ ฏ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๋ฐ˜ํ™˜๋œ ์œ„์ ฏ์€ ํ˜„์žฌ ์œ„์ ฏ์˜ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.

3. ๋ฐ˜ํ™˜๋œ ์œ„์ ฏ์€ ๊ธฐ์กด UI์™€ ๋น„๊ตํ•˜์—ฌ ํ™”๋ฉด์— ๊ทธ๋ฆด UI๋ฅผ ์ •์˜ํ•˜์—ฌ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค. 

 

[์˜ˆ์‹œ ์ฝ”๋“œ]

1. MaterialApp: Flutter ์•ฑ์˜ ์‹œ์ž‘์ .

2. Scaffold: ํ™”๋ฉด ๋ ˆ์ด์•„์›ƒ(์•ฑ๋ฐ”, ๋ณธ๋ฌธ, ํ”Œ๋กœํŒ… ๋ฒ„ํŠผ ๋“ฑ)์„ ์ •์˜.

3. AppBar: ํ™”๋ฉด ์ƒ๋‹จ์˜ ์•ฑ๋ฐ”.

4. Center: ์ž์‹ ์œ„์ ฏ์„ ํ™”๋ฉด ๊ฐ€์šด๋ฐ์— ๋ฐฐ์น˜.

5. Column: ์ž์‹ ์œ„์ ฏ์„ ์„ธ๋กœ๋กœ ๋‚˜์—ด.

6. ElevatedButton: ํด๋ฆญ ๊ฐ€๋Šฅํ•œ ๋ฒ„ํŠผ.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter ์ดˆ๋ณด ์—ฐ์Šต'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('์•ˆ๋…•ํ•˜์„ธ์š”, Flutter์ž…๋‹ˆ๋‹ค!'),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  print('๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ์Šต๋‹ˆ๋‹ค!');
                },
                child: const Text('ํด๋ฆญํ•˜์„ธ์š”'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

 

BuildContext์˜ ์—ญํ• 

1. ์œ„์น˜ ์ •๋ณด ์ œ๊ณต

BuildContext๋Š” ํ˜„์žฌ ์œ„์ ฏ์ด ํŠธ๋ฆฌ์˜ ์–ด๋””์— ์œ„์น˜ํ•ด ์žˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
์ด๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ์œ„์ ฏ์ด ์ƒ์œ„ ์œ„์ ฏ(๋ถ€๋ชจ)์œผ๋กœ๋ถ€ํ„ฐ ์–ด๋–ค ์ •๋ณด๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

2. ์ƒ์†๋œ ์œ„์ ฏ(InheritedWidget) ์ ‘๊ทผ

ํŠธ๋ฆฌ์—์„œ ์ด ์œ„์น˜์™€ ์—ฐ๊ด€๋œ ์ƒ์†๋œ ์œ„์ ฏ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ํ…Œ๋งˆ ์ •๋ณด๋‚˜ ์•ฑ์˜ ์ „์—ญ ์ƒํƒœ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

3. ์œ„์ ฏ ์ด๋™์— ๋”ฐ๋ฅธ ์ปจํ…์ŠคํŠธ ๋ณ€๊ฒฝ

์œ„์ ฏ์ด ํŠธ๋ฆฌ์˜ ๋‹ค๋ฅธ ์œ„์น˜๋กœ ์ด๋™ํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ์œ„์น˜์—์„œ ๋™์‹œ์— ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ,
**๊ฐ ์œ„์น˜์— ๋งž๋Š” BuildContext**๊ฐ€ ์ œ๊ณต๋œ๋‹ค.

 

build ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ ์‹œ ์œ ์˜์‚ฌํ•ญ

build ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ์š”์†Œ์—๋งŒ ์˜์กดํ•ด์•ผ ํ•œ๋‹ค.

1. ์œ„์ ฏ์˜ ํ•„๋“œ

• ์œ„์ ฏ์— ์ •์˜๋œ ๊ฐ’(ํ•„๋“œ)์„ ์‚ฌ์šฉํ•ด UI๋ฅผ ๊ทธ๋ฆฐ๋‹ค.
• ํ•„๋“œ๋Š” ํ•œ ๋ฒˆ ์„ค์ •๋˜๋ฉด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์›์น™(StatelessWidget์˜ ๊ฒฝ์šฐ).

class MyWidget extends StatelessWidget {
  final String title;

  MyWidget(this.title);

  @override
  Widget build(BuildContext context) {
    return Text(title); // title ํ•„๋“œ์— ์˜์กด
  }
}

 

2. Context๋ฅผ ์–ป์€ ์ƒํƒœ

• BuildContext.dependOnInheritedWidgetOfExactType ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํŠธ๋ฆฌ์—์„œ ์ƒ์†๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
• ์˜ˆ๋ฅผ ๋“ค์–ด, ํ…Œ๋งˆ๋‚˜ ์–ธ์–ด ์„ค์ • ๊ฐ™์€ ์ „์—ญ ์ƒํƒœ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

@override
Widget build(BuildContext context) {
  final theme = Theme.of(context); // Theme ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
  return Text('Hello, Flutter!', style: theme.textTheme.headline4);
}

 

๋งŒ์•ฝ ๋‹ค๋ฅธ ์š”์†Œ์— ์˜์กดํ•ด์•ผ ํ•œ๋‹ค๋ฉด?

build ๋ฉ”์„œ๋“œ๊ฐ€ ์œ„์˜ ๋‘ ๊ฐ€์ง€ ์™ธ์— ์ถ”๊ฐ€์ ์ธ ์š”์†Œ์— ์˜์กดํ•ด์•ผ ํ•œ๋‹ค๋ฉด,
StatefulWidget์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

StatefulWidget์„ ์‚ฌ์šฉํ•˜๋ฉด ์ƒํƒœ(State)๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ณด๋‹ค ์œ ์—ฐํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•