Flutter02-SDK安裝和環(huán)境配置

Flutter SDK安裝

以下以在Mac系統(tǒng)下操作為例
首先在本地創(chuàng)建一個文件夾來存放Flutter SDK似枕,比如我在Documents下創(chuàng)建一個Flutter:

image.png

安裝方式一:

手動下載Flutter SDK
官網(wǎng):https://flutter.io/docs/get-started/install/macos#
Github:https://github.com/flutter/flutter/releases
下載完成后解壓到剛才新建的文件夾下恍箭。

安裝方式二:

終端克隆

git clone -b master https://github.com/flutter/flutter.git

如圖所示:

image.png

安裝完成后會在Flutter下自動生成一個flutter目錄,用來存放SDK,用tree命令查看目錄結(jié)構(gòu):

├── AUTHORS
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── PATENTS
├── README.md
├── analysis_options.yaml
├── bin
│   ├── flutter
│   ├── flutter.bat
│   └── internal
├── dartdoc_options.yaml
├── dev
│   ├── README.md
│   ├── automated_tests
│   ├── benchmarks
│   ├── bots
│   ├── ci
│   ├── devicelab
│   ├── docs
│   ├── integration_tests
│   ├── manual_tests
│   ├── missing_dependency_tests
│   ├── snippets
│   └── tools
├── examples
│   ├── README.md
│   ├── catalog
│   ├── flutter_gallery
│   ├── flutter_view
│   ├── hello_world
│   ├── layers
│   ├── platform_channel
│   ├── platform_channel_swift
│   ├── platform_view
│   └── stocks
├── flutter_console.bat
└── packages
    ├── analysis_options.yaml
    ├── flutter
    ├── flutter_driver
    ├── flutter_goldens
    ├── flutter_goldens_client
    ├── flutter_localizations
    ├── flutter_test
    ├── flutter_tools
    └── fuchsia_remote_debug_protocol
Flutter環(huán)境配置

為了以后使用方便炕矮,我們需要配置一下環(huán)境,需要將項(xiàng)目根目錄下bin路徑加入環(huán)境變量PATH中,打開~/.bash_profile文件袭蝗,修改環(huán)境變量,操作如下:

1般婆、打開~/.bash_profile
open -e .bash_profile #執(zhí)行此命令打開~/.bash_profile
2到腥、配置環(huán)境變量
export PATH={flutter下bin目錄路徑}:$PATH #export PATH=/Users/GRR/Documents/Flutter/flutter/bin:$PATH 我本地的路徑

如下圖所示:


image.png
3、更新環(huán)境配置
source .bash_profile #執(zhí)行此命令更新環(huán)境配置
4蔚袍、驗(yàn)證安裝結(jié)果
flutter #執(zhí)行此命令驗(yàn)證安裝結(jié)果

如果掛了梯子乡范,請?zhí)^第5步;

如果沒掛梯子页响,你可能會看到以下提示:

Jersay:flutter GRR$ flutter
Downloading Dart SDK from Flutter engine 215ca15600887ffeffef2df3f216e0189159150e...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:02:45 --:--:--     0
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to storage.googleapis.com:443 

Failed to retrieve the Dart SDK from: https://storage.googleapis.com/flutter_infra/flutter/215ca15600887ffeffef2df3f216e0189159150e/dart-sdk-darwin-x64.zip
If you're located in China, please see this page:
  https://flutter.io/community/china

image.png

根據(jù)提示篓足,我們來到了官網(wǎng)的這里,往下拉闰蚕,官網(wǎng)寫著(請允許我裝個逼):

If you’re installing or using Flutter in China, it may be helpful to use a trustworthy local mirror site that hosts Flutter’s dependencies. To instruct the Flutter tool to use an alternate storage location, you need to set two environment variables, PUB_HOSTED_URL and FLUTTER_STORAGE_BASE_URL, before running the flutter command.
Taking MacOS or Linux as an example, here are the first few steps in the setup process for using a mirror site. Run the following in a Bash shell from the directory where you wish to store your local Flutter clone:

 export PUB_HOSTED_URL=https://pub.flutter-io.cn
 export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
 git clone -b dev https://github.com/flutter/flutter.git
 export PATH="$PWD/flutter/bin:$PATH"
 cd ./flutter
 flutter doctor

解決辦法就是在環(huán)境變量里加上鏡像地址栈拖,為國人量身打造。

