第一弹:Flutter安装和配置

本文详细介绍了如何配置Flutter开发环境,包括下载SDK、设置环境变量、集成AndroidStudio插件,以及创建并运行第一个带有自定义Text的Flutter项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标:

1)配置Flutter开发环境

2)创建第一个Flutter Demo项目

Flutter中文开发者网站:

https://flutter.cn/

一、配置Flutter开发环境

Flutter开发环境已经提供集成IDE开发环境,因此需要配置开发环境的时候,可以借助IDE。

1.1 下载flutterSDK

1.1.1 下载flutterSDK

可以使用git同步下载flutter SDK
git clone https://github.com/flutter/flutter.git

1.1.2 添加配置环境

配置flutter的环境变量

# Flutter
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export FLUTTER_HOME=/Users/$(whoami)/IDES/flutter

export PATH=/opt/local/bin:$GRADLE_HOME/bin:$JAVA_HOME/bin:$FLUTTER_HOME/bin:$NDK_HOME:$NDK_HOME/build:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$PATH:

执行 flutter devices查看flutter的安装是否成功。

appledeMacBook-Pro:etc zhouronghua$ flutter devices

┌─────────────────────────────────────────────────────────┐
│ A new version of Flutter is available!                  │
│                                                         │
│ To update to the latest version, run "flutter upgrade". │
└─────────────────────────────────────────────────────────┘
1 connected device:

Chrome (web) • chrome • web-javascript • Google Chrome 122.0.6261.94

1.1.3 IDE集成Flutter

Android Studio安装Flutter相关插件,主要有:Dart(Flutter指定编程语言)、Flutter.dev插件。

安装成功重启,重启后,Projects-->New Flutter Project

出现Flutter项目菜单。

二、创建第一个Flutter项目

2.1 创建第一个Flutter项目

创建Flutter项目,选择Flutter菜单。

New Project->Flutter-->Next

然后点击 Next,创建一个Flutter项目

 点击创建的时候,会弹出提示,模块名不正确,需要采用dart的命名方式,小写+下划线。

修改项目名为小写+下划线,创建项目。

 创建项目成功,IDE会自动初始化项目信息。

 2.2 项目结构分析

|-.dart_tool  dart依赖配置

|-.idea  IDEA ItellJ配置

|-android[flutter_world_android]   安卓平台端

|-ios   IOS平台端

|-windows  Windows平台端

|-lib  跨平台库

|-web  资源

|-test  测试程序

2.3 运行项目

选择一个平台设备,然后点击运行,开始将项目安装到指定平台。 

在页面中添加一个Text, 

@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>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            // 新增一个自定义的Text
            Text(
              "我是来自地球村的Flutter新手"
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );

重新运行后,显示新的页面

 

在MacOS上安装配置Flutter的步骤如下: 1. **安装Homebrew**: 如果你还没有安装Homebrew,可以通过终端运行以下命令来安装: ```sh /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 2. **安装Xcode**: Flutter需要Xcode来构建iOS应用。你可以通过App Store安装Xcode。 3. **安装Flutter SDK**: 使用Homebrew安装Flutter SDK: ```sh brew install --cask flutter ``` 4. **设置环境变量**: 将Flutter的路径添加到你的shell配置文件(如`~/.bash_profile`或`~/.zshrc`): ```sh export PATH="$PATH:/usr/local/bin/flutter/bin" ``` 然后运行以下命令使更改生效: ```sh source ~/.bash_profile ``` 或者 ```sh source ~/.zshrc ``` 5. **运行Flutter Doctor**: 通过运行以下命令来检查Flutter安装状态: ```sh flutter doctor ``` 这个命令会检查你的环境并显示任何需要安装的依赖项。 6. **安装Android Studio**: Flutter需要Android Studio来构建Android应用。你可以从[Android Studio官网](https://developer.android.com/studio)下载并安装。 7. **配置Android设备或模拟器**: 你可以使用物理设备或Android模拟器来测试你的Flutter应用。确保你已经启用了USB调试模式(如果使用物理设备)。 8. **安装Flutter插件**: 打开Android Studio,进入`Preferences` -> `Plugins`,搜索并安装Flutter插件。 9. **创建Flutter项目**: 在终端中运行以下命令来创建一个新的Flutter项目: ```sh flutter create my_flutter_app ``` 10. **运行Flutter应用**: 进入项目目录并运行: ```sh flutter run ``` 通过以上步骤,你应该能够在MacOS上成功安装配置Flutter环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值