1、盡量避免在 build() 中執(zhí)行耗時操作疼进,因為build()
會被頻繁地調用
2兰迫、盡可能地使用 const
構造器信殊,子組件也不會重新進行 rebuild
操作
3、盡量避免使用 ListView
和GridView
的默認構造方法汁果,使用builder
方法創(chuàng)建,盡可能的指定itemExtent
或prototypeItem
4鸡号、在Image
加載圖片的時候盡量指定 cacheWidth
和 cacheHeight
的展示大小
5、如果ListView的item中有圖片须鼎,需要設置addRepaintBoundaries
和addAutomaticKeepAlives
為false
鲸伴,這樣就可以在item滑出屏幕外的時候銷毀掉,防止內存溢出
6晋控、如果一個頁面中某一個組件一直頻繁構建可以用RepaintBoundary
來包裹隔離汞窗,這樣不會整個頁面組件都重新構建,但是在頁面中盡量少用這個組件赡译,構建它也很消耗性能
7仲吏、在低性能手機里可以降級CustomScrollView
和ListView
的預渲染區(qū)域
//默認上下250,120可以改成item的高度大小
cacheExtent: 120,
8蝌焚、透明度不用Opacity
可用Color
代替 裹唆、剪裁不用Clip
可用組件的borderRadius
屬性代替、淡入淡出只洒,請考慮使用 FadeInImage
9许帐、盡量用SizedBox
而不是Container
,占位組件使用SizedBox.Shrink()
10、在dio
請求中毕谴,處理json
數(shù)據(jù)可用compute
方法放在多線程中進行
11成畦、抽離Widget組件的時候不要用函數(shù)返回
Widget _buildFooterWidget() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text('This is the footer '),
);
}
-------改成-------
class FooterWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text('This is the footer '),
);
}
}
12、如果只是判斷某個條件成立才加載組件可以用if (text != null) Text(text)
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者