5没陡、配置鏡像地址

重復(fù)上邊步驟1涩哟、2索赏、3添加以下鏡像地址:

 export PUB_HOSTED_URL=https://pub.flutter-io.cn
 export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

繼續(xù)執(zhí)行flutter命令
結(jié)果如下(部分):

Jersay:~ GRR$ flutter
Building flutter tool...
  ╔════════════════════════════════════════════════════════════════════════════╗
  ║ A new version of Flutter is available!                                     ║
  ║                                                                            ║
  ║ To update to the latest version, run "flutter upgrade".                    ║
  ╚════════════════════════════════════════════════════════════════════════════╝
6、更新Flutter
flutter upgrade #執(zhí)行此命令更新flutter

結(jié)果如下(部分):

Jersay:~ GRR$ flutter upgrade
Upgrading Flutter from /Users/GRR/Documents/Flutter/flutter...
From https://github.com/flutter/flutter
   d05fa45fb..4d2d800d9  master     -> origin/master
Updating d05fa45fb..4d2d800d9
 5 files changed, 219 insertions(+), 48 deletions(-)

Upgrading engine...
Downloading Dart SDK from Flutter engine d8c5ec0e11b51ff385327fe5939f9405f89f46c7...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  2 49.7M    2 1465k    0     0  3578k      0  0:00:14 --:--:--  0:00:14 3573k
 14 49.7M   14 7485k    0     0  5347k      0  0:00:09  0:00:01  0:00:08 5346k
7贴彼、安裝Flutter剩余依賴工具
flutter doctor #執(zhí)行此命令安裝依賴

結(jié)果如下(部分):

Jersay:~ GRR$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel master, v1.1.5-pre.24, on Mac OS X 10.14.2 18C54, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
AndroidStudio Flutter插件安裝
1潜腻、安裝Flutter插件

AndroidStudio->Preferences->plugins,搜素Flutter

image.png

2器仗、設(shè)置Flutter SDK路徑
Flutter SDK路徑.png
3融涣、新建Flutter項(xiàng)目

步驟和新建Android項(xiàng)目一樣:
File->New->New Flutter Project

新建Flutter項(xiàng)目.png

Flutter項(xiàng)目包名.png

4、Flutter項(xiàng)目結(jié)構(gòu)

項(xiàng)目主要有3個Module構(gòu)成分別是lib精钮、android威鹿、ios
lib下的main.dart是程序入口

Flutter項(xiàng)目結(jié)構(gòu).png

5轨香、Demo代碼預(yù)覽

程序入口main.dart中代碼如下:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 
    );
  }
}
6忽你、運(yùn)行Flutter項(xiàng)目

run.png

選擇設(shè)備,點(diǎn)擊右上角的綠色三角形按鈕或者使用快捷鍵^R運(yùn)行程序臂容。

在Android設(shè)備運(yùn)行效果如下:


Android_Flutter.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末科雳,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子脓杉,更是在濱河造成了極大的恐慌糟秘,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丽已,死亡現(xiàn)場離奇詭異蚌堵,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)沛婴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門吼畏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人嘁灯,你說我怎么就攤上這事泻蚊。” “怎么了丑婿?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵性雄,是天一觀的道長。 經(jīng)常有香客問我羹奉,道長秒旋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任诀拭,我火速辦了婚禮迁筛,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘耕挨。我一直安慰自己细卧,他們只是感情好尉桩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著贪庙,像睡著了一般蜘犁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上止邮,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天这橙,我揣著相機(jī)與錄音,去河邊找鬼农尖。 笑死析恋,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的盛卡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼筑凫,長吁一口氣:“原來是場噩夢啊……” “哼滑沧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起巍实,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤滓技,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后棚潦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體令漂,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年丸边,在試婚紗的時候發(fā)現(xiàn)自己被綠了叠必。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡妹窖,死狀恐怖纬朝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情骄呼,我是刑警寧澤共苛,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站蜓萄,受9級特大地震影響隅茎,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嫉沽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一辟犀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧耻蛇,春花似錦踪蹬、人聲如沸胞此。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽漱牵。三九已至,卻和暖如春疚漆,著一層夾襖步出監(jiān)牢的瞬間酣胀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工娶聘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留闻镶,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓丸升,卻偏偏與公主長得像铆农,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子狡耻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容