一.坑系列
1.更改package
要改src/debug/AndroidManifest.xml里的package
2.Waiting for another flutter command to release the starup lock
- 關(guān)閉studio,殺掉dart.ext進程
- 在flutter sdk bin cache里初家,刪掉Lock文件
3.安卓啟動白屏
啟動白屏是因為flutter冷啟動得耗時幾秒,在android的drawable目錄下靴拱,有一個launcher_background.xml涮帘,這個就是啟動時的頁面,修改這個
4.變量寫在類外
描述:有一個動畫實現(xiàn)我用到了三個類,StatefulWidget,State,CustomPainter撒汉,為了讓變量共用,我將變量寫在類外,如圖所示
int value = 1;
class XXX extends StatefulWidget{
value = 100;
}
XXX被pop之后塞淹,再返回這個頁面,這個value還是100,解決變法罪裹,變量寫在類里面饱普,在State里,通過widget.value來獲取坊谁,創(chuàng)建CustomPainter時傳入State费彼,也是通過以上的方法取,最簡單的做法是在StatefulWidget里定義一個方法口芍,重置所有變量的值
5. no device
需要配置android-sdk 和android-studio環(huán)境
flutter config --android-sdk your path
flutter coig --android-studio-dir your path
二.邊學邊做
1. 顏色資源定義與使用
flutter自定Colors類箍铲,但是滿足不了各自的設(shè)計師,定義如下:
class SColors {
///按鈕主色調(diào)
static const Color red_oa = Color(0xffd04f0f);
///按鈕暗色調(diào)
static const Color red_aa = Color(0xffaa4d27);
///按鈕禁止色調(diào)
static const Color red_51 = Color(0xff513f38);
///文字主色調(diào)
static const Color grey_61cc = Color(0x61cccccc);
}
使用:
backgroundColor: SColors.red_oa
2. 頁面跳轉(zhuǎn)與傳遞數(shù)據(jù)
A界面跳轉(zhuǎn):
///跳轉(zhuǎn)到軀干設(shè)置
static void jumpToBodySetting(BuildContext context,
{int trainingChoose, int execiseMode, int execiseOnMode}) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (__) => new BodySettingPage(
trainingType: trainingType,
execiseMode: execiseMode,
execiseOnMode: execiseOnMode)));
}
B界面得到數(shù)據(jù):
int trainingType;
int execiseMode;
int execiseOnMode;
/* 軀干調(diào)節(jié) */
class BodySettingPage extends StatefulWidget {
int trainingType;
BodySettingPage({this.trainingType, execise, execiseOnMode}){
execiseMode = execise;
}
@override
State<StatefulWidget> createState() {
print(trainingType.toString()+",,"+execiseMode.toString()+",,"+execiseOnMode.toString());
return new _BodySettingPageState();
}
}
注:留意這里的賦值鬓椭,execiseOnMode是沒有成功賦值的
3.使用本地資源圖片
- 項目根目錄下颠猴,與lib等級的地方建一個文件夾,比如img小染,圖片放到此去
- 在pubspec.yaml文件翘瓮,在flutter:下定義img:
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
assets:
- img/
注意間距,多一個空格就會報錯
圖片的使用:
Image.asset('img/first_logo.png',
width: 100.0, height: 100.0)
三.語法熟悉
1.給String指定默認值
Text(curUserBean?.birth ?? '1980-10-10'),
//int類型
return (partPulse?.arms ?? 0).toString() + "%";
??= 的意思是如果前面的birth不為null裤翩,則取birth的值资盅,如果為null,則取后面給的默認值
2.給對象里的int類型設(shè)置默認的值
如果你為一個對象創(chuàng)建了一個構(gòu)造器踊赠,那么你需要在構(gòu)造器里為int類型指定默認的值
PartPulseBean({this.id = 0})