첫 Flutter 앱을 만드는 방법을 안내합니다. 변수, 반복문, 조건문 등 기본 프로그래밍 개념과 객체지향에 친숙하다면, 이 튜토리얼을 완료할 수 있습니다. Dart 경험이나 모바일, 웹 프로그래밍 경험이 없어도 상관없습니다.
이 가이드는 코드랩 2부 중 1부입니다. 2부는 Google Developers에서 보실 수 있습니다. 1부 또한 Google Developers에서도 볼 수 있습니다.
스타트업 회사를 위해 이름을 생성하여 제안하는 간단한 모바일 앱을 구현할 것입니다. 사용자는 이름을 선택하거나 선택을 취소할 수 있으며, 가장 좋은 이름을 저장할 수 있습니다. 코드는 lazy하게 이름을 생성합니다. 사용자가 스크롤하면 더 많은 이름이 생성됩니다. 무한하게 스크롤 할 수 있습니다.
GIF 애니메이션은 1부를 완료하면 앱이 어떻게 동작하는지를 보여줍니다.
코드랩 2부에서는 상호작용을 추가하고, 앱 테마를 수정하며, 새 화면으로 이동하는 기능 (Flutter에서 route 라고 불립니다)을 추가합니다.
이 코드랩을 완료하기 위해 Flutter SDK와 에디터가 필요합니다. 이 코드랩은 안드로이드 스튜디오를 기반으로 되어있지만, 원한다면 에디터를 이용할 수도 있습니다.
이 코드랩을 아래 기기에서 실행할 수 있습니다:
첫 Flutter 앱 시작하기에 있는 지침을 따라 간단한 템플릿 기반 Flutter 앱을 만듭니다. 이름을 startup_namer로 지정합니다 (myapp 대신).
Tip: IDE에서 “New Flutter Project”가 보이지 않는다면, Flutter와 Dart 플러그인 설치가 설치되어 있는지 확인하세요.
코드랩에서는 주로 Dart 코드가 있는 lib/main.dart를 수정할 것입니다.
// Copyright 2018 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: Center(
child: Text('Hello World'),
),
),
);
}
}


이 단계에서는 가장 많이 사용되는 영어 단어 수천 개와 몇 가지 유틸리티 기능이 포함되어 있는 오픈 소스 패키지인 english_words를 이용할 것입니다.
다른 오픈 소스 패키지와 마찬가지로, [the Package site][]에서 english_words 패키지를 찾을 수 있습니다.
|
@@ -5,4 +5,5 @@
|
|
|
5
5
|
dependencies:
|
|
6
6
|
flutter:
|
|
7
7
|
sdk: flutter
|
|
8
8
|
cupertino_icons: ^0.1.2
|
|
9
|
+ english_words: ^3.1.0
|
$ flutter pub get
Running "flutter pub get" in startup_namer...
Process finished with exit code 0
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
|
@@ -9,6 +10,7 @@
|
|
|
9
10
|
class MyApp extends StatelessWidget {
|
|
10
11
|
@override
|
|
11
12
|
Widget build(BuildContext context) {
|
|
13
|
+ final wordPair = WordPair.random();
|
|
12
14
|
return MaterialApp(
|
|
13
15
|
title: 'Welcome to Flutter',
|
|
14
16
|
home: Scaffold(
|
|
@@ -16,7 +18,7 @@
|
|
|
16
18
|
title: Text('Welcome to Flutter'),
|
|
17
19
|
),
|
|
18
20
|
body: Center(
|
|
19
|
- child: Text(
|
|
21
|
+ child: Text(wordPair.asPascalCase),
|
|
20
22
|
),
|
|
21
23
|
),
|
|
22
24
|
);
|


앱이 올바르게 동작하지 않는다면, 오타를 확인해보세요. Flutter 디버깅 툴을 사용해보고 싶다면, DevTools 제품군과 프로파일링 툴을 확인해보세요. 필요하다면, 아래 코드를 사용하여 다시 올바르게 동작하게 하세요.
Stateless 위젯은 변경불가능immutable합니다. 속성을 변경할 수 없습니다—모든 값이 final입니다.
Stateful 위젯은 위젯의 수명동안 변경될 수 있는 상태를 유지합니다. Stateful 위젯은 최소 두 개 이상 클래스가 필요합니다: 1) StatefulWidget 클래스가 2) State 클래스 의 인스턴스를 생성합니다. StatefulWidget 클래스 그자체는 변경불가능합니다. 하지만 State 클래스가 위젯의 수명동안 상태를 유지합니다.
이 단계에서는, Stateful 위젯 RandomWords를 추가하고, 그 위젯에서 State 클래스인 RandomWordsState를 생성할 것입니다. 그런 다음 RandomWords를 기존 Stateless 위젯 MyApp의 자식으로 사용할 것입니다.
class RandomWordsState extends State<RandomWords> {
// TODO Add build() method
}
class RandomWords extends StatefulWidget {
@override
RandomWordsState createState() => RandomWordsState();
}
class RandomWordsState extends State<RandomWords> {
@override
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return Text(wordPair.asPascalCase);
}
}
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
class MyApp extends StatelessWidget {
|
|
11
11
|
@override
|
|
12
12
|
Widget build(BuildContext context) {
|
|
13
|
- final wordPair = WordPair.random();
|
|
14
13
|
return MaterialApp(
|
|
15
14
|
title: 'Welcome to Flutter',
|
|
16
15
|
home: Scaffold(
|
|
@@ -18,8 +17,8 @@
|
|
|
18
17
|
title: Text('Welcome to Flutter'),
|
|
19
18
|
),
|
|
20
19
|
body: Center(
|
|
21
|
- child:
|
|
20
|
+ child: RandomWords(),
|
|
22
21
|
),
|
|
23
22
|
),
|
|
24
23
|
);
|
|
25
24
|
}
|
Tip: Hot reload 할 때 아래와 같은 경고문구가 나온다면, 앱을 재시작하는 게 좋습니다.
Reloading…
Some program elements were changed during reload but did not run when the view was reassembled; you might need to restart the app (by pressing “R”) for the changes to have an effect.
메시지가 잘못 표시된 것일 수 있지만, 재시작하면 변경 사항이 확실하게 앱 UI에 반영됩니다.
앱이 올바르게 동작하지 않는다면, 오타를 확인해보세요. Flutter 디버깅 툴을 사용해보고 싶다면, DevTools 제품군과 프로파일링 툴을 확인해보세요. 필요하다면, 아래 코드를 사용하여 다시 올바르게 동작하게 하세요.
이 단계에서는, RandomWordsState를 확장하여 단어 쌍 목록을 생성하고 표시합니다. ListView 위젯 안에 표시되는 목록이 사용자가 스크롤할 때마다 무한하게 늘어납니다. ListView의 builder 팩토리 생성자를 사용하면 필요에 따라 lazy한 방식으로 목록을 만듭니다.
class RandomWordsState extends State<RandomWords> {
final _suggestions = <WordPair>[];
final _biggerFont = const TextStyle(fontSize: 18.0);
// ···
}
Widget _buildSuggestions() {
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: /*1*/ (context, i) {
if (i.isOdd) return Divider(); /*2*/
final index = i ~/ 2; /*3*/
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10)); /*4*/
}
return _buildRow(_suggestions[index]);
});
}
Widget _buildRow(WordPair pair) {
return ListTile(
title: Text(
pair.asPascalCase,
style: _biggerFont,
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Startup Name Generator'),
),
body: _buildSuggestions(),
);
}
|
@@ -10,15 +10,8 @@
|
|
|
10
10
|
class MyApp extends StatelessWidget {
|
|
11
11
|
@override
|
|
12
12
|
Widget build(BuildContext context) {
|
|
13
13
|
return MaterialApp(
|
|
14
|
- title: '
|
|
15
|
- home:
|
|
14
|
+ title: 'Startup Name Generator',
|
|
15
|
+ home: RandomWords(),
|
|
16
|
- appBar: AppBar(
|
|
17
|
- title: Text('Welcome to Flutter'),
|
|
18
|
- ),
|
|
19
|
- body: Center(
|
|
20
|
- child: RandomWords(),
|
|
21
|
- ),
|
|
22
|
- ),
|
|
23
16
|
);
|
|
24
17
|
}
|


| PHP, Mysql 샘플 소스 (0) | 2023.02.23 |
|---|---|
| 비쥬얼 스튜디오 코드(VS code)에서 FTP 연결하기 (0) | 2023.02.22 |
| [Flutter] Android Studio 에서 Flutter 시작하기 (0) | 2023.02.22 |
| io.Platform.packageRoot; // ignore: deprecated_member_use (0) | 2023.02.21 |
| Flutter 플러터 스트림 통신 (0) | 2023.02.21 |
댓글 영역