flutter版本:2.0.5
有时间就写,慢慢填坑
参考
flutter
开发工具
一些功能可以提高开发效率,建议一看
常用命令
flutter clean #清空构建和依赖缓存
flutter pub get #拉取依赖
空安全支持
迁移指南
要确保你使用的库也支持空安全
# 自动升级
dart migrate --apply-changes
平台判断
在web浏览器的环境下不能使用Platform判断,判断是否为web用kIsWeb
import 'dart:io';
import 'package:flutter/foundation.dart';
class DeviceType {
static bool isWeb = kIsWeb;
static bool isIOS = !isWeb && Platform.isIOS;
static bool isAndroid = !isWeb && Platform.isAndroid;
static bool isMacOS = !isWeb && Platform.isMacOS;
static bool isLinux = !isWeb && Platform.isLinux;
static bool isWindows = !isWeb && Platform.isWindows;
static bool get isDesktop => isWindows || isMacOS || isLinux;
static bool get isMobile => isAndroid || isIOS;
static bool get isDesktopOrWeb => isDesktop || isWeb;
static bool get isMobileOrWeb => isMobile || isWeb;
}
Adnroid打包
# 为arm、arm64生成不同的apk,减小apk大小
flutter build apk --target-platform android-arm,android-arm64 --split-per-abi
# 安装
adb install .\build\app\outputs\flutter-apk\app-arm64-v8a-release.apk
flutter plugin
ios
ios在编写原生的代码的时候,要先在Android studio运行一下,让CocoaPods生成一下项目文件,然后再用xcode打开,否则在目录都看不到原生的plugin文件
Web
启用web支持
flutter config --enable-web
## 在已有项目的根目录创建web模板
flutter create .
运行
flutter run -d chrome
打包
web打包可以选择两种渲染模式,pc端对应canvaskit,移动端对应html
# 生成文件在build/web
# 移动端优化
flutter build web --web-renderer html
# pc端优化
flutter build web --web-renderer canvaskit
起服务
需要python环境支持
python -m http.server 80 --directory .\build\web\
Windows
启用windows支持
flutter config --enable-windows-desktop
## 在已有项目的根目录创建windows模板
flutter create .
运行
flutter run -d windows
打包
# 生成文件在build/windows/runner/Release
flutter build windows