当前位置:网站首页>Using repositoryprovider to simplify the value passing of parent-child components
Using repositoryprovider to simplify the value passing of parent-child components
2022-07-05 19:36:00 【InfoQ】
Preface
- Constructors pass values : The parent component passes the objects required by the child component to the child component through the constructor ;
- Singleton object : Building singleton objects , Make the parent and child components use the same object ;
- Containers : Store objects in containers , When the parent and child components are used, they are directly obtained from the container .
RepositoryProvider
Provider
RepositoryProvider Definition
Repository
Provider
Provider
Bloc
Bloc
RepositoryProvider
create
value
create
value
RepositoryProvider
class RepositoryProvider<T> extends Provider<T>
with RepositoryProviderSingleChildWidget {
RepositoryProvider({
Key? key,
required Create<T> create,
Widget? child,
bool? lazy,
}) : super(
key: key,
create: create,
dispose: (_, __) {},
child: child,
lazy: lazy,
);
RepositoryProvider.value({
Key? key,
required T value,
Widget? child,
}) : super.value(
key: key,
value: value,
child: child,
);
static T of<T>(BuildContext context, {bool listen = false}) {
try {
return Provider.of<T>(context, listen: listen);
} on ProviderNotFoundException catch (e) {
if (e.valueType != T) rethrow;
throw FlutterError(
'''
RepositoryProvider.of() called with a context that does not contain a repository of type $T.
No ancestor could be found starting from the context that was passed to RepositoryProvider.of<$T>().
This can happen if the context you used comes from a widget above the RepositoryProvider.
The context used was: $context
''',
);
}
}
}
mixin RepositoryProviderSingleChildWidget on SingleChildWidget {}
MultiRepositoryProvider
RepositoryProvider
RepositoryProvider
Provider
of
listen
false
// The way 1
context.read<T>()
// The way 2
RepositoryProvider.of<T>(context)
MultiRepositoryProvider
MultiRepositoryProvider(
providers: [
RepositoryProvider<RepositoryA>(
create: (context) => RepositoryA(),
),
RepositoryProvider<RepositoryB>(
create: (context) => RepositoryB(),
),
RepositoryProvider<RepositoryC>(
create: (context) => RepositoryC(),
),
],
child: ChildA(),
)
RepositoryProvider application
- Avatar and background :
_getBannerWithAvatar
;
- The personal data :
_getPersonalProfile
;
- Personal data statistics :
_getPersonalStatistic
.

PersonalEntity personalProfile = personalResponse.personalProfile!;
return Stack(
children: [
CustomScrollView(
slivers: [
_getBannerWithAvatar(context, personalProfile),
_getPersonalProfile(personalProfile),
_getPersonalStatistic(personalProfile),
],
),
// ...
],
);
},
//...
personalProfile

personalProfile
RepositoryProvider.value(
child: CustomScrollView(
slivers: [
const BannerWithAvatar(),
const PersonalProfile(),
const PersonalStatistic(),
],
),
value: personalProfile,
),
// ...
value
personalProfile
personalProfile
context.read<PersonalEntity>()
RepositoryProvider
personalProfile
class BannerWithAvatar extends StatelessWidget {
final double bannerHeight = 230;
final double imageHeight = 180;
final double avatarRadius = 45;
final double avatarBorderSize = 4;
const BannerWithAvatar({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SliverToBoxAdapter(
child: Container(
height: bannerHeight,
color: Colors.white70,
alignment: Alignment.topLeft,
child: Stack(
children: [
Container(
height: bannerHeight,
),
Positioned(
top: 0,
left: 0,
child: CachedNetworkImage(
imageUrl:
'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=688497718,308119011&fm=26&gp=0.jpg',
height: imageHeight,
width: MediaQuery.of(context).size.width,
fit: BoxFit.fill,
),
),
Positioned(
left: 20,
top: imageHeight - avatarRadius - avatarBorderSize,
child: _getAvatar(
context.read<PersonalEntity>().avatar,
avatarRadius * 2,
avatarBorderSize,
),
),
],
),
),
);
}
Widget _getAvatar(String avatarUrl, double size, double borderSize) {
return Stack(alignment: Alignment.center, children: [
Container(
width: size + borderSize * 2,
height: size + borderSize * 2,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(size / 2 + borderSize),
),
),
Container(
width: size,
height: size,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(size / 2),
),
child: CachedNetworkImage(
imageUrl: avatarUrl,
height: size,
width: size,
fit: BoxFit.fill,
),
),
]);
}
}
summary
RepositoryProvider
RepositoryProvider
Provider
RepositoryProvider
context
RepositoryProvider.of

边栏推荐
- Ten years at sea: old and new relay, dark horse rising
- Fuzor 2020软件安装包下载及安装教程
- 使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
- Postman core function analysis - parameterization and test report
- PHP uses ueditor to upload pictures and add watermarks
- Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
- Worthy of being a boss, byte Daniel spent eight months on another masterpiece
- After the company went bankrupt, the blackstones came
- Go语言 | 02 for循环及常用函数的使用
- How to apply smart contracts more wisely in 2022?
猜你喜欢
安卓面试宝典,2022Android面试笔试总结
40000 word Wenshuo operator new & operator delete
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
After the company went bankrupt, the blackstones came
word如何转换成pdf?word转pdf简单的方法分享!
redis集群模拟消息队列
Android面试,android音视频开发
The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
C#应用程序界面开发基础——窗体控制(5)——分组类控件
What does software testing do? What are the requirements for learning?
随机推荐
618“低调”谢幕,百秋尚美如何携手品牌跨越“不确定时代”?
JAD installation, configuration and integration idea
手机股票开户安全吗?靠不靠谱啊?
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
Necessary skills for interview in large factories, 2022android will not die, I will not fall
力扣 1200. 最小绝对差
MMO项目学习一:预热
How about testing outsourcing companies?
Pandora IOT development board learning (HAL Library) - Experiment 8 timer interrupt experiment (learning notes)
Postman core function analysis - parameterization and test report
Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
Shell编程基础(第8篇:分支语句-case in)
不愧是大佬,字节大牛耗时八个月又一力作
集合
The problem of returning the longtext field in MySQL and its solution
What are the reliable domestic low code development platforms?
HiEngine:可媲美本地的云原生内存数据库引擎
What is the function of okcc call center
Debezium系列之:修改源码支持drop foreign key if exists fk
How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI