๐Ÿ—๏ธ์†Œํ”„ํŠธ์›จ์–ด/Flutter

[Flutter] ๋ฒ„ํŠผ ์œ„์ ฏ(TextButton, ElevatedButton, OutlinedButton) ์‚ฌ์šฉ๋ฒ•๊ณผ ์˜ˆ์ œ ์ฝ”๋“œ ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์™ธ ํ•„์ˆ˜ ๊ฐœ๋…๋“ค

Dev.Op 2023. 10. 2. 01:17
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”! ์˜ค๋Š˜์€ Flutter์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฒ„ํŠผ ์œ„์ ฏ์ธ TextButton, ElevatedButton, OutlinedButton์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ธ€์—์„œ๋Š” ํ•ด๋‹น ์œ„์ ฏ๋“ค์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•๊ณผ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ฝ”๋“œ ์ž‘์„ฑ ์‹œ ์ฃผ์˜ํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ํ”Œ๋Ÿฌํ„ฐ ๊ทœ์น™๊ณผ ์œ ์šฉํ•œ ์ฝ”๋“œ ์ •๋ฆฌ ๋‹จ์ถ•ํ‚ค๋„ ํ•จ๊ป˜ ์•Œ์•„๋ด…์‹œ๋‹ค.


Button

- TextButton

TextButton์€ ๊ฐ„๋‹จํ•œ ํ…์ŠคํŠธ ๋ฒ„ํŠผ์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” TextButton์„ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ์— ๋Œ€ํ•œ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

TextButton(
  onPressed: () {
    // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์‹คํ–‰ํ•  ๋™์ž‘
    print('TextButton ํด๋ฆญ๋จ');
  },
  child: Text('TextButton ํ…์ŠคํŠธ'),
)

 

- ElevatedButton

ElevatedButton์€ ์ผ๋ฐ˜์ ์ธ ๋†’์ด๋ฅผ ๊ฐ€์ง„ ๋ฏธ๋ฆฌ ์ •์˜๋œ ๋ฒ„ํŠผ ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ElevatedButton์˜ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

ElevatedButton(
  onPressed: () {
    // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์‹คํ–‰ํ•  ๋™์ž‘
    print('ElevatedButton ํด๋ฆญ๋จ');
  },
  child: Text('ElevatedButton ํ…์ŠคํŠธ'),
)

 

- OutlinedButton

OutlinedButton์€ ์™ธ๊ณฝ์„ ์ด ์žˆ๋Š” ๋ฒ„ํŠผ์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” OutlinedButton์˜ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

OutlinedButton(
  onPressed: () {
    // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์‹คํ–‰ํ•  ๋™์ž‘
    print('OutlinedButton ํด๋ฆญ๋จ');
  },
  child: Text('OutlinedButton ํ…์ŠคํŠธ'),
)

 

์‚ฌ์šฉํ•œ ์˜ˆ๋Š” ๋‹ค์Œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

ํŒŒ์ผ : main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
          // appBar ์ƒ‰์ƒ ๋ณ€๊ฒฝ
          appBarTheme: AppBarTheme(
            backgroundColor: Colors.blue, // appBar ์ „์ฒด ์ƒ‰์ƒ ํŒŒ๋ž€์ƒ‰
            titleTextStyle: TextStyle(
              color: Colors.black,
              fontSize: 25.0, // ํฐํŠธ ์‚ฌ์ด์ฆˆ ๊ธฐ๋ณธ ์‚ฌ์ด์ฆˆ์˜ 2.5๋ฐฐ
            ), // title ํ…์ŠคํŠธ ์ƒ‰์ƒ ๊ฒ€์ •์ƒ‰
          )),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int count = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('$count',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 100,
                )),
            Text('$count',
                style: TextStyle(
                  color: Colors.red,
                  fontSize: 70,
                )),
            ElevatedButton(
              onPressed: () {
                print('ElevatedButton');
              },
              child: Text('ElevatedButton'),
            ),
            TextButton(
              onPressed: () {
                print('TextButton');
              },
              child: Text('TextButton'),
            ),
            OutlinedButton(
              onPressed: () {
                print('OutlinedButton');
              },
              child: Text('TextButton'),
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            // ํ™”๋ฉด ๊ฐฑ์‹ 
            setState(() {
              count++;
            });
          },
          child: Icon(Icons.add_box_outlined)),
    );
  }
}

 

 

์ฝ”๋“œ ์ •๋ฆฌ ๋‹จ์ถ•ํ‚ค

- 'Command' + 'Alt' + 'l'

 

 

ํ”Œ๋Ÿฌํ„ฐ ๊ทœ์น™ 

