Flutter(十)| FAQ
记录一下在使用Flutter过程中经常碰到的一些问题?
锁定App方向
在使用App的时候,有时候我们需要锁定App横屏或竖屏,可以修改main.dart文件中的build接口:
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
DeviceOrientation一共有4个选项,可以多项:
enum DeviceOrientation {
/// If the device shows its boot logo in portrait, then the boot logo is shown
/// in [portraitUp]. Otherwise, the device shows its boot logo in landscape
/// and this orientation is obtained by rotating the device 90 degrees
/// clockwise from its boot orientation.
portraitUp,
/// The orientation that is 90 degrees clockwise from [portraitUp].
///
/// If the device shows its boot logo in landscape, then the boot logo is
/// shown in [landscapeLeft].
landscapeLeft,
/// The orientation that is 180 degrees from [portraitUp].
portraitDown,
/// The orientation that is 90 degrees counterclockwise from [portraitUp].
landscapeRight,
}
App标题栏
App标题栏和手机最上面的状态栏使用同一颜色,也是修改main.dart文件中的build接口:
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
));
消除右上角Debug标志
修改main.dart文件中的build接口,在MaterialApp下面新增一行代码:
debugShowCheckedModeBanner: false,
Release版
flutter run默认是运行debug版本,如果要发布release版,使用下面的命令:
flutter build apk
打包apkflutter build web
打包web版本
apk所在目录build\app\outputs\apk\release\app-release.apk。
web所在目录build\web
Bottom Overflow
当打开屏幕键盘的时候,body和Scaffold会自行调整大小,如果手机屏幕太小,有可能导致Widget超出屏幕,报”bottom overflowed by pixels”的错误,可以修改build接口下的Scaffold:
home: Scaffold(
resizeToAvoidBottomInset: false,
...
),
设置为false之后,再打开屏幕键盘,就不会报错了,但是有可能屏幕键盘会遮挡住文本输入框。
而且这个错误只会在debug版本中显示,在release版本中不会显示错误,所以大可不必处理。
Gradle错误
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
gradle-wrapper.properties配置文件中,通过distributionUrl的下载超时:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip
我们可以从https://services.gradle.org/distributions/手动下载对应的包,然后指定路径即可:
distributionUrl=file:///D:/download/gradle-7.6.3-all.zip
或者也可以把下载后的压缩包放到对应的下载路径,aocdy2d2z8kodnny3rsumj8i8是随机生成的:
C:\Users\登录账号\.gradle\wrapper\dists\gradle-7.6.3-all\aocdy2d2z8kodnny3rsumj8i8
Gradle编译慢
使用阿里云镜像仓库:https://developer.aliyun.com/mvn/guide
修改android\settings.gradle
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
mavenLocal()
// google()
// mavenCentral()
// gradlePluginPortal()
}
然后执行命令
cd .\android\
.\gradlew dependencies