1. didChangeDependencies
這個(gè)生命周期方法很多文章沒講明白,大都一語帶過
- 這個(gè)方法設(shè)計(jì)的目的是什么止吁?
- 告訴你你的圖層結(jié)構(gòu)發(fā)生了變化
- 告訴你你的代碼正在執(zhí)行耗費(fèi)性能的操作或者存在性能問題被辑,圖層正在進(jìn)行較大成本的重布局、重繪制敬惦、重合成
/// Subclasses rarely override this method because the framework always
/// calls [build] after a dependency changes. Some subclasses do override
/// this method because they need to do some expensive work (e.g., network
/// fetches) when their dependencies change, and that work would be too
/// expensive to do for every build.
@protected
@mustCallSuper
void didChangeDependencies() { }
- 依賴變化具體是指什么發(fā)生了變化盼理?是widget的類型?布局俄删?某些屬性值宏怔?
先拋結(jié)論:widget樹中,若節(jié)點(diǎn)的父級結(jié)構(gòu)中的層級
或 父級結(jié)構(gòu)中的任一節(jié)點(diǎn)的widget類型
有變化畴椰,節(jié)點(diǎn)會
調(diào)用didChangeDependencies臊诊;若僅僅是父級結(jié)構(gòu)某一節(jié)點(diǎn)的widget的某些屬性值
變化,節(jié)點(diǎn)不會
調(diào)用didChangeDependencies
看個(gè)例子
- A
import 'package:flutter/material.dart';
class A extends StatefulWidget {
@override
State<StatefulWidget> createState() => AState();
}
class AState extends State<A> {
bool bDependenciesShouldChange = false;
@override
void initState() {
super.initState();
Future.delayed(Duration(seconds: 15), () {
bDependenciesShouldChange = true;
setState(() {});
});
}
@override
Widget build(BuildContext context) {
/// B`父級結(jié)構(gòu)中的層級`發(fā)生變化時(shí)(`Scaffold`迅矛、`Container`妨猩、`C`潜叛、`B` => `Scaffold`秽褒、`Container`、`C`威兜、`SizedBox`销斟、`B`), B會執(zhí)行didChangeDependencies
return bDependenciesShouldChange
? Scaffold(
body: Container(
height: 500,
alignment: Alignment.centerLeft,
child: C(child: B()),
))
: Scaffold(
body: Container(
height: 500,
alignment: Alignment.centerLeft,
child: C(child: SizedBox(width: 20, height: 50, child: B())),
));
// /// B`父級結(jié)構(gòu)中的任一節(jié)點(diǎn)的widget類型`發(fā)生變化時(shí)(`Container` => `Center`)椒舵, B會執(zhí)行didChangeDependencies
// return bDependenciesShouldChange
// ? Scaffold(body: Center(child: C(child: B())))
// : Scaffold(
// body: Container(
// height: 500,
// alignment: Alignment.centerLeft,
// child: C(child: B()),
// ));
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
debugPrint("AdidChangeDependencies");
}
}
class B extends StatefulWidget {
@override
State<StatefulWidget> createState() => BState();
}
class BState extends State<B> {
@override
Widget build(BuildContext context) {
return Center(child: Text("B"));
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
debugPrint("BdidChangeDependencies");
}
}
class C extends StatefulWidget {
final Widget child;
C({Key key, this.child}) : super(key: key);
@override
State<StatefulWidget> createState() => CState();
}
class CState extends State<C> {
@override
Widget build(BuildContext context) {
return widget.child;
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
debugPrint("CdidChangeDependencies");
}
}
2. didUpdateWidget
先拋結(jié)論:widget樹中蚂踊,若節(jié)點(diǎn)調(diào)用setState方法,節(jié)點(diǎn)本身不會
觸發(fā)didUpdateWidget笔宿,此節(jié)點(diǎn)的子節(jié)點(diǎn) 會
調(diào)用didUpdateWidget
看個(gè)例子
A調(diào)用reload后犁钟,A不會
調(diào)用didUpdateWidget,A的子節(jié)點(diǎn)B會
調(diào)用didUpdateWidget
- A
class A extends StatefulWidget {
@override
State<StatefulWidget> createState() => AState();
}
class AState extends State {
@override
Widget build(BuildContext context) {
return B();
}
/// AState調(diào)用此方法后泼橘,[AState.didUpdateWidget]不會調(diào)用, [BState.didUpdateWidget]會調(diào)用
void reload() {
setState(() {});
}
@override
void didUpdateWidget(StatefulWidget oldWidget) {
super.didUpdateWidget(oldWidget);
}
}
class B extends StatefulWidget {
@override
State<StatefulWidget> createState() => AState();
}
class BState extends State {
@override
Widget build(BuildContext context) {
return null;
}
/// AState調(diào)用[AState.reload]后涝动,[AState.didUpdateWidget]不會調(diào)用, [BState.didUpdateWidget]會調(diào)用
@override
void didUpdateWidget(StatefulWidget oldWidget) {
super.didUpdateWidget(oldWidget);
}
}
推薦自維護(hù)的一個(gè)Flutter下載器及相關(guān)文章:
al_downloader
Flutter 下載器 al_downloader