文章目录
- 1.flutter widget分类
- 2. 代理组件又分为
- 3. 状态 state 数据
- 4. 刷新 数据变化
- 5. code
1.flutter widget分类
- 组合
- 渲染
- 代理
2. 代理组件又分为
- Positioned向父组件传递数据
- InheritedWidget向子组件传递数据
3. 状态 state 数据
状态就是用到了向子组件传递数据,并且需要刷新的时候刷新组件
4. 刷新 数据变化
- 获取并在改变的时候会导致刷新
dependOnInheritedWidgetOfExactType- 只获取不会因为数据变化导致刷新
getInheritedWidgetOfExactType
5. code
import 'package:flutter/material.dart';//material cupertino
//widget
//rendering
//foundation 动画 事件 painting
//flutter框架层
//engine
//embedded嵌入式main() {runApp(const MaterialApp(home: StateStudyPage(),));
}class StateStudyPage extends StatefulWidget {const StateStudyPage({super.key});State<StateStudyPage> createState() => _StateStudyPageState();
}class _StateStudyPageState extends State<StateStudyPage> {int aa = 0;int dt1 = 0;int dt2 = 0;Widget build(BuildContext context) {return Scaffold(appBar: AppBar(),body: ShareData2Widget(dt1: dt1,dt2: dt2,child: Column(children: [Text("parent=$aa"),TextButton(onPressed: () {setState(() {aa++;});},child: Text("刷新当前state")),TextButton(onPressed: () {setState(() {dt1++;});},child: Text("刷新当前state dt1=$dt1")),TextButton(onPressed: () {setState(() {dt2++;});},child: Text("刷新当前state dt2=$dt2")),StateStudyChildPage(aa: aa,),],),),);}
}class StateStudyChildPage extends StatefulWidget {final int aa;const StateStudyChildPage({super.key, required this.aa});State<StateStudyChildPage> createState() => _StateStudyChildPageState();
}class _StateStudyChildPageState extends State<StateStudyChildPage> {int a = 0;///由于父widget setState 导致该方法调用void didUpdateWidget(covariant StateStudyChildPage oldWidget) {super.didUpdateWidget(oldWidget);print("更新widget");}///由于用到了共享数据,并在数据变化的时候调用了父组件的setState,导致该方法调用void didChangeDependencies() {super.didChangeDependencies();print("依赖变化");}Widget build(BuildContext context) {return Column(children: [Text("child=$a"),Text("child==aa=${widget.aa}"),Text("child==cc=${ShareData2Widget.of(context, "data")?.dt1}"),TextButton(onPressed: () {setState(() {a++;});},child: Text("刷新当前state"))],);}
}///整体数据刷新
class ShareDataWidget extends InheritedWidget {const ShareDataWidget({super.key,required this.data,required super.child,});final int data; //需要在子树中共享的数据,保存点击次数//定义一个便捷方法,方便子树中的widget获取共享数据static ShareDataWidget? of(BuildContext context) {return context.dependOnInheritedWidgetOfExactType<ShareDataWidget>();}//该回调决定当data发生变化时,是否通知子树中依赖data的Widget重新build bool updateShouldNotify(ShareDataWidget oldWidget) {return oldWidget.data != data;}
}///局部数据导致刷新
class ShareData2Widget extends InheritedModel<String> {const ShareData2Widget({super.key,required this.dt2,required this.dt1,required super.child,});final int dt1;final int dt2;//定义一个便捷方法,方便子树中的widget获取共享数据//type就是根据type来判断是否需要刷新组件static ShareData2Widget? of(BuildContext context, String type) {return context.dependOnInheritedWidgetOfExactType<ShareData2Widget>(aspect: type);}//该回调决定当data发生变化时,是否通知子树中依赖data的Widget重新build bool updateShouldNotify(ShareData2Widget oldWidget) {print("oldWidget.dt1 != dt1=${oldWidget.dt1 != dt1}");return oldWidget.dt1 != dt1 || oldWidget.dt2 != dt2;} bool updateShouldNotifyDependent(covariant ShareData2Widget oldWidget, Set<String> dependencies) {return dependencies.contains("data") && oldWidget.dt1 != dt1 ||dependencies.contains("data2") && oldWidget.dt2 != dt2;}
}