如图,我们最终会启动三个客户端:

flutter-ios,flutter-web,flutter-macos

查看可运行设备

flutter devices

创建项目

flutter create <project-name>
flutter create --macos <project-name>

运行设备

其中 device-name 不区分大小写,并且只需要匹配一部分即可

flutter run -d <device-name>

release 真机测试

flutter run -d <device-name> --release

运行web版本

flutter config --enable-web
flutter run -d chrome

如果是历史项目,需要运行命令以添加 web环境

flutter create .

运行桌面版本

google/flutter-desktop-embedding
Examples of using, and testbed for, the in-development Flutter for Desktop - google/flutter-desktop-embedding

虽然桌面版还处于测试阶段,但是它非常适合开发过程,因为他可以很好的响应布局,并且启动更新都会比移动版更快。

桌面版已知缺陷:键盘相关的操作不够完善,如tab切换输入框、输入内容复制粘贴等功能。

桌面版只能运行本机系统的桌面版,本文以运行 macOS 版本为例:

首先需要切换到master:

flutter channel master

打开desktop开发的开关:

flutter config --enable-linux-desktop
flutter config --enable-macos-desktop
flutter config --enable-windows-desktop

当然,最好设置到 .bash-profile 中:

export ENABLE_FLUTTER_DESKTOP=true
export ENABLE_WEB=true

检查iOS开发环境是否正常:

flutter doctor

创建支持macos版本的flutter

flutter create --macos <project-name>

运行:

flutter config --enable-macos-desktop
flutter devices
flutter run -d macos

切换channel之后,首次运行需要下载 Dart SDK from flutter,需要配置科学上网:

Using Flutter in China
Where to find a version of the Flutter site that is localized to Simplified Chinese.

Dart SDK from flutter 的下载经常会中断,但是每次中断都会前进一些,笔者下了10多次,逐步下载成功。

编译:

flutter build linux
flutter build macos
flutter build windows

TargetPlatform 异常处理:

desktop版本需要指定平台版本,masos 指定为 TargetPlatform.iOS 即可

import 'package:flutter/foundation.dart';

void main(){
  debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
  return runApp(MyApp());
}

在后续的flutter版本中,我们可以指定为fuchsia版本:

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

// Sets a platform override for desktop to avoid exceptions. See
// https://flutter.dev/desktop#target-platform-override for more info.
void _enablePlatformOverrideForDesktop() {
  if (!kIsWeb && (Platform.isMacOS || Platform.isWindows || Platform.isLinux)) {
    debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  }
}

void main() {
  _enablePlatformOverrideForDesktop();
  runApp(MyApp());
}