flutter 当父节点刷新子节点时,调用子节点的didUpdateWidget ? 欢迎留言
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
home: InitPage(),
));
}
class InitPage extends StatelessWidget {
const InitPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('点击跳转'),
),
body: TextButton(
child: const Text("跳转页面"),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return const CounterWidget();
}));
},
),
);
}
}
class CounterWidget extends StatefulWidget {
final int initValue;
const CounterWidget({Key? key, this.initValue = 0}) : super(key: key);
@override
State createState() => _CounterWidgetState();
}
class _CounterWidgetState extends State<CounterWidget> {
late int _counter;
bool _magic = false;
//State 对象创建时候初始调用一次
@override
void initState() {
super.initState();
_counter = widget.initValue;
print("Counter initState");
}
//父或祖先组件(InheritedWidget)发生变化时候调用
@override
void didChangeDependencies() {
super.didChangeDependencies();
print("Counter didChangeDependencies");
}
//多次调用重绘界面
@override
Widget build(BuildContext context) {
print("Counter build");
return Scaffold(
appBar: AppBar(
title: const Text('生命周期测试'),
),
body: Center(
child: Column(
children: [
TextButton(
child: Text('$_counter'),
onPressed: () {
setState(
() {
_magic = !_magic;
++_counter;
},
);
},
),
TextButton(
child: const Text('返回上一页面'),
onPressed: () => setState(() {
Navigator.pop(context, "");
}),
),
_magic ? const SonWidget() : const SonWidget(),
],
),
),
);
}
//IDE 热重载调用
@override
void reassemble() {
super.reassemble();
print("Counter reassemble");
}
@override
void didUpdateWidget(CounterWidget oldWidget) {
super.didUpdateWidget(oldWidget);
print("Counter didUpdateWidget");
}
//删除 State 对象调用
@override
void deactivate() {
super.deactivate();
print("Counter deactive");
}
//永久删除 State 对象调用
@override
void dispose() {
super.dispose();
print("Counter dispose");
}
}
class SonWidget extends StatefulWidget {
const SonWidget({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _SonState();
}
class _SonState extends State<SonWidget> {
int _counter = 0;
//State 对象创建时候初始调用一次
@override
void initState() {
super.initState();
print("Son initState");
}
//State 依赖关系(InheritedWidget)发生变化时候调用.一般无需重写
@override
void didChangeDependencies() {
super.didChangeDependencies();
print("Son didChangeDependencies");
}
@override
Widget build(BuildContext context) {
print("Son build");
return Container(
width: 100,
height: 100,
decoration: const BoxDecoration(color: Colors.red),
child: TextButton(
child: Text('$_counter'),
onPressed: () => setState(() {
_counter++;
print("点击事件");
}),
));
}
//IDE 热重载调用
@override
void reassemble() {
super.reassemble();
print("Son reassemble");
}
//flutter 3,子节点被父节点刷新时再次调用时调用?
@override
void didUpdateWidget(SonWidget oldWidget) {
super.didUpdateWidget(oldWidget);
print("Son didUpdateWidget");
}
//删除 State 对象调用
@override
void deactivate() {
super.deactivate();
print("Son deactive");
}
//永久删除 State 对象调用
@override
void dispose() {
super.dispose();
print("Son dispose");
}
}
网友评论