- ํ•˜๋‚˜์˜ ํ™”๋ฉด์€ ํ•˜๋‚˜์˜ ํŒŒ์ผ์— ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

- ๋ณ€์ˆ˜๋Š” ํ•ญ์ƒ ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค.(๋ณ€์ˆ˜, ๋ฉ”์„œ๋“œ, ํ•จ์ˆ˜)

- ํด๋ž˜์Šค๋Š” ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค.

 

[ํŒŒ์ผ๋ช… ๋„ค์ด๋ฐ ๊ทœ์น™] ํ”Œ๋Ÿฌํ„ฐ ์ƒˆ๋กœ์šด ํŒŒ์ผ ๋งŒ๋“ค๋•Œ ๊ทœ์น™

- CamelCase๋กœ ์ž‘์„ฑ๋˜์—ˆ๋˜ ๋ถ€๋ถ„์˜ ๋ช…์นญ์„ snake_case๋กœ ๋ฐ”๊ฟ”์„œ ์ €์žฅํ•ด์•ผํ•จ!!

 

import ํŽธํ•˜๊ฒŒ ํ•˜๊ธฐ

- ๋นจ๊ฐ„์ƒ‰ ๋ฐ‘์ค„ ์žˆ๋Š” ๋ถ€๋ถ„์—์„œ option + enter ํ‚ค ๋ˆŒ๋Ÿฌ์„œ ๋ถˆ๋Ÿฌ์˜ค๊ณ , ์ ˆ๋Œ€ ๊ฒฝ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ

 

๋„ค์ด๋ฐ ์ผ๊ด„ ๋ณ€๊ฒฝ

- 'Shift' + 'f6' ๋ฒ„ํŠผ์œผ๋กœ ํ•œ๋ฒˆ์— ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๊ณ 

- ์šฐํด๋ฆญํ•˜์—ฌ Refactor์—์„œ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

- ํ•œ๋ฒˆ ์ˆ˜์ •ํ•˜๋ฉด ๋ชจ๋“  ์ฝ”๋“œ, ๋ชจ๋“  ํŒŒ์ผ์—์„œ ๋™์ผํ•˜๊ฒŒ ์ˆ˜์ •๋˜๋Š” ์—„์ฒญ๋‚œ ๊ธฐ๋Šฅ์ด๋‹ค!!

 

ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์–ด๋–ค ์‹์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š”์ง€ ๋ณด๋Š” ๋ฒ•

- 'Command' + 'b'

 

์ตœ๊ทผ์— ์‚ฌ์šฉํ–ˆ๋˜ ํŒŒ์ผ ์—ด๊ธฐ

- 'Command' + 'e'

 

pubspec.yaml ํŒŒ์ผ์„ ์ˆ˜์ •ํ–ˆ์„๋•Œ

- ์šฐ์ธก ์ƒ๋‹จ 'Pub get' ๊ผญ ๋ˆŒ๋Ÿฌ์ฃผ๊ธฐ

- ํ™˜๊ฒฝ ์„ธํŒ…ํ•œ ์ดํ›„์— ์„ธํŒ…์„ ๋ฐ˜์˜ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ค˜์•ผ ํ•œ๋‹ค

 

์ด๋ฏธ์ง€ ํ‘œํ˜„ํ•˜๊ธฐ

- image.network ๋ฐฉ์‹

- ํŒŒ์ผ์„ ์ €์žฅํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ์‹ - yaml ํŒŒ์ผ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์กด์žฌํ•จ

Container

- ์ƒ‰๊น”๊ณผ ํฌ๊ธฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ

- ์ž์‹(child)๋Š” ๋ถ€๋ชจ ์†์„ฑ์„ ๋”ฐ๋ผ๊ฐ€๋Š” ์„ฑํ–ฅ์ด ์žˆ์Œ

์ด๋ ‡๊ฒŒ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž์‹์˜ ๊ทธ๋ฆผ ์‚ฌ์ด์ฆˆ๊ฐ€ 120 120์ด ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. 100 100 ์‚ฌ์ด์ฆˆ๊ฐ€ ์•„๋‹ˆ๋ผ.

- ๋ณดํ†ต ์ด๋Ÿฐ ๊ฒฝ์šฐ์— padding ์œผ๋กœ ๊ฐ์‹ธ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ์„ ๋งŽ์ด ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ

 

์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋•Œ ํ™”๋ฉด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์—ฌ์ง€๊ฒŒ ๋œ๋‹ค.

 

 

์‚ฌ์ง„ ์•„๋ž˜

 

 

๋ฐ˜์‘ํ˜•