Flutter02-SDK安装和环境配置

Flutter SDK安装

以下以在Mac系统下操作为例
首先在本地创建一个文件夹来存放Flutter SDK,比如我在Documents下创建一个Flutter:

image.png

安装方式一:

手动下载Flutter SDK
官网: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命令查看目录结构:

├── 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环境配置

为了以后使用方便,我们需要配置一下环境,需要将项目根目录下bin路径加入环境变量PATH中,打开~/.bash_profile文件,修改环境变量,操作如下:

1、打开~/.bash_profile
open -e .bash_profile #执行此命令打开~/.bash_profile
2、配置环境变量
export PATH={flutter下bin目录路径}:$PATH #export PATH=/Users/GRR/Documents/Flutter/flutter/bin:$PATH 我本地的路径

如下图所示:


image.png
3、更新环境配置
source .bash_profile #执行此命令更新环境配置
4、验证安装结果
flutter #执行此命令验证安装结果

如果挂了梯子,请跳过第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

根据提示,我们来到了官网的这里,往下拉,官网写着(请允许我装个逼):

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

解决办法就是在环境变量里加上镜像地址,为国人量身打造。

5、配置镜像地址

重复上边步骤1、2、3添加以下镜像地址:

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

继续执行flutter命令
结果如下(部分):

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 #执行此命令更新flutter

结果如下(部分):

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 #执行此命令安装依赖

结果如下(部分):

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、设置Flutter SDK路径
Flutter SDK路径.png
3、新建Flutter项目

步骤和新建Android项目一样:
File->New->New Flutter Project

新建Flutter项目.png

Flutter项目包名.png

4、Flutter项目结构

项目主要有3个Module构成分别是lib、android、ios
lib下的main.dart是程序入口

Flutter项目结构.png

5、Demo代码预览

程序入口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、运行Flutter项目

run.png

选择设备,点击右上角的绿色三角形按钮或者使用快捷键^R运行程序。

在Android设备运行效果如下:


Android_Flutter.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,056评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,842评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,938评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,296评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,292评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,413评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,824评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,493评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,686评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,502评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,553评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,281评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,820评论 3 305
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,873评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,109评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,699评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,257评论 2 341

推荐阅读更多精彩内容