creating-your-first-flutter-project について
creating-your-first-flutter-project は、Flutter アプリの作成、確認、実行、テストを解説する入門ガイドです。プロジェクト内のファイルとコマンドライン操作を紹介し、ウィジェットテストやカバレッジ生成も扱います。
主な機能
- 01flutter create によるプロジェクト作成
- 02アプリのロジックと依存関係の確認
- 03コマンドラインからのアプリ実行
- 04ウィジェットテストとカバレッジ生成
活用シーン
- 01初めての Flutter アプリのセットアップ
- 02指定したデバイスでのプロジェクト実行
- 03テストによるウィジェット動作の確認
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ rodydavis/ skills --skill creating-your-first-flutter-projectインストール後、Claude Code で/creating-your-first-flutter-projectを入力して読み込まれたか確認します。
詳細
初めての Flutter プロジェクトを作成する
Flutter は Google の UI ツールキットで、どのプラットフォームでも比類のない、表現力豊かで独自性のあるエクスペリエンスを作成できます。UI を一度書けば、どこでも実行できます。そう、どこでもです!Web、iOS、Android、Windows、Linux、MacOS、Raspberry PI など、さまざまな環境に対応しています…
![]()
動画で学びたい場合は、私が制作している「Flutter Take 5」という YouTube シリーズをご覧ください。Flutter アプリケーションの構築時によく遭遇するトピックを取り上げています。シリーズを進めながら、ヒントやコツも紹介します。
または、こちらの短い動画をご覧ください。
Flutter とは
Flutter は最近 GitHub で React Native を追い越し、現在では 200 万人を超える開発者が Flutter を使ってアプリケーションを作成しています。Google Play だけでも、Flutter で公開されたアプリは 50,000 個以上あります。
はじめに
SDK をインストールすれば、簡単に始められます。インストール後は、新しいアプリケーション、プラグイン、パッケージを非常に速く作成できます。次のガイドに従って Flutter をインストールしてください。
Flutter の優れた点のひとつは、誰でも貢献できるオープンソースプロジェクトとして開発されていることです。不足している機能があれば、リポジトリをフォークして、足りない機能を追加する PR を簡単に作成できます。
![]()
プロジェクトを作成する
Flutter のインストールが完了したので、いよいよ初めての(たくさん作るうちのひとつの 😉)Flutter プロジェクトを作成しましょう!ターミナルを開き、アプリケーションのフォルダーを作成したい場所に移動してください。ディレクトリに「cd」で移動したら、次のコマンドを入力します。
flutter create my_awesome_project
「my_awesome_project」は、好きなプロジェクト名に置き換えられます。dart で有効なプロジェクト名の構文は snake_case なので、それを使うことが重要です。
![]()
おめでとうございます。これで最初のプロジェクトが作成されました!
プロジェクトを開く
作成したものが何なのか気になるかもしれません。詳しく見ていきましょう。VSCode がインストールされている場合は、ターミナルで次のコマンドを入力してプロジェクトを開けます。
cd my_awesome_project && code .
お好みで、好きな IDE からフォルダーを開くこともできます。注目すべき重要なファイルは、pubspec.yaml と lib/main.dart の 2 つです。
UI とロジックは「lib/main.dart」にあります。次のように表示されるはずです。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
アプリケーションに必要な依存関係やプラグインは「pubspec.yaml」で定義できます。次のように表示されるはずです。
name: example
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.webp
# - images/a_dot_ham.webp
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
プロジェクトを実行する
アプリケーションの実行も簡単です。どの IDE にもボタンがありますが、すばやくテストしたい場合は、コマンドラインからプロジェクトを実行することもできます。また、Flutter for Desktop を設定すれば、エミュレーターの起動を待つ必要もありません。プロジェクトを開き、ターミナルで次のコマンドを入力してください。
flutter run -d macos
「-d macos」に注目してください。実行するデバイスは変更できます。ターミナルに次のように表示されるはずです。
Building macOS application...
Syncing files to device macOS... 141ms
Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h Repeat this help message.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).
An Observatory debugger and profiler on macOS is available at: [http://127.0.0.1:58932/f1Mspofty_k=/](http://127.0.0.1:58932/f1Mspofty_k=/)
Application finished.
複数のデバイスを同時に実行することもできます。詳しくは、Flutter Octopus のこちらのページをご覧ください。問題なく進めば、次のようにアプリケーションが起動します。
![]()
現時点ではごく基本的なアプリケーションですが、アプリケーションの状態を簡単に変更できることを示すうえで重要です。「setState()」を呼び出すだけで UI を再構築できます。
プロジェクトのテスト
テストは、私が Flutter をとても気に入っている理由の一つです。プロジェクトのテストを実行したり作成したりするのは、とても簡単です。「test/widget_test.dart」ファイルを見ると、次のようになっています。
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:example/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
これらのテストはとても簡単に実行できます。プロジェクトを開き、ターミナルに次のように入力してください。
flutter test
00:07 +1: All tests passed!
これだけで、すべてのテストが実行され、見落としていたバグを見つけることができます。
![]()
次のように入力するだけで、アプリケーションのコードカバレッジも簡単に生成できます。
flutter test --coverage
これにより、「coverage/lcov.info」に新しいファイルが生成され、次の内容が読み込まれます。
SF:lib/main.dart
DA:3,0
DA:4,0
DA:9,1
DA:11,1
DA:13,1
DA:27,1
DA:29,1
DA:35,2
DA:48,1
DA:49,1
DA:55,1
DA:56,2
DA:62,2
DA:66,1
DA:74,1
DA:75,1
DA:78,3
DA:80,1
DA:83,1
DA:99,1
DA:100,1
DA:103,1
DA:104,2
DA:105,3
DA:110,1
DA:111,1
DA:113,1
LF:27
LH:25
end_of_record
LCOV データを使って、バッジやグラフを簡単に作成できるようになりました。作成を簡単にしてくれるパッケージをご紹介します。
まとめ
Flutter を使えば、ウェブやモバイルのテクノロジーに依存しないアプリケーションを非常に短時間で構築できます。UI をすべて自分で設計する必要があるため、ゲームを作るような感覚で開発できます。最終的なソースコードはこちらで確認できます。
Flutter のソースコードはこちらで確認できます。