Flutter でプロジェクトを新規に作成するのは、Android Studioでも可能ですが
VSCodeが軽くていいのとGoogleの推奨でもあります
New Project
1. プロジェクト作成
2. 仮想デバイスで実行
2.1 Chrome
2.2 Android Studio エミュレータ
2.3 iOS Simulator
2.4 iOS Simulator
3. 実機で実行
プロジェクト作成
VSCodeを起動します
メニューから「View」「Command Palette」
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
flutter と入力
候補が出てくるので
flutter: New Project
を選択
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
次に、which Flutter template?
と出てくるのでとりあえず
Application
を選択
そのほかにも用途に合わせたテンプレートがあります
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
プロジェクトを保存する先を指定します
指定したら「Select a folder to create the projection in」をクリック
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
プロジェクト名を決めます
ここでは推奨で出てきた、flutter_application_1 にします
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
プロジェクトが作成されました
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
仮想デバイスで実行
実行させるDeviceを決めます
右下隅に「No Device」とあるのでそれをクリック
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Chrome
現状使えるデバイスがリストアップされます
とりあえず「Chrome」でやってみましょう
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
メニューの「Run」「Start Debugging」をクリック
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Chromeで実行されています
ボタンクリックで数字が増えていきます
Android Studio エミュレータ
Android Studio でエミュレータを作成してあればこのリストに上がります
エミュレータの作成は以下を参考に;
Pixel 8 API 35 を選択して実行してみます
自動ではADBを呼び出せないので設定をマニュアルでやらないとできないというワーニングが出てきました
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
この Extended Controls がどこか悩んだんですが、エミュレータの設定でした
エミュレータの右側にメニュー表示されている一番下の「…」アイコンから
「Settings」そして「Use detected ADB location」
これが自動に設定されているのではずします
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
オフにするとロケーションを探すためのフォルダアイコンが出てきます
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
adbの場所はパス設定をするときにやっていると思います
adb を選択して「Open」
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
自動設定はオフのままです
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
これで Pixel 8 で実行してみると
エミュレータでアプリが実行されました
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
iOS Simulator
今度はiOSシミュレータで実行してみましょう
Xcodeでシミュレータを動かしてないとエラーになることがあるようなので
一度は起動させておきます
「Start iOS Simulator」を選択してRunすると
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
iOSシミュレータが起動しアプリが実行されました
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
ホットリロード
例えば、テキスト内容を変えてレイアウトを確認するなどでも
再度一から実行、Runさせると時間がかかります
そういうケースではホットリロードが使えます
VSCodeで一度コードを実行させると
雷アイコンが現れます
これを実行させると、簡単にコードの修正を確認できます
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
ただし、ホットリロードでは、
Widgetの再構築をするだけなので
initState()などのメソッドは実行されないので注意です
実機で実行
Androidデバイスは開発用の設定が必要です
実機で実行させてみると
Pixel 6a でできました
![[Flutter] VSCode 初めてのプロジェクト作成 1x1.trans - [Flutter] VSCode 初めてのプロジェクト作成](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)