如何在 Linux 上安装和设置 Flutter 开发环境
Flutter
![](/data/attachment/album/202112/06/115835fdjlxmd55mxew1nn.jpg)


谷歌的 UI 工具包 Flutter 在创建移动、网页和桌面的跨平台应用程序方面越来越受欢迎。


[Flutter](https://flutter.dev/) 不是一种编程语言，而是一个软件开发工具包。[Dart](https://dart.dev/) 是 Flutter SDK 下面使用的编程语言。


Flutter 是谷歌开源的 Fuchsia OS、谷歌 STADIA 以及许多其他软件和移动应用背后的主要框架。


如果你想使用 Flutter 进行开发，本教程将帮助你在 Ubuntu 和其他类似 Linux 发行版上搭建好你的环境。


### 在 Ubuntu 和其他 Linux 上用 Snap 安装 Flutter


在 Linux 上安装 Flutter 最简单的方法是使用 Snap。如果你使用的是 Ubuntu，你已经有了 Snap。**对于其他发行版，请确保 [启用 Snap 支持](https://itsfoss.com/install-snap-linux/)**。


[打开终端](https://itsfoss.com/open-terminal-ubuntu/) 并在终端中使用以下命令来安装 Flutter：



```
sudo snap install flutter --classic

```

你会在你的终端上看到类似这样的东西：


![](/data/attachment/album/202112/06/115908c0guz15670d06d6x.png)


一旦安装完成，就是验证它的时候了。不仅仅是 Flutter 的安装，还要验证 Flutter 正常运行所需满足的每一个依赖关系。


#### 验证 Flutter 的依赖项


为了验证 Flutter 正确工作所需的每一个依赖项，Flutter 有一个内置选项：



```
Flutter doctor

```

这个过程开始看起来像这样：


![](/data/attachment/album/202112/06/115909pjh5fiivvxi5vcfi.png)


而它完成时像这样：


![](/data/attachment/album/202112/06/115910der0s2p332p2n0j1.png)


正如你所看到的，我们需要 Android Studio 来工作。所以让我们来安装它。我们该怎么做呢？用 Snap [在 Linux 上安装 Android Studio](https://itsfoss.com/install-android-studio-ubuntu-linux/) 也是毫不费力的。


#### 安装并设置好 Android Studio


在终端中，使用下面的命令来安装 Android Studio：



```
sudo snap install android-studio --classic

```

![](/data/attachment/album/202112/06/115911oket1ntw0bsfsbfq.png)


安装完毕后，从我们的操作系统菜单中打开 Android Studio。


![](/data/attachment/album/202112/06/115912igijeqg1pgdy4yoy.jpg)


就快完成了。现在是配置 Android Studio 的时候了。


![](/data/attachment/album/202112/06/115913k0ofnntswfnfnkno.png)


点击下一步，如果你不想让事情复杂化，就选择“<ruby> 标准 <rt>  standard </rt></ruby>”。


![](/data/attachment/album/202112/06/115914asshz8cahdan0yh4.png)


选择你喜欢的主题（我喜欢“<ruby> 暗色 <rt>  Darcula </rt></ruby>”的）。


![](/data/attachment/album/202112/06/115915w44g54cl7nyfuklz.png)


确认一切正常，然后点击“<ruby> 下一步 <rt>  Next </rt></ruby>”。


![](/data/attachment/album/202112/06/115917i7fwrcfxcxzffq2x.png)


最后，点击“<ruby> 完成 <rt>  Finish </rt></ruby>”按钮。


![](/data/attachment/album/202112/06/115918ex78nrr9yyz4lygi.png)


然后等待，直到下载完成。


![](/data/attachment/album/202112/06/115920rexuef3soxfbu3cu.png)


### 创建一个 Hello World Flutter 应用样本


在 Android Studio 中，进入项目，选择“<ruby> 新建 Flutter 项目 <rt>  New Flutter Project </rt></ruby>”。Flutter SDK 路径会默认设置。


![](/data/attachment/album/202112/06/115921w4hockzas77ocxc4.png)


在这里，神奇的事情开始出现了，这是你设置你的项目名称的地方，在这个例子中，它将被称为 “hello\_world”。


让我们选择三个可用的平台。**Android、iOS 和 Web**。最后，点击“<ruby> 完成 <rt>  Finish </rt></ruby>”。


![](/data/attachment/album/202112/06/115922tmauzq7h71lhku7a.png)


项目中的主文件位于 `lib/main.dart`，如下图所示：


![](/data/attachment/album/202112/06/115925g25vmr5drd5rrar2.png)


选定后，擦除文件中包含的所有内容，并将其改为本示例代码：



```
// 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: const Text('Welcome to Flutter'),
        ),
        body: const Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

```

重要的是，这只是向你展示 Flutter 是如何工作的，如果你确信要学习这种美丽而不可思议的语言，这里有 [文档](https://flutter.dev/docs) 可以看到更多关于它的信息。**尝试**它！


最后，选择 “Chome Web” 设备，并点击“<ruby> 运行 <rt>  Run </rt></ruby>”按钮，如下图所示；并看到神奇的效果！


![](/data/attachment/album/202112/06/115927nm9n4wtyzgqh6mv6.png)


你可以如此快速地创建一个 Flutter 项目，真是不可思议。跟你的 Hello World 项目打个招呼吧。


![](/data/attachment/album/202112/06/115927c2k8uv05guph8fth.png)


### 最后...


如果你想在短时间内做出漂亮的移动和网页界面的贡献，Flutter 和 Dart 是完美的。


现在你知道了如何在 Ubuntu Linux 上安装 Flutter，以及如何用它创建你的第一个应用程序。我很高兴可以为你写这篇文章，希望对你有所帮助，如果你有任何问题，请通过留言或给我发邮件来告诉我，祝你好运!


本教程由 Marco Antonio Carmona Galván 提供，他是物理学和数据科学专业的学生。




---


via: <https://itsfoss.com/install-flutter-linux/>


作者：[Marco Antonio Carmona Galván](https://itsfoss.com/author/itsfoss/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Google’s UI toolkit Flutter is getting increasingly popular for creating cross-platform applications for the mobile, web and desktop.

[Flutter](https://flutter.dev/) is not a programming language but a software development kit. [Dart](https://dart.dev/) is the programming language used underneath the Flutter SDK.

Flutter is the main framework behind Google’s open source Fuchsia OS, Google STADIA and many other software and mobile apps.

If you want to start developing with Flutter, this tutorial will help you to get your set-up ready on Ubuntu and hopefully other Linux distributions.

## Installing Flutter on Ubuntu and other Linux with Snap

The easiest way to install Flutter on Linux is by using Snap. If you are using Ubuntu, you already have got Snap. *For other distributions, please make sure to enable Snap support.*

[Open a terminal](https://itsfoss.com/open-terminal-ubuntu/) and use the following command in a terminal to install Flutter:

`sudo snap install flutter --classic`


You’ll see something like this on your terminal:

![installing Flutter on ubuntu](https://itsfoss.com/content/images/wordpress/2021/09/installing-flutter-ubuntu.png)

Once the installation completes, it is time to verify it. Not just Flutter installation but also verify every dependency that needs to be satisfied for Flutter to function properly.

### Verify Flutter dependencies

To verify that every dependency, for the correct work of Flutter, is installed, Flutter has a built-in option:

`flutter doctor`


The process will start, looking like this:

![Verify Flutter Install](https://itsfoss.com/content/images/wordpress/2021/09/verify-flutter-install.png)

And it will be finishing like this:

![Flutter verification completes](https://itsfoss.com/content/images/wordpress/2021/09/Flutter-verification-completes.png)

As you can see, we need Android Studio for working. So let’s install it. How do we do that? [Installing Android Studio on Linux](https://itsfoss.com/install-android-studio-ubuntu-linux/) is also effortless with Snap.

### Install and set up Android Studio

In a terminal, use the following command to get Android Studio installed:

`sudo snap install android-studio --classic`


![Install Android Studio in Linux with Snap](https://itsfoss.com/content/images/wordpress/2021/09/install-android-studio-linux-snap.png)

Once installed, open Android Studio from our operating system menu.

![open android studio](https://itsfoss.com/content/images/wordpress/2021/09/Open_Android_Studio.webp)

You are almost done. It’s time for configuring Android Studio.

![Setting up Android Studio](https://itsfoss.com/content/images/wordpress/2021/09/Setting-Up-Android-Studio-1-800x603.png)

Click next and select standard if you don’t want to complicate things.

![Setting up Android Studio](https://itsfoss.com/content/images/wordpress/2021/09/Setting-Up-Android-Studio-2-800x603.png)

Select your preferred theme (I like the Dark one).

![Setting up Android Studio](https://itsfoss.com/content/images/wordpress/2021/09/Setting-Up-Android-Studio-3-800x603.png)

Verify that everything is OK and click on Next.

![Setting up Android Studio](https://itsfoss.com/content/images/wordpress/2021/09/Setting-Up-Android-Studio-4-800x603.png)

Finally, hit the Finish button.

![Setting up Android Studio](https://itsfoss.com/content/images/wordpress/2021/09/Setting-Up-Android-Studio-5-800x603.png)

And wait until the download is finished.

![Setting up Android Studio](https://itsfoss.com/content/images/wordpress/2021/09/Setting-Up-Android-Studio-6-800x603.png)

## Creating a sample Hello World Flutter app

In Android Studio, go to Projects and select New Flutter Project. Flutter SDK path will be set by default.

![new flutter project](https://itsfoss.com/content/images/wordpress/2021/09/New_flutter_project-800x639.png)

And here is where the magic starts to appear because this is where you set your project name, which in this case it will be called hello_world.

Let’s select the three available platforms: **Android, iOS, and Web**. And finally, click on Finish.

![Sample Flutter project](https://itsfoss.com/content/images/wordpress/2021/09/sample-flutter-project-800x751.png)

The principal file in the projects is located in `lib/main.dart`

, as is shown in the next image.

![Sample Flutter projects](https://itsfoss.com/content/images/wordpress/2021/09/sample-flutter-project-1-800x435.png)

Once selected, erase everything contained inside the file and change it for this sample code:

```
// 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: const Text('Welcome to Flutter'),
),
body: const Center(
child: Text('Hello World'),
),
),
);
}
}
```


It’s important to say that this is only for showing you how Flutter works, in case you’re convinced about learning this beautiful and incredible language, here is the [Documentation](https://flutter.dev/docs) to see more about it. **Try** it!

Finally, select **Chome Web** device and do click on the **Run** button, as is shown below; and see the magic!

![Sample Flutter project in Ubuntu](https://itsfoss.com/content/images/wordpress/2021/09/sample-flutter-project-2-800x450.png)

It’s incredible how fast you can create a Flutter project. Say hello to your Hello World project.

![Running a sample Flutter project](https://itsfoss.com/content/images/wordpress/2021/09/sample-flutter-project-3-800x549.png)

## In the end…

Flutter and Dart are perfect if you want to contribute with beautiful mobile and Web interfaces in a short time.

Now you know how to install Flutter on Ubuntu Linux and how to create your first app with it. I really enjoyed writing this post for you, hoping this helps you and if you have any questions, please let me know by leaving a comment or sending me an email to [[email protected]](/cdn-cgi/l/email-protection). Good luck!

*Tutorial contributed by Marco Antonio Carmona Galván, a student of physics and data science